如何快速创建具有约束和索引的表
Use the following approach to create tables with constraints and indexes:
- Create the tables with the constraints.
NOTNULLconstraints can be unnamed and should be created enabled and validated. All other constraints (CHECK,UNIQUE,PRIMARYKEY, andFOREIGNKEY) should be named and created disabled.
Note:By default, constraints are created in the
ENABLEDstate. - Load old data into the tables.
- Create all indexes, including indexes needed for constraints.
- Enable novalidate all constraints. Do this to primary keys before foreign keys.
- Allow users to query and modify data.
- With a separate
ALTERTABLEstatement for each constraint, validate all constraints. Do this to primary keys before foreign keys. For example,CREATE TABLE t (a NUMBER CONSTRAINT apk PRIMARY KEY DISABLE,
-
b NUMBER NOT NULL);
-
CREATE TABLE x (c NUMBER CONSTRAINT afk REFERENCES t DISABLE);
Now you can use Import or Fast Loader to load data into table
t.CREATE UNIQUE INDEX tai ON t (a);
-
CREATE INDEX tci ON x (c);
-
ALTER TABLE t MODIFY CONSTRAINT apk ENABLE NOVALIDATE;
-
ALTER TABLE x MODIFY CONSTRAINT afk ENABLE NOVALIDATE;
At this point, users can start performing
INSERTs,UPDATEs,DELETEs, andSELECTs on tablet.ALTER TABLE t ENABLE CONSTRAINT apk;
-
ALTER TABLE x ENABLE CONSTRAINT afk;
Now the constraints are enabled and validated.
