SQL Data Definition Language
In this section, the basic SQL Data Definition Language statements are introduced and their syntax is given with examples.schema.object_nameBy default, all objects are created in the user's own schema. For example, when JONES creates a database object such as a table, it is created in her own schema. If JONES creates an EMPLOYEE table, the full name of the table becomes:
JONES.EMPLOYEE. Thus database objects with the same name can be created in more than one schema. This feature allows each user to have their own EMPLOYEE table, for example. Create, Modify and Drop Tables, Views and Sequences
- ALTER TABLE - Change an existing table definition. The table indicated in the ALTER statement must already exist. This statement can be used to add a new column or remove an existing column in a table, modify the data type for an existing column, or add or remove a constraint. ALTER TABLE has the following syntax for adding a new column to an existing table:
<br> ALTER TABLE &amp;lt;table name&amp;gt;<br> ADD ( &amp;lt;column name&amp;gt; &amp;lt;data type&amp;gt; <br>&amp;lt;[not]null&amp;gt; ) ;<br> Another ALTER TABLE option can change a data type of column. The syntax is:
<br> ALTER TABLE &amp;lt;table name&amp;gt;<br> MODIFY ( &amp;lt;column name&amp;gt; &amp;lt;new data <br>type&amp;gt; &amp;lt;[not]null&amp;gt; ) ;<br>
Finally, ALTER TABLE can also be used to add a constraint to a table such as for a PRIMARY KEY, FOREIGN KEY or CHECK CONSTRAINT. The syntax to add a PRIMARY KEY is:<br> ALTER TABLE &amp;lt;table name&amp;gt;<br> ADD CONSTRAINT &amp;lt;constraint-name&amp;gt;<br> PRIMARY KEY (&amp;lt;column-name&amp;gt;);<br> The syntax to add a FOREIGN KEY constraint is:<br> ALTER TABLE &amp;lt;table-name&amp;gt;<br> ADD CONSTRAINT &amp;lt;constraint-name&amp;gt;<br> FOREIGN KEY (&amp;lt;column-name&amp;gt;) <br> REFERENCES &amp;lt;parent-table-name&amp;gt; (column-name);<br> In Oracle, you must use an ALTER TABLE statement to define a composite PRIMARY KEY (a key made up of two or more columns).
NOTE: In Oracle, there is no single command to drop a column of a table. In order to drop a column from a table, you must create a temporary table containing all of the columns and records that will be retained. Then drop the original table and rename the temporary table to the original name. This is demonstrated below in the section on Creating, Altering and Dropping Tables.
- CREATE TABLE - Create a new table in the database. The table name must not already exist. CREATE TABLE has the following syntax:
<br> CREATE TABLE &amp;lt;table_name&amp;gt;<br> ( &amp;lt;column1_name&amp;gt; &amp;lt;data type&amp;gt; <br>&amp;lt;[not]null&amp;gt;,<br> &amp;lt;column2_name&amp;gt; &amp;lt;data type&amp;gt; <br>&amp;lt;[not]null&amp;gt;,<br> . . .<br> ) ;<br> An alternate syntax can be used to create a table with a subset of rows or columns from an existing table.
<br> CREATE TABLE &amp;lt;table_name&amp;gt; AS<br> &amp;lt;sql select statement&amp;gt; ;<br>
- DROP TABLE - Drop a table from the database. The table name must already exist in the database. The syntax for the DROP TABLE statement is:
<br> DROP TABLE &amp;lt;table name&amp;gt; ;<br>
- CREATE INDEX - Create a new Index that facilitates rapid lookup of data. An index is typically created on the primary and/or secondary keys of the table. The basic syntax for the CREATE INDEX statement is:
<br> CREATE INDEX &amp;lt;index name&amp;gt;<br> ON &amp;lt;table name&amp;gt; <br> ( &amp;lt;column name&amp;gt;, &amp;lt;column <br>name&amp;gt; ) ;<br>
- DROP INDEX - Drop an index from the database. The syntax for the DROP INDEX statement is:
<br> DROP INDEX &amp;lt;index name&amp;gt; ;<br>
- CREATE SEQUENCE - Create a new Oracle Sequence of values. The new sequence name must not exist. CREATE SEQUENCE has the following syntax:
<br> CREATE SEQUENCE &amp;lt;sequence name&amp;gt; <br> INCREMENT BY &amp;lt;increment number&amp;gt;<br> START WITH &amp;lt;start number&amp;gt;<br> MAXVALUE &amp;lt;maximum value&amp;gt; <br> CYCLE ;<br> - DROP SEQUENCE - Drop an Oracle Sequence. The sequence name must exist. DROP SEQUENCE has the following syntax:
<br> DROP SEQUENCE &amp;lt;sequence name&amp;gt; ;<br>
- CREATE VIEW - Create a new view based on existing tables in the database. The table names must already exist. The new view name must not exist. CREATE VIEW has the following syntax:
<br> CREATE VIEW &amp;lt;view name&amp;gt; AS<br> &amp;lt;sql select statement&amp;gt; ;<br>
where sql select statement is in the form:
<br> SELECT &amp;lt;column names&amp;gt;<br> FROM &amp;lt;table name&amp;gt;<br> WHERE &amp;lt;where clause&amp;gt;<br>
Additional information on the SELECT statement and SQL queries can be found in the next section.
Note that an ORDER BY clause may not be added to the sql select statement when defining a view.
In general, views are read-only. That is, one may query a view but it is normally the case that views can not be operated on with INSERT, UPDATE or DELETE. This is especially true in cases where views joing two or more tables together or when a view contains an aggregate function.
- DROP VIEW - Drop a view from the database. The view name must already exist in the database. The syntax for the DROP VIEW command is:
DROP VIEW &amp;lt;view name&amp;gt; ;
Creating, Altering and Dropping Tables
- VARCHAR2 - Character data type. Can contain letters, numbers and punctuation. The syntax for this data type is: VARCHAR2(size) where size is the maximum number of alphanumeric characters the column can hold. For example VARCHAR2(25) can hold up to 25 alphanumeric characters. In Oracle8, the maximum size of a VARCHAR2 column is 4,000 bytes. The VARCHAR data type is a synonym for VARCHAR2. It is recommended to use VARCHAR2 instead of VARCHAR.
- NUMBER - Numeric data type. Can contain integer or floating point numbers only. The syntax for this data type is: NUMBER(precision, scale) where precision is the total size of the number including decimal point and scale is the number of places to the right of the decimal. For example, NUMBER(6,2) can hold a number between -999.99 and 999.99.
- DATE - Date and Time data type. Can contain a date and time portion in the format: DD-MON-YY HH:MI:SS. No additional information is needed when specifying the DATE data type. If no time component is supplied when the date is inserted, the time of 00:00:00 is used as a default. The output format of the date and time can be modified to conform to local standards.
- RAW - Free form binary data. Can contain binary data up to 255 characters. Data type LONG RAW can contain up to 2 gigabytes of binary data. RAW and LONG RAW data cannot be indexed and can not be displayed or queried in SQL*Plus. Only one RAW column is allowed per table.
- LOB - Large Object data types. These include BLOB (Binary Large OBject) and CLOB (Character Large OBject). More than one LOB column can appear in a table. These data types are the prefferred method for storing large objects such as text documents (CLOB), images, or video (BLOB).
SQL> CREATE TABLE high_pay_emp
AS SELECT *
FROM employee
WHERE salary > 50000 ;
Table created. employee table but does not copy any data into it. - Primary key and Unique key constraints.
- Foreign key constraints (for referential integrity).
- Check constraints.
dno column in the employee table references the dnumber column in the department table. If a department is deleted, all employees that reference the department are also deleted. This is given by the ON DELETE CASCADE option: dnumber column in the department table in the above example) must be either the primary key or a unique key for the table. Thus, the child (or detail) table must be created first before the parent (or master) table is created using the above constraints. ON DELETE SET DEFAULT and ON DELETE SET NULL. In addition, the behavior of child tables when a parent table is updated can also be specified using an ON UPDATE clause. - Primary key constraints: pk_
- Foreign key constraints: fk_
- Check constraints: ck_
emp_department_1 and then modify the data type of the fname column. emp_department_1 created previously, is dropped from the database.
button, Programs, Oracle for Windows 95 and then SQL*Plus. The SQL*Plus login screen will appear after roughly 15 seconds.
. This will launch your local Web Browser (Netscape Navigator/Communicator or Microsoft Internet Explorer) and the Welcome to the Oracle8 Documentation Library! screen will be displayed. From here, click on the "TEXT VERSION" link to get to the Oracle Product Documentation Library

1 comments: