Initial commit

This commit is contained in:
2025-11-06 09:53:12 +08:00
commit ea8d38c146
50 changed files with 1200 additions and 0 deletions

79
Experiment3/Exp3-1.sql Normal file
View File

@@ -0,0 +1,79 @@
Use TPCH;
Go
-- (1) 创建表时定义实体完整性(列级实体完整性)
-- 定义供应商表的实体完整性。
CREATE TABLE Sales_Exp3.Supplier(
suppkey INT CONSTRAINT PK_Supplier PRIMARY KEY,
name CHAR(25),
address VARCHAR(40),
nationkey INT,
phone CHAR(15),
acctbal REAL,
comment VARCHAR(101)
);
Go
DROP TABLE Sales_Exp3.Supplier;
Go
-- (2) 创建表时定义实体完整性(表级实体完整性)
-- 定义供应商表的实体完整性。
CREATE TABLE Sales_Exp3.Supplier (
suppkey INT,
name CHAR(25),
address VARCHAR(40),
nationkey INT,
phone CHAR(15),
acctbal REAL,
comment VARCHAR(101),
CONSTRAINT PK_Supplier PRIMARY KEY (suppkey)
);
Go
DROP TABLE Sales_Exp3.Supplier;
Go
-- (3) 创建表后定义实体完整性
-- 定义供应商表。
CREATE TABLE Sales_Exp3.Supplier (
suppkey INT NOT NULL,
name CHAR(25),
address VARCHAR(40),
nationkey INT,
phone CHAR(15),
acctbal REAL,
comment VARCHAR(101)
);
ALTER TABLE Sales_Exp3.Supplier /* 再修改供应商表,增加实体完整性 */
ADD CONSTRAINT PK_Supplier PRIMARY KEY (suppkey);
Go
-- (4) 定义实体完整性(主码由多个属性组成)
-- 定义供应关系表的实体完整性。
CREATE TABLE Sales_Exp3.PartSupp (
partkey INT,
suppkey INT,
availqty INT,
supplycost REAL,
comment VARCHAR(199),
PRIMARY KEY (partkey, suppkey)
);
/* 主码由多个属性组成,实体完整性必须定义在表级 */
Go
-- (5) 有多个候选码时定义实体完整性
-- 定义国家表的实体完整性其中nationkey和name都是候选码选择nationkey作为主码name上定义唯一性约束。
CREATE TABLE Sales_Exp3.nation (
nationkey INT CONSTRAINT PK_Nation PRIMARY KEY,
name CHAR(25) UNIQUE,
regionkey INT,
comment VARCHAR(152)
);
Go
-- (6) 删除实体完整性
-- 删除国家实体的主码。
ALTER TABLE Sales_Exp3.nation
DROP CONSTRAINT PK_Nation;
Go
-- (7) 增加两条相同记录,验证实体完整性是否起作用
/* 插入两条主码相同的记录就会违反实体完整性约束 */
INSERT INTO Sales_Exp3.Supplier (suppkey, name, address, nationkey, phone, acctbal, comment)
VALUES (11, 'test1', 'test1_addr', 101, '12345678', 0.0, 'test1_comment');
INSERT INTO Sales_Exp3.Supplier (suppkey, name, address, nationkey, phone, acctbal, comment)
VALUES (11, 'test2', 'test2_addr', 102, '23456789', 0.0, 'test2_comment');
Go
/* [23000][2627] 行 5: 违反了 PRIMARY KEY 约束“PK_Supplier”。不能在对象“Sales_Exp3.Supplier”中插入重复键。重复键值为 (11)。 */