Use TPCH;
Go
-- (1) 创建表时定义参照完整性
-- 先定义地区表的实体完整性，再定义国家表上的参照完整性。
CREATE TABLE Sales_Exp3.region (
                                   regionkey INT PRIMARY KEY,
                                   name CHAR(25),
                                   comment VARCHAR(152)
);
CREATE TABLE Sales_Exp3.nation (
                                   nationkey INT PRIMARY KEY,
                                   name CHAR(25),
                                   regionkey INT REFERENCES Sales_Exp3.region(regionkey), /* 列级参照完整性 */
                                   comment VARCHAR(152)
);
-- OR
DROP TABLE Sales_Exp3.nation;
CREATE TABLE Sales_Exp3.nation (
                                   nationkey INT PRIMARY KEY,
                                   name CHAR(25),
                                   regionkey INT,
                                   comment VARCHAR(152),
                                   CONSTRAINT FK_Nation_Region FOREIGN KEY (regionkey) REFERENCES Sales_Exp3.region(regionkey) /* 表级参照完整性 */
);
Go
DROP TABLE Sales_Exp3.nation;
Go
-- (2) 创建表后定义参照完整性
-- 定义国家表的参照完整性。
CREATE TABLE Sales_Exp3.nation (
                                   nationkey INT PRIMARY KEY,
                                   name CHAR(25),
                                   regionkey INT,
                                   comment VARCHAR(152)
);
ALTER TABLE Sales_Exp3.nation
    ADD CONSTRAINT FK_Nation_Region FOREIGN KEY (regionkey) REFERENCES Sales_Exp3.region(regionkey);
Go
-- (3) 定义参照完整性（外码由多个属性组成）
-- 定义订单项目表的参照完整性。
CREATE TABLE Sales_Exp3.Supplier(suppkey INT PRIMARY KEY, name CHAR(25));
CREATE TABLE Sales_Exp3.Part(partkey INT PRIMARY KEY, name VARCHAR(55));
CREATE TABLE Sales_Exp3.Orders(orderkey INT PRIMARY KEY, orderdate DATE);
Go
CREATE TABLE Sales_Exp3.PartSupp (
                                     partkey INT,
                                     suppkey INT,
                                     availqty INT,
                                     supplycost REAL,
                                     comment VARCHAR(199),
                                     PRIMARY KEY (partkey, suppkey)
);
CREATE TABLE Sales_Exp3.Lineitem (
                                     orderkey INT REFERENCES Sales_Exp3.Orders(orderkey),
                                     partkey INT REFERENCES Sales_Exp3.Part(partkey),
                                     suppkey INT REFERENCES Sales_Exp3.Supplier(suppkey),
                                     linenumber INT,
                                     quantity REAL,
                                     extendedprice REAL,
                                     discount REAL,
                                     tax REAL,
                                     returnflag CHAR(1),
                                     linestatus CHAR(1),
                                     shipdate DATE,
                                     commitdate DATE,
                                     receiptdate DATE,
                                     shipinstruct CHAR(25),
                                     shipmode CHAR(10),
                                     comment VARCHAR(44),
                                     PRIMARY KEY (orderkey, linenumber),
                                     FOREIGN KEY (partkey, suppkey) REFERENCES Sales_Exp3.PartSupp(partkey, suppkey)
);
Go
DROP TABLE Sales_Exp3.nation;
Go
-- (4) 定义参照完整性的违约处理
-- 定义国家表的参照完整性，当删除或修改被参照表记录时，设置参照表中相应记录的值为空值。
CREATE TABLE Sales_Exp3.nation (
                                   nationkey INT PRIMARY KEY,
                                   name CHAR(25),
                                   regionkey INT NULL,
                                   comment VARCHAR(152),
                                   CONSTRAINT FK_Nation_regionkey
                                       FOREIGN KEY (regionkey) REFERENCES Sales_Exp3.region(regionkey)
                                           ON DELETE SET NULL
                                           ON UPDATE SET NULL
);
Go
-- (5) 删除参照完整性
-- 删除国家表的外码
ALTER TABLE Sales_Exp3.nation
    DROP CONSTRAINT FK_Nation_regionkey;
Go
-- (6) 插入一条国家记录，验证参照完整性是否起作用
/* 插入一条国家记录，如果'1001'号地区记录不存在，违反参照完整性约束。 */
ALTER TABLE Sales_Exp3.nation
    ADD CONSTRAINT PK_Nation_regionkey
        FOREIGN KEY (regionkey) REFERENCES Sales_Exp3.region(regionkey)
            ON DELETE SET NULL
            ON UPDATE SET NULL;
Go
INSERT INTO Sales_Exp3.nation (nationkey, name, regionkey, comment)
VALUES (1001, 'nation1', 1001, 'comment1');
Go
/* [23000][547] 行 1: INSERT 语句与 FOREIGN KEY 约束"PK_Nation_regionkey"冲突。该冲突发生于数据库"TPCH"，表"Sales_Exp3.region", column 'regionkey'。 */