3 -- Constraints can be specified with:
6 -- - PRIMARY KEY clauses
12 CREATE TABLE DEFAULT_TBL (i int DEFAULT 100,
13 x text DEFAULT 'vadim', f float8 DEFAULT 123.456);
14 INSERT INTO DEFAULT_TBL VALUES (1, 'thomas', 57.0613);
15 INSERT INTO DEFAULT_TBL VALUES (1, 'bruce');
16 INSERT INTO DEFAULT_TBL (i, f) VALUES (2, 987.654);
17 INSERT INTO DEFAULT_TBL (x) VALUES ('marc');
18 INSERT INTO DEFAULT_TBL VALUES (3, null, 1.0);
19 SELECT '' AS five, * FROM DEFAULT_TBL;
21 ------+-----+--------+---------
22 | 1 | thomas | 57.0613
25 | 100 | marc | 123.456
29 CREATE SEQUENCE DEFAULT_SEQ;
30 CREATE TABLE DEFAULTEXPR_TBL (i1 int DEFAULT 100 + (200-199) * 2,
31 i2 int DEFAULT nextval('default_seq'));
32 INSERT INTO DEFAULTEXPR_TBL VALUES (-1, -2);
33 INSERT INTO DEFAULTEXPR_TBL (i1) VALUES (-3);
34 INSERT INTO DEFAULTEXPR_TBL (i2) VALUES (-4);
35 INSERT INTO DEFAULTEXPR_TBL (i2) VALUES (NULL);
36 SELECT '' AS four, * FROM DEFAULTEXPR_TBL;
46 -- test for extraneous comma
47 CREATE TABLE error_tbl (i int DEFAULT (100, ));
48 ERROR: syntax error at or near ")"
49 LINE 1: CREATE TABLE error_tbl (i int DEFAULT (100, ));
51 -- this will fail because gram.y uses b_expr not a_expr for defaults,
52 -- to avoid a shift/reduce conflict that arises from NOT NULL being
53 -- part of the column definition syntax:
54 CREATE TABLE error_tbl (b1 bool DEFAULT 1 IN (1, 2));
55 ERROR: syntax error at or near "IN"
56 LINE 1: CREATE TABLE error_tbl (b1 bool DEFAULT 1 IN (1, 2));
58 -- this should work, however:
59 CREATE TABLE error_tbl (b1 bool DEFAULT (1 IN (1, 2)));
64 CREATE TABLE CHECK_TBL (x int,
65 CONSTRAINT CHECK_CON CHECK (x > 3));
66 INSERT INTO CHECK_TBL VALUES (5);
67 INSERT INTO CHECK_TBL VALUES (4);
68 INSERT INTO CHECK_TBL VALUES (3);
69 ERROR: new row for relation "check_tbl" violates check constraint "check_con"
70 INSERT INTO CHECK_TBL VALUES (2);
71 ERROR: new row for relation "check_tbl" violates check constraint "check_con"
72 INSERT INTO CHECK_TBL VALUES (6);
73 INSERT INTO CHECK_TBL VALUES (1);
74 ERROR: new row for relation "check_tbl" violates check constraint "check_con"
75 SELECT '' AS three, * FROM CHECK_TBL;
83 CREATE SEQUENCE CHECK_SEQ;
84 CREATE TABLE CHECK2_TBL (x int, y text, z int,
85 CONSTRAINT SEQUENCE_CON
86 CHECK (x > 3 and y <> 'check failed' and z < 8));
87 INSERT INTO CHECK2_TBL VALUES (4, 'check ok', -2);
88 INSERT INTO CHECK2_TBL VALUES (1, 'x check failed', -2);
89 ERROR: new row for relation "check2_tbl" violates check constraint "sequence_con"
90 INSERT INTO CHECK2_TBL VALUES (5, 'z check failed', 10);
91 ERROR: new row for relation "check2_tbl" violates check constraint "sequence_con"
92 INSERT INTO CHECK2_TBL VALUES (0, 'check failed', -2);
93 ERROR: new row for relation "check2_tbl" violates check constraint "sequence_con"
94 INSERT INTO CHECK2_TBL VALUES (6, 'check failed', 11);
95 ERROR: new row for relation "check2_tbl" violates check constraint "sequence_con"
96 INSERT INTO CHECK2_TBL VALUES (7, 'check ok', 7);
97 SELECT '' AS two, * from CHECK2_TBL;
99 -----+---+----------+----
105 -- Check constraints on INSERT
107 CREATE SEQUENCE INSERT_SEQ;
108 CREATE TABLE INSERT_TBL (x INT DEFAULT nextval('insert_seq'),
109 y TEXT DEFAULT '-NULL-',
110 z INT DEFAULT -1 * currval('insert_seq'),
111 CONSTRAINT INSERT_CON CHECK (x >= 3 AND y <> 'check failed' AND x < 8),
113 INSERT INTO INSERT_TBL(x,z) VALUES (2, -2);
114 ERROR: new row for relation "insert_tbl" violates check constraint "insert_con"
115 SELECT '' AS zero, * FROM INSERT_TBL;
120 SELECT 'one' AS one, nextval('insert_seq');
126 INSERT INTO INSERT_TBL(y) VALUES ('Y');
127 ERROR: new row for relation "insert_tbl" violates check constraint "insert_con"
128 INSERT INTO INSERT_TBL(y) VALUES ('Y');
129 INSERT INTO INSERT_TBL(x,z) VALUES (1, -2);
130 ERROR: new row for relation "insert_tbl" violates check constraint "insert_tbl_check"
131 INSERT INTO INSERT_TBL(z,x) VALUES (-7, 7);
132 INSERT INTO INSERT_TBL VALUES (5, 'check failed', -5);
133 ERROR: new row for relation "insert_tbl" violates check constraint "insert_con"
134 INSERT INTO INSERT_TBL VALUES (7, '!check failed', -7);
135 INSERT INTO INSERT_TBL(y) VALUES ('-!NULL-');
136 SELECT '' AS four, * FROM INSERT_TBL;
138 ------+---+---------------+----
141 | 7 | !check failed | -7
145 INSERT INTO INSERT_TBL(y,z) VALUES ('check failed', 4);
146 ERROR: new row for relation "insert_tbl" violates check constraint "insert_tbl_check"
147 INSERT INTO INSERT_TBL(x,y) VALUES (5, 'check failed');
148 ERROR: new row for relation "insert_tbl" violates check constraint "insert_con"
149 INSERT INTO INSERT_TBL(x,y) VALUES (5, '!check failed');
150 INSERT INTO INSERT_TBL(y) VALUES ('-!NULL-');
151 SELECT '' AS six, * FROM INSERT_TBL;
153 -----+---+---------------+----
156 | 7 | !check failed | -7
158 | 5 | !check failed | -5
162 SELECT 'seven' AS one, nextval('insert_seq');
168 INSERT INTO INSERT_TBL(y) VALUES ('Y');
169 ERROR: new row for relation "insert_tbl" violates check constraint "insert_con"
170 SELECT 'eight' AS one, currval('insert_seq');
176 -- According to SQL92, it is OK to insert a record that gives rise to NULL
177 -- constraint-condition results. Postgres used to reject this, but it
179 INSERT INTO INSERT_TBL VALUES (null, null, null);
180 SELECT '' AS nine, * FROM INSERT_TBL;
182 ------+---+---------------+----
185 | 7 | !check failed | -7
187 | 5 | !check failed | -5
193 -- Check inheritance of defaults and constraints
195 CREATE TABLE INSERT_CHILD (cx INT default 42,
196 cy INT CHECK (cy > x))
197 INHERITS (INSERT_TBL);
198 INSERT INTO INSERT_CHILD(x,z,cy) VALUES (7,-7,11);
199 INSERT INTO INSERT_CHILD(x,z,cy) VALUES (7,-7,6);
200 ERROR: new row for relation "insert_child" violates check constraint "insert_child_check"
201 INSERT INTO INSERT_CHILD(x,z,cy) VALUES (6,-7,7);
202 ERROR: new row for relation "insert_child" violates check constraint "insert_tbl_check"
203 INSERT INTO INSERT_CHILD(x,y,z,cy) VALUES (6,'check failed',-6,7);
204 ERROR: new row for relation "insert_child" violates check constraint "insert_con"
205 SELECT * FROM INSERT_CHILD;
207 ---+--------+----+----+----
208 7 | -NULL- | -7 | 42 | 11
211 DROP TABLE INSERT_CHILD;
213 -- Check constraints on INSERT INTO
215 DELETE FROM INSERT_TBL;
216 ALTER SEQUENCE INSERT_SEQ RESTART WITH 4;
217 CREATE TABLE tmp (xd INT, yd TEXT, zd INT);
218 INSERT INTO tmp VALUES (null, 'Y', null);
219 INSERT INTO tmp VALUES (5, '!check failed', null);
220 INSERT INTO tmp VALUES (null, 'try again', null);
221 INSERT INTO INSERT_TBL(y) select yd from tmp;
222 SELECT '' AS three, * FROM INSERT_TBL;
224 -------+---+---------------+----
226 | 5 | !check failed | -5
230 INSERT INTO INSERT_TBL SELECT * FROM tmp WHERE yd = 'try again';
231 INSERT INTO INSERT_TBL(y,z) SELECT yd, -7 FROM tmp WHERE yd = 'try again';
232 INSERT INTO INSERT_TBL(y,z) SELECT yd, -8 FROM tmp WHERE yd = 'try again';
233 ERROR: new row for relation "insert_tbl" violates check constraint "insert_con"
234 SELECT '' AS four, * FROM INSERT_TBL;
236 ------+---+---------------+----
238 | 5 | !check failed | -5
246 -- Check constraints on UPDATE
248 UPDATE INSERT_TBL SET x = NULL WHERE x = 5;
249 UPDATE INSERT_TBL SET x = 6 WHERE x = 6;
250 UPDATE INSERT_TBL SET x = -z, z = -x;
251 UPDATE INSERT_TBL SET x = z, z = x;
252 ERROR: new row for relation "insert_tbl" violates check constraint "insert_con"
253 SELECT * FROM INSERT_TBL;
255 ---+---------------+----
263 -- DROP TABLE INSERT_TBL;
265 -- Check constraints on COPY FROM
267 CREATE TABLE COPY_TBL (x INT, y TEXT, z INT,
269 CHECK (x > 3 AND y <> 'check failed' AND x < 7 ));
270 COPY COPY_TBL FROM '@abs_srcdir@/data/constro.data';
271 SELECT '' AS two, * FROM COPY_TBL;
273 -----+---+---------------+---
274 | 4 | !check failed | 5
278 COPY COPY_TBL FROM '@abs_srcdir@/data/constrf.data';
279 ERROR: new row for relation "copy_tbl" violates check constraint "copy_con"
280 CONTEXT: COPY copy_tbl, line 2: "7 check failed 6"
281 SELECT * FROM COPY_TBL;
283 ---+---------------+---
284 4 | !check failed | 5
291 CREATE TABLE PRIMARY_TBL (i int PRIMARY KEY, t text);
292 NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "primary_tbl_pkey" for table "primary_tbl"
293 INSERT INTO PRIMARY_TBL VALUES (1, 'one');
294 INSERT INTO PRIMARY_TBL VALUES (2, 'two');
295 INSERT INTO PRIMARY_TBL VALUES (1, 'three');
296 ERROR: duplicate key value violates unique constraint "primary_tbl_pkey"
297 INSERT INTO PRIMARY_TBL VALUES (4, 'three');
298 INSERT INTO PRIMARY_TBL VALUES (5, 'one');
299 INSERT INTO PRIMARY_TBL (t) VALUES ('six');
300 ERROR: null value in column "i" violates not-null constraint
301 SELECT '' AS four, * FROM PRIMARY_TBL;
310 DROP TABLE PRIMARY_TBL;
311 CREATE TABLE PRIMARY_TBL (i int, t text,
313 NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "primary_tbl_pkey" for table "primary_tbl"
314 INSERT INTO PRIMARY_TBL VALUES (1, 'one');
315 INSERT INTO PRIMARY_TBL VALUES (2, 'two');
316 INSERT INTO PRIMARY_TBL VALUES (1, 'three');
317 INSERT INTO PRIMARY_TBL VALUES (4, 'three');
318 INSERT INTO PRIMARY_TBL VALUES (5, 'one');
319 INSERT INTO PRIMARY_TBL (t) VALUES ('six');
320 ERROR: null value in column "i" violates not-null constraint
321 SELECT '' AS three, * FROM PRIMARY_TBL;
331 DROP TABLE PRIMARY_TBL;
335 CREATE TABLE UNIQUE_TBL (i int UNIQUE, t text);
336 NOTICE: CREATE TABLE / UNIQUE will create implicit index "unique_tbl_i_key" for table "unique_tbl"
337 INSERT INTO UNIQUE_TBL VALUES (1, 'one');
338 INSERT INTO UNIQUE_TBL VALUES (2, 'two');
339 INSERT INTO UNIQUE_TBL VALUES (1, 'three');
340 ERROR: duplicate key value violates unique constraint "unique_tbl_i_key"
341 INSERT INTO UNIQUE_TBL VALUES (4, 'four');
342 INSERT INTO UNIQUE_TBL VALUES (5, 'one');
343 INSERT INTO UNIQUE_TBL (t) VALUES ('six');
344 INSERT INTO UNIQUE_TBL (t) VALUES ('seven');
345 SELECT '' AS five, * FROM UNIQUE_TBL;
356 DROP TABLE UNIQUE_TBL;
357 CREATE TABLE UNIQUE_TBL (i int, t text,
359 NOTICE: CREATE TABLE / UNIQUE will create implicit index "unique_tbl_i_key" for table "unique_tbl"
360 INSERT INTO UNIQUE_TBL VALUES (1, 'one');
361 INSERT INTO UNIQUE_TBL VALUES (2, 'two');
362 INSERT INTO UNIQUE_TBL VALUES (1, 'three');
363 INSERT INTO UNIQUE_TBL VALUES (1, 'one');
364 ERROR: duplicate key value violates unique constraint "unique_tbl_i_key"
365 INSERT INTO UNIQUE_TBL VALUES (5, 'one');
366 INSERT INTO UNIQUE_TBL (t) VALUES ('six');
367 SELECT '' AS five, * FROM UNIQUE_TBL;
377 DROP TABLE UNIQUE_TBL;