db2创建删除外键
程序员文章站
2022-05-08 14:20:12
...
db2 9.7
db2可以在创建表时创建外键,也可以建好表后创建外键,不用担心重建表
创建外键命令
ALTER TABLE "SCHEMANAME"."TABLENAME" ADD CONSTRAINT "FK_TABLENAME_COLUMNNAME" FOREIGN KEY("COLUMNNAME") REFERENCES "SCHEMANAME"."PARENTTABLE"("COLUMNNAME") ON DELETE CASCADE --ON UPDATE NO ACTION --ENFORCED/NOT ENFORCED --ENABLE QUERY OPTIMIZATION/DISABLE QUERY OPTIMIZATION
从ON DELETE开始都是可选的
ON DELETE ACTION类型
NO ACTION(默认)
RESTRICT
CASCADE
SET NULL
ON UPDATE ACTION类型
NO ACTION(默认)
RESTRICT
ENFORCED 启用外键(默认)
ENABLE QUERY OPTIMIZATION 启用查询优化(默认)
删除外键命令
ALTER TABLE TABLENAME DROP CONSTRAINT FK_TABLENAME_COLUMNNAME
推荐阅读