CREATE TABLE語法
CREATE [[ GLOBAL | LOCAL ]{ TEMPORARY | TEMP }] TABLE table_name
(
{ column_name data_type [ DEFAULT default_expr ][ column_constraint [...]]
| table_constraint
| LIKE parent_table [{ INCLUDING | EXCLUDING } DEFAULTS ]
}[,...]
)
[ INHERITS ( parent_table [,...])]
[ WITH OIDS | WITHOUT OIDS ]
[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP }]
[ TABLESPACE tablespace ]
緊跟在字段定義後
column_constraint |
和字段定義並列寫在表定義中(約束子句)
table_constraint |
|
|
[CONSTRAINT constraint_name] CHECK (expression) |
[CONSTRAINT constraint_name] CHECK ( expression ) |
表約 束 |
檢查 |
NOT NULL | NULL |
|
字段 約束 (修 飾詞) |
非空 |
[CONSTRAINT constraint_name] CHECK (colunmn is not null) |
[CONSTRAINT constraint_name] CHECK (colunmn is not null) |
表約 束 |
非空 |
UNIQUE [USING INDEX TABLESPACE tablespace] |
[CONSTRAINT constraint_name] UNIQUE (column_name [,...]) [USING INDEX TABLESPACE tablespace] |
表索 引約 束 |
惟一 |
[CONSTRAINT constraint_name] REFERENCES ref_table [( ref_column )] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE action ][ ON UPDATE action ] |
[CONSTRAINT constraint_name] FOREIGN KEY ( column_name [,...]) REFERENCES ref_table [( ref_column [,...])] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE action ][ ON UPDATE action ] |
表外 鍵 |
外鍵 |
ADD [ COLUMN ] column type [ column_constraint [...]] ALTER [ COLUMN ] column SET DEFAULT expression ALTER [ COLUMN ] column DROP DEFAULT ALTER [ COLUMN ] column { SET | DROP } NOT NULL |
ADD table_constraint DROP CONSTRAINT constraint_name [ RESTRICT | CASCADE ] CLUSTER ON index_name |
相關 DDL |
|
非空約束not null有兩種方式實現,一種是修飾詞「非空」,如
creata table a(a int not null);
alter table a alter a drop not null;
一種是check,而check又分爲字段約束和表約束,如
create table a(a int check(a is not null));
alter table a drop constraint a_a_check;
默認值default 也是經過alter語句的set和drop來進行修改的。
惟一約束unique 能夠經過DROP CONSTRAINT 約束名稱來刪除。
在使用alter table 語句添加新字段的時候只能使用column_constraint,只有單純添加約束才能使用table_constraint,兩種狀況下unique約束就存在區別。