Creating tables in a referential structure
In a referential structure, you can create table spaces in any order. Using a model for the structure can be helpful.
You can create table spaces in any order. However, you need to create the table spaces before you perform the following steps. (This procedure uses the DEPT and EMP tables.)
Examples
- Create the DEPT table and define its primary key on the DEPTNO column. The PRIMARY KEY clause of the CREATE TABLE statement defines the primary key.
CREATE TABLE DEPT ⋮ PRIMARY KEY (DEPTNO);
- Create the EMP table and define its primary key as EMPNO and its foreign key as DEPT. The FOREIGN KEY clause of the CREATE TABLE statement defines the foreign key.
CREATE TABLE EMP ⋮ PRIMARY KEY (EMPNO) FOREIGN KEY (DEPT) REFERENCES DEPT (DEPTNO) ON DELETE SET NULL;
- Alter the DEPT table to add the definition of its foreign key, MGRNO.
ALTER TABLE DEPT FOREIGN KEY (MGRNO) REFERENCES EMP (EMPNO) ON DELETE RESTRICT;