This topic describes how to create a table.
Syntax
CREATE [ GLOBAL TEMPORARY ] TABLE table_name (
{ column_name data_type [ DEFAULT default_expr ]
[ column_constraint [ ... ] ] table_constraint } [, ...]
)
[ ON COMMIT { PRESERVE ROWS DELETE ROWS } ]
[ TABLESPACE tablespace ]
In the preceding syntax, column_constraint can be set to one of the following constraints:
[ CONSTRAINT constraint_name ]
{ NOT NULL
NULL
UNIQUE [ USING INDEX TABLESPACE tablespace ]
PRIMARY KEY [ USING INDEX TABLESPACE tablespace ]
CHECK (expression)
REFERENCES reftable [ ( refcolumn ) ]
[ ON DELETE action ] }
[ DEFERRABLE NOT DEFERRABLE ] [ INITIALLY DEFERRED
INITIALLY IMMEDIATE ]
table_constraint can be set to one of the following constraints: [ CONSTRAINT constraint_name ]
{ UNIQUE ( column_name [, ...] )
[ USING INDEX TABLESPACE tablespace ]
PRIMARY KEY ( column_name [, ...] )
[ USING INDEX TABLESPACE tablespace ]
CHECK ( expression )
FOREIGN KEY ( column_name [, ...] )
REFERENCES reftable [ ( refcolumn [, ...] ) ]
[ ON DELETE action ] }
[ DEFERRABLE NOT DEFERRABLE ]
[ INITIALLY DEFERRED INITIALLY IMMEDIATE ]
Parameters
Parameter | Description |
---|---|
GLOBAL TEMPORARY | When this parameter is specified, the resulting table is a temporary table. Temporary
tables are automatically deleted after the current session ends. You can also manually
delete temporary tables after you complete transactions. For more information, see
the description about the ON COMMIT parameter in this table.
Note
|
table name | The name of the table.
Note To reference this table, you can use the schema-qualified name.
|
column name | The name of a column that you want to create in the table. |
data type | The data type of the column. The value can contain array specifiers. |
DEFAULT default_expr | The expression that is used to generate a default value for the defined column. This
expression is specified in the DEFAULT clause. This expression cannot contain subqueries or references to other columns
in the table. The data type of this default expression must be the same as that of
the column.
When no values are inserted for a column, this default expression is used to generate a value. If you do not specify the default expression, the value used is NULL. |
CONSTRAINT constraint_name | The name of the column or table constraint that you want to configure. The constraint_name parameter is optional. If you leave this parameter empty, the system automatically generates a name for the column or table constraint. |
NOT NULL | Specifies that the column cannot contain NULL values. |
PRIMARY KEY - column constraint |
Specifies that one or more columns must contain values that uniquely identify each row in the table and cannot contain NULL values. The PRIMARY KEY constraint is a combination of the UNIQUE and NOT NULL constraints. However, if the primary key consists of a set of columns, you must provide the metadata of the schema. This is because the primary key implies that other tables can rely on this set of columns as a unique identifier for rows. Note
|
PRIMARY KEY ( column_name [, ...] ) - table constraint | |
CHECK (expression) |
The expression that is used to produce a Boolean result. This expression is specified
in the An INSERT or UPDATE operation can succeed only when this expression evaluates to true or unknown. Otherwise, the operation fails and an error is returned. If you define a CHECK constraint on a column, you can limit the values that can be written to the column. If you define a CHECK constraint on a table, you can limit the values that can be written to multiple columns in the table. Note
This expression cannot contain subqueries or references to variables other than the
columns of the current row.
|
REFERENCES reftable [ ( refcolumn ) ] [ ON DELETE action ] - column constraint |
These clauses specify a foreign key constraint. The foreign key constraint requires
that the values in the columns of the created table must exist in the columns of the
referenced table. If the If the data in the referenced columns is changed, the same change operation is performed
on the columns of this table. The
ON DELETE clause specifies the action to perform when a referenced row in the referenced table
is being deleted. Referential actions cannot be deferred, even if the constraint is
deferrable. The ON DELETE clause supports the following referential actions:
Note If the values of the referenced columns change frequently, we recommend that you add
an index to the foreign key column. This way, the referential actions associated with
the foreign key column can be performed in an efficient manner.
|
FOREIGN KEY ( column [, ...] ) REFERENCES reftable [ ( refcolumn [, ...] ) ] [ ON DELETE action ] - table constraint | |
DEFERRABLE NOT | Specifies whether the constraint is deferrable. A non-deferrable constraint is checked
immediately after each statement is executed. You can use the SET CONSTRAINTS statement to postpone the check of deferrable constraints. When you postpone the
check of a constraint, the constraint is not checked until the end of the transaction.
|
DEFERRABLE | |
INITIALLY IMMEDIATE | The time when the specified deferrable constraint is checked.
The INITIALLY IMMEDIATE value specifies that the constraint is checked after each statement is executed. INITIALLY IMMEDIATE is the default value. The INITIALLY DEFERRED value specifies that the constraint is checked after the transaction ends. You can execute theSET CONSTRAINTS statement to change the time when the constraint is checked.
|
INITIALLY DEFERRED | |
ON COMMIT | The action that is performed on the temporary table at the end of a transaction block. Default value: PRESERVE ROWS. Valid values:
|
Description
The CREATE TABLE
statement creates a table in the current database. The table is initially empty.
The user who executes the statement is the table owner.
If you specify a schema name in the statement, the table is created in the specified
schema. Otherwise, the table is created in the current schema. For example, if you
execute the CREATE TABLE myschema.mytable
statement, the mytable table is created in the myschema schema. If the table that
you want to create is a temporary table, you do not need to specify a schema. Temporary
tables exist in a dedicated schema. The table name must be different from the names
of other tables, sequences, indexes, and views in the same schema.
The CREATE TABLE
statement also automatically creates a data type. The data type represents the composite
type that corresponds to a row of the table. The table name cannot be the same as
the name of an existing data type in the same schema.
A table can contain up to 1,600 columns. In practice, a table may contain fewer than 1,600 columns due to constraints on the length of tuples.
- A column constraint is defined on a single column.
- A table constraint is defined on multiple columns in a table.
Examples
Create two tables: dept and emp.
CREATE TABLE dept (
deptno NUMBER(2) NOT NULL CONSTRAINT dept_pk PRIMARY KEY,
dname VARCHAR2(14),
loc VARCHAR2(13)
);
CREATE TABLE emp (
empno NUMBER(4) NOT NULL CONSTRAINT emp_pk PRIMARY KEY,
ename VARCHAR2(10),
job VARCHAR2(9),
mgr NUMBER(4),
hiredate DATE,
sal NUMBER(7,2),
comm NUMBER(7,2),
deptno NUMBER(2) CONSTRAINT emp_ref_dept_fk
REFERENCES dept(deptno)
);
Define a unique table constraint for the dept table. You can define unique table constraints for multiple columns in the table.
CREATE TABLE dept (
deptno NUMBER(2) NOT NULL CONSTRAINT dept_pk PRIMARY KEY,
dname VARCHAR2(14) CONSTRAINT dept_dname_uq UNIQUE,
loc VARCHAR2(13)
);
Define a column-level CHECK constraint:
CREATE TABLE emp (
empno NUMBER(4) NOT NULL CONSTRAINT emp_pk PRIMARY KEY,
ename VARCHAR2(10),
job VARCHAR2(9),
mgr NUMBER(4),
hiredate DATE,
sal NUMBER(7,2) CONSTRAINT emp_sal_ck CHECK (sal > 0),
comm NUMBER(7,2),
deptno NUMBER(2) CONSTRAINT emp_ref_dept_fk
REFERENCES dept(deptno)
);
Define a table-level CHECK constraint:
CREATE TABLE emp (
empno NUMBER(4) NOT NULL CONSTRAINT emp_pk PRIMARY KEY,
ename VARCHAR2(10),
job VARCHAR2(9),
mgr NUMBER(4),
hiredate DATE,
sal NUMBER(7,2),
comm NUMBER(7,2),
deptno NUMBER(2) CONSTRAINT emp_ref_dept_fk
REFERENCES dept(deptno),
CONSTRAINT new_emp_ck CHECK (ename IS NOT NULL AND empno > 7000)
);
Define a table-level PRIMARY KEY constraint for the jobhist table. A table-level PRIMARY KEY constraint can be defined on one or more columns of the table.
CREATE TABLE jobhist (
empno NUMBER(4) NOT NULL,
startdate DATE NOT NULL,
enddate DATE,
job VARCHAR2(9),
sal NUMBER(7,2),
comm NUMBER(7,2),
deptno NUMBER(2),
chgdesc VARCHAR2(80),
CONSTRAINT jobhist_pk PRIMARY KEY (empno, startdate)
);
Configure a literal constant as the default value for the job column and set the default value of the hiredate column to the date when the row is inserted.
CREATE TABLE emp (
empno NUMBER(4) NOT NULL CONSTRAINT emp_pk PRIMARY KEY,
ename VARCHAR2(10),
job VARCHAR2(9) DEFAULT 'SALESMAN',
mgr NUMBER(4),
hiredate DATE DEFAULT SYSDATE,
sal NUMBER(7,2),
comm NUMBER(7,2),
deptno NUMBER(2) CONSTRAINT emp_ref_dept_fk
REFERENCES dept(deptno)
);
Create a table named dept in the diskvol1 tablespace:
CREATE TABLE dept (
deptno NUMBER(2) NOT NULL CONSTRAINT dept_pk PRIMARY KEY,
dname VARCHAR2(14),
loc VARCHAR2(13)
) TABLESPACE diskvol1;