Using DDL Statements
Using DDL Statements
One of the first steps in creating a database is to create the tables that will store an organization's
data.Database design involves identifying system user requirements for various organizational
systems such as order entry, inventory management, and accounts receivable. Regardless of
database size and complexity, each database is comprised of tables.
Cannot use the same name of another existing object in your schema
Following the above guidelines, 'EMP85' can be a valid table name.But 85EMP is not.Similarly,
UPDATE cannot be a chosen as a table name since it a SQL reserved keyword.
Here is the syntax of a basic CREATE TABLE statement.There may be many additional clauses to
explicitly provide the storage specifications or segment values.
Constraints are the rules defined optionally at the column level or table level coveredlaterinthischapter
.These rules are checked during any data action Insert, update on the table and raise error to abort
the action upon its violation.
For example, the CREATE TABLE statement below creates a table EMP_TEST. Note the column
specifications, data type and precision.
A user can refer the tables from other user's schema by prefixing the username or schema with
the table name.For example, a user GUEST wishes to query the employee name and salary from
the EMP_TEST table which is owned by SCOTT. He can issue the below query -
A column can hold a default value during the time of table creation.It helps to restrict the NULL
values getting into the column. Default value can be deduced from either a literal, expression or
SQL function which must return a compatible data type to the column. In the below CREATE TABLE
statement, note that the LOCATION_ID column has default value 100.
The below CTAS script creates a new table EMP_BACKUP. Employee data of department 20 gets
copied into the new table.
Data types
Data types are used to specify the basic behavior of a column in the table.On a broader
basis,column behavior can either belong to number,character or a date family.There are multiple
other subtypes which belong to these families.
FLOAT [p],where p is the binary precision that can range from 1 to 126. If p is not specified the
default value is binary 126.
Constraints
Constraints are the set of rules defined in Oracle tables to ensure data integrity.These rules are
enforced placed for each column or set of columns.Whenever the table participates in data action,
these rules are validated and raise exception upon violation. The available constraint types are
NOT NULL, Primary Key, Unique, Check, and Foreign Key.
The below syntax can be used to impose constraint at the column level.
Syntax:
All constraints except NOT NULL, can also be defined at the table level. Composite constraints can
only be specified at the table level.
UNIQUE constraint
Sometimes it is necessary to enforce uniqueness for a column value that is not a primary key
column.The UNIQUE constraint can be used to enforce this rule and Oracle will reject any rows that
violate the unique constraint.Unique constraint ensures that the column values are distinct, without
any duplicates.
Syntax:
Column Level:
Note: Oracle internally creates unique index to prevent duplication in the column values.Indexes
would be discussed later in PL/SQL.
Primary Key
Each table must normally contain a column or set of columns that uniquely identifies rows of data
that are stored in the table.This column or set of columns is referred to as the primary key.Most
tables have a single column as the primary key.Primary key columns are restricted against NULLs
and duplicate values.
Points to be noted -
A table can have only one primary key.
Oracle internally creates unique index to prevent duplication in the column values.Indexes
would be discussed later in PL/SQL.
Syntax:
Column level:
Table level:
CONSTRAINT [constraint name] PRIMARY KEY [column (s)]
The following example shows how to use PRIMARY KEY constraint at column level.
The following example shows how to define composite primary key using PRIMARY KEY constraint
at the table level.
Foreign Key
When two tables share the parent child relationship based on specific column, the joining column
in the child table is known as Foreign Key.This property of corresponding column in the parent
table is known as Referential integrity.Foreign Key column values in the child table can either be
null or must be the existing values of the parent table.Please note that only primary key columns of
the referenced table are eligible to enforce referential integrity.
If a foreign key is defined on the column in child table then Oracle does not allow the parent row to
be deleted,if it contains any child rows.However,if ON DELETE CASCADE option is given at the time
of defining foreign key,Oracle deletes all child rows while parent row is being deleted.Similarly,ON
DELETE SET NULL indicates that when a row in the parent table is deleted, the foreign key values
are set to null.
Syntax:
Column Level:
COLUMN [data type] [CONSTRAINT] [constraint name] [REFERENCES] [table name (column
name)]
Table level:
CONSTRAINT [constraint name] [FOREIGN KEY (foreign key column name) REFERENCES]
[referenced table name (referenced column name)]
The following example shows how to use FOREIGN KEY constraint at column level.
Check constraint
Sometimes the data values stored in a specific column must fall within some acceptable range of
values.A CHECK constraint requires that the specified check condition is either true or unknown for
each row stored in the table.Check constraint allows to impose a conditional rule on a column,
which must be validated before data is inserted into the column. The condition must not contain a
sub query or pseudo column CURRVAL NEXTVAL, LEVEL, ROWNUM, or SYSDATE.
Oracle allows a single column to have more than one CHECK constraint. In fact, there is no
practical limit to the number of CHECK constraints that can be defined for a column.
Syntax:
Column level:
Table level:
The following example shows how to use CHECK constraint at column level.
The following example shows how to use CHECK constraint at table level.
The below ALTER TABLE statement renames the table EMP to EMP_NEW.
The below ALTER TABLE statement adds a new column TESTCOL to the EMP_NEW table
The below ALTER TABLE statement renames the column TESTCOL to TESTNEW.
The below ALTER TABLE statement drop the column TESTNEW from EMP_NEW table
The below ALTER TABLE statement adds primary key on the EMPLOYEE_ID column.
The below ALTER TABLE statement switches the table mode to read only.
Syntax:
Illustration
Table altered.
Syntax:
The below statement will drop the table and place it into the recyclebin.
The below statement will drop the table and flush it out from the recyclebin also.