约束
约束子句用于声明约束,新行或者更新的行必须满足这些约束才能成功插入或更新。如果存在违反约束的数据行为,行为会被约束终止。
约束可以在创建表时规定(通过 CREATE TABLE 语句),或者在表创建之后规定(通过 ALTER TABLE 语句)。
约束可以是列级或表级。列级约束仅适用于列,表级约束被应用到整个表。
openGauss中常用的约束如下:
- NOT NULL:指示某列不能存储NULL值。
- UNIQUE:确保某列的值都是唯一的。
- PRIMARY KEY:NOT NULL 和 UNIQUE 的结合。确保某列(或两个列多个列的结合)有唯一标识,有助于更容易更快速地找到表中的一个特定的记录。
- FOREIGN KEY: 保证一个表中的数据匹配另一个表中的值的参照完整性。
- CHECK: 保证列中的值符合指定的条件。
NOT NULL约束
创建表时,如果不指定约束,默认值为NULL,即允许列插入空值。如果您不想某列存在NULL值,那么需要在该列上定义NOT NULL约束,指定在该列上的值不允许存在NULL值。插入数据时,如果该列存在NULL值,则会报错,插入失败。
NULL与没有数据是不一样的,它代表着未知的数据。
例如,创建表staff,共有5个字段,其中NAME,ID设置不接受空值。
1. openGauss=# CREATE TABLE staff(
2. ID INT NOT NULL,
3. NAME char(8) NOT NULL,
4. AGE INT ,
5. ADDRESS CHAR(50),
6. SALARY REAL
7. );
给表staff插入数据。当ID字段插入空值时,数据库返回报错。
1. openGauss=# INSERT INTO staff VALUES (1,'lily',28);
2. INSERT 0 1
3. openGauss=# INSERT INTO staff (NAME,AGE) VALUES ('JUCE',28);
4. ERROR: null value in column "id" violates not-null constraint
5. DETAIL: Failing row contains (null, JUCE , 28, null, null).
UNIQUE约束
UNIQUE约束表示表里的一个字段或多个字段的组合必须在全表范围内唯一。
对于唯一约束,NULL被认为是互不相等的。
例如,创建表staff1,表包含5个字段,其中AGE设置为UNIQUE,因此不能添加两条有相同年龄的记录。
1. openGauss=# CREATE TABLE staff1(
2. ID INT NOT NULL,
3. NAME char(8) NOT NULL,
4. AGE INT NOT NULL UNIQUE ,
5. ADDRESS CHAR(50),
6. SALARY REAL
7. );
给表staff1表插入数据。当字段AGE插入两条一样的数据时,数据库返回报错。
1. openGauss=# INSERT INTO staff1 VALUES (1,'lily',28);
2. INSERT 0 1
3. openGauss=# INSERT INTO staff1 VALUES (2, 'JUCE',28);
4. ERROR: duplicate key value violates unique constraint "staff1_age_key"
5. DETAIL: Key (age)=(28) already exists.
PRIMARY KEY
PRIMARY KEY为主键,是数据表中每一条记录的唯一标识。主键约束声明表中的一个或者多个字段只能包含唯一的非NULL值。
主键是非空约束和唯一约束的组合。一个表只能声明一个主键。
例如,创建表staff2,其中ID为主键。
1. openGauss=# CREATE TABLE staff2(
2. ID INT PRIMARY KEY ,
3. NAME TEXT NOT NULL,
4. AGE INT NOT NULL,
5. ADDRESS CHAR(50),
6. SALARY REAL
7. );
8. NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "staff2_pkey" for table "staff2"
9. CREATE TABLE
FOREIGN KEY
FOREIGN Key即外键约束,指定列(或一组列)中的值必须匹配另一个表的某一行中出现的值。通常一个表中的FOREIGN KEY指向另一个表中的 UNIQUE KEY(唯一约束的键),即维护了两个相关表之间的引用完整性。
例如,创建表staff3,包含5个字段。
1. openGauss=# CREATE TABLE staff3(
2. ID INT PRIMARY KEY NOT NULL,
3. NAME TEXT NOT NULL,
4. AGE INT NOT NULL,
5. ADDRESS CHAR(50),
6. SALARY REAL
7. );
创建一张DEPARTMENT表,并添加3个字段,其中EMP_ID为外键,参照staff3的ID字段:
1. openGauss=# CREATE TABLE DEPARTMENT(
2. ID INT PRIMARY KEY NOT NULL,
3. DEPT CHAR(50) NOT NULL,
4. EMP_ID INT references staff3(ID)
5. );
FOREIGN Key在MySQL兼容性下,外键可以关联非唯一性索引。即一个表中的FOREIGN Key指向另一个表中的 Non-unique KEY(非唯一约束的键)。 注: 如果在MySQL兼容性下,定义外键指定ON UPDATE | DELETE CASCADE时,在非唯一性索引中,非唯一索引字段存在多个元组时,只要dml其中一行数据,则会触发外键表里关联的字段全部修改。但如果字段为NULL时,则不触发外键关联的字段做对应的修改。
MySQL兼容性需要安装dolphin插件才可生效。
1. openGauss=# create table t1(id int, name varchar);
2. CREATE TABLE
3. openGauss=# create table t2(id int, a_id int);
4. CREATE TABLE
5. -- create non-unique index on table t1.
6. openGauss=# create index a_index_1 on t1(id);
7. CREATE INDEX
8. -- create foreign key on non-unique index
9. openGauss=# alter table t2 add constraint t2_fk foreign key (a_id) references t1(id);
10. ALTER TABLE
11. openGauss=# \d t1
12. Table "public.t1"
13. Column | Type | Modifiers
14. --------+-------------------+-----------
15. id | integer |
16. name | character varying |
17. Indexes:
18. "a_index_1" btree (id) TABLESPACE pg_default
19. Referenced by:
20. TABLE "t2" CONSTRAINT "t2_fk" FOREIGN KEY (a_id) REFERENCES t1(id)
22. openGauss=# \d t2
23. Table "public.t2"
24. Column | Type | Modifiers
25. --------+---------+-----------
26. id | integer |
27. a_id | integer |
28. Foreign-key constraints:
29. "t2_fk" FOREIGN KEY (a_id) REFERENCES t1(id)
31. openGauss=# insert into t1 values(1,'a'),(2,'b');
32. INSERT 0 2
33. openGauss=# select * from t1;
34. id | name
35. ----+------
36. 1 | a
37. 2 | b
38. (2 rows)
40. openGauss=# insert into t2 values(1,1);
41. INSERT 0 1
42. openGauss=# select * from t2;
43. id | a_id
44. ----+------
45. 1 | 1
46. (1 row)
48. openGauss=# insert into t2 values(1,3);
49. INSERT 0 1
50. ERROR: insert or update on table "t2" violates foreign key constraint "t2_fk"
51. DETAIL: Key (a_id)=(3) is not present in table "t1".
52. openGauss=# select * from t2;
53. id | a_id
54. ----+------
55. 1 | 1
56. (1 row)
58. openGauss=# alter table t2 drop constraint t2_fk;
59. ALTER TABLE
60. openGauss=# alter table t2 add constraint t2_fk foreign key (a_id) references t1(id) on update cascade;
61. ALTER TABLE
62. openGauss=# select * from t1;
63. id | name
64. ----+------
65. 1 | a
66. 2 | b
67. (2 rows)
69. openGauss=# insert into t1 values(1,'s');
70. INSERT 0 1
71. openGauss=# select * from t1;
72. id | name
73. ----+------
74. 1 | a
75. 2 | b
76. 1 | s
77. (3 rows)
79. openGauss=# insert into t2 values(2,1);
80. INSERT 0 1
81. openGauss=# select * from t2;
82. id | a_id
83. ----+------
84. 1 | 1
85. 2 | 1
86. (2 rows)
88. openGauss=# update t1 set id = 11 where name = 'a';
89. UPDATE 1
90. openGauss=# select * from t1;
91. id | name
92. ----+------
93. 2 | b
94. 1 | s
95. 11 | a
96. (3 rows)
98. openGauss=# select * from t2;
99. id | a_id
100. ----+------
101. 1 | 11
102. 2 | 11
103. (2 rows)
105. openGauss=# update t1 set id =1 where name = 'a';
106. UPDATE 1
107. openGauss=# alter table t2 drop constraint t2_fk;
108. ALTER TABLE
109. openGauss=# alter table t2 add constraint t2_fk foreign key (a_id) references t1(id) on delete cascade;
110. ALTER TABLE
111. openGauss=# select * from t1;
112. id | name
113. ----+------
114. 2 | b
115. 1 | s
116. 1 | a
117. (3 rows)
119. openGauss=# select * from t2;
120. id | a_id
121. ----+------
122. 1 | 1
123. 2 | 1
124. (2 rows)
126. openGauss=# delete from t1 where name = 's';
127. DELETE 1
128. openGauss=# select * from t1;
129. id | name
130. ----+------
131. 2 | b
132. 1 | a
133. (2 rows)
135. openGauss=# select * from t2;
136. id | a_id
137. ----+------
138. (0 rows)
CHECK约束
CHECK约束声明一个布尔表达式,每次要插入的新行或者要更新的行的新值必须使表达式结果为真或未知才能成功,否则会抛出一个异常并且不会修改数据库。
声明为字段约束的检查约束应该只引用该字段的数值,而在表约束里出现的表达式可以引用多个字段。expression表达式中,如果存在“<>NULL”或“!=NULL”,这种写法是无效的,需要写成“is NOT NULL”。
例如,创建表staff4,对字段SALARY新增CHECK约束,确保插入此列数值大于0。
1. openGauss=# CREATE TABLE staff4(
2. ID INT PRIMARY KEY NOT NULL,
3. NAME TEXT NOT NULL,
4. AGE INT NOT NULL,
5. ADDRESS CHAR(50),
6. SALARY REAL CHECK(SALARY > 0)
7. );
8. NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "staff4_pkey" for table "staff4"
9. CREATE TABLE
给表staff4插入数据。当字段SALARY插入数据不大于0时,数据库返回报错。
1. openGauss=# INSERT INTO staff4(ID,NAME,AGE,SALARY) VALUES (2, 'JUCE',16,0);
2. ERROR: new row for relation "staff4" violates check constraint "staff4_salary_check"
3. DETAIL: N/A
