]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/alter_table.out
0b5dce62c9ff71c1f4b42792b8a628405d34f75c
[postgresql] / src / test / regress / expected / alter_table.out
1 --
2 -- ALTER_TABLE
3 -- add attribute
4 --
5 CREATE TABLE tmp (initial int4);
6 ALTER TABLE tmp ADD COLUMN a int4;
7 ALTER TABLE tmp ADD COLUMN b name;
8 ALTER TABLE tmp ADD COLUMN c text;
9 ALTER TABLE tmp ADD COLUMN d float8;
10 ALTER TABLE tmp ADD COLUMN e float4;
11 ALTER TABLE tmp ADD COLUMN f int2;
12 ALTER TABLE tmp ADD COLUMN g polygon;
13 ALTER TABLE tmp ADD COLUMN h abstime;
14 ALTER TABLE tmp ADD COLUMN i char;
15 ALTER TABLE tmp ADD COLUMN j abstime[];
16 ALTER TABLE tmp ADD COLUMN k int4;
17 ALTER TABLE tmp ADD COLUMN l tid;
18 ALTER TABLE tmp ADD COLUMN m xid;
19 ALTER TABLE tmp ADD COLUMN n oidvector;
20 --ALTER TABLE tmp ADD COLUMN o lock;
21 ALTER TABLE tmp ADD COLUMN p smgr;
22 ALTER TABLE tmp ADD COLUMN q point;
23 ALTER TABLE tmp ADD COLUMN r lseg;
24 ALTER TABLE tmp ADD COLUMN s path;
25 ALTER TABLE tmp ADD COLUMN t box;
26 ALTER TABLE tmp ADD COLUMN u tinterval;
27 ALTER TABLE tmp ADD COLUMN v datetime;
28 ALTER TABLE tmp ADD COLUMN w timespan;
29 ALTER TABLE tmp ADD COLUMN x float8[];
30 ALTER TABLE tmp ADD COLUMN y float4[];
31 ALTER TABLE tmp ADD COLUMN z int2[];
32 INSERT INTO tmp (a, b, c, d, e, f, g, h, i, j, k, l, m, n, p, q, r, s, t, u,
33         v, w, x, y, z)
34    VALUES (4, 'name', 'text', 4.1, 4.1, 2, '(4.1,4.1,3.1,3.1)', 
35         'Mon May  1 00:30:30 1995', 'c', '{Mon May  1 00:30:30 1995, Monday Aug 24 14:43:07 1992, epoch}', 
36         314159, '(1,1)', '512',
37         '1 2 3 4 5 6 7 8', 'magnetic disk', '(1.1,1.1)', '(4.1,4.1,3.1,3.1)',
38         '(0,2,4.1,4.1,3.1,3.1)', '(4.1,4.1,3.1,3.1)', '["epoch" "infinity"]',
39         'epoch', '01:00:10', '{1.0,2.0,3.0,4.0}', '{1.0,2.0,3.0,4.0}', '{1,2,3,4}');
40 SELECT * FROM tmp;
41  initial | a |  b   |  c   |  d  |  e  | f |           g           |              h               | i |                                               j                                                |   k    |   l   |  m  |        n        |       p       |     q     |           r           |              s              |          t          |                      u                      |            v             |        w         |     x     |     y     |     z     
42 ---------+---+------+------+-----+-----+---+-----------------------+------------------------------+---+------------------------------------------------------------------------------------------------+--------+-------+-----+-----------------+---------------+-----------+-----------------------+-----------------------------+---------------------+---------------------------------------------+--------------------------+------------------+-----------+-----------+-----------
43          | 4 | name | text | 4.1 | 4.1 | 2 | ((4.1,4.1),(3.1,3.1)) | Mon May 01 00:30:30 1995 PDT | c | {"Mon May 01 00:30:30 1995 PDT","Mon Aug 24 14:43:07 1992 PDT","Wed Dec 31 16:00:00 1969 PST"} | 314159 | (1,1) | 512 | 1 2 3 4 5 6 7 8 | magnetic disk | (1.1,1.1) | [(4.1,4.1),(3.1,3.1)] | ((0,2),(4.1,4.1),(3.1,3.1)) | (4.1,4.1),(3.1,3.1) | ["Wed Dec 31 16:00:00 1969 PST" "infinity"] | Thu Jan 01 00:00:00 1970 | @ 1 hour 10 secs | {1,2,3,4} | {1,2,3,4} | {1,2,3,4}
44 (1 row)
45
46 DROP TABLE tmp;
47 -- the wolf bug - schema mods caused inconsistent row descriptors 
48 CREATE TABLE tmp (
49         initial         int4
50 );
51 ALTER TABLE tmp ADD COLUMN a int4;
52 ALTER TABLE tmp ADD COLUMN b name;
53 ALTER TABLE tmp ADD COLUMN c text;
54 ALTER TABLE tmp ADD COLUMN d float8;
55 ALTER TABLE tmp ADD COLUMN e float4;
56 ALTER TABLE tmp ADD COLUMN f int2;
57 ALTER TABLE tmp ADD COLUMN g polygon;
58 ALTER TABLE tmp ADD COLUMN h abstime;
59 ALTER TABLE tmp ADD COLUMN i char;
60 ALTER TABLE tmp ADD COLUMN j abstime[];
61 ALTER TABLE tmp ADD COLUMN k int4;
62 ALTER TABLE tmp ADD COLUMN l tid;
63 ALTER TABLE tmp ADD COLUMN m xid;
64 ALTER TABLE tmp ADD COLUMN n oidvector;
65 --ALTER TABLE tmp ADD COLUMN o lock;
66 ALTER TABLE tmp ADD COLUMN p smgr;
67 ALTER TABLE tmp ADD COLUMN q point;
68 ALTER TABLE tmp ADD COLUMN r lseg;
69 ALTER TABLE tmp ADD COLUMN s path;
70 ALTER TABLE tmp ADD COLUMN t box;
71 ALTER TABLE tmp ADD COLUMN u tinterval;
72 ALTER TABLE tmp ADD COLUMN v datetime;
73 ALTER TABLE tmp ADD COLUMN w timespan;
74 ALTER TABLE tmp ADD COLUMN x float8[];
75 ALTER TABLE tmp ADD COLUMN y float4[];
76 ALTER TABLE tmp ADD COLUMN z int2[];
77 INSERT INTO tmp (a, b, c, d, e, f, g, h, i, j, k, l, m, n, p, q, r, s, t, u,
78         v, w, x, y, z)
79    VALUES (4, 'name', 'text', 4.1, 4.1, 2, '(4.1,4.1,3.1,3.1)', 
80         'Mon May  1 00:30:30 1995', 'c', '{Mon May  1 00:30:30 1995, Monday Aug 24 14:43:07 1992, epoch}', 
81         314159, '(1,1)', '512',
82         '1 2 3 4 5 6 7 8', 'magnetic disk', '(1.1,1.1)', '(4.1,4.1,3.1,3.1)',
83         '(0,2,4.1,4.1,3.1,3.1)', '(4.1,4.1,3.1,3.1)', '["epoch" "infinity"]',
84         'epoch', '01:00:10', '{1.0,2.0,3.0,4.0}', '{1.0,2.0,3.0,4.0}', '{1,2,3,4}');
85 SELECT * FROM tmp;
86  initial | a |  b   |  c   |  d  |  e  | f |           g           |              h               | i |                                               j                                                |   k    |   l   |  m  |        n        |       p       |     q     |           r           |              s              |          t          |                      u                      |            v             |        w         |     x     |     y     |     z     
87 ---------+---+------+------+-----+-----+---+-----------------------+------------------------------+---+------------------------------------------------------------------------------------------------+--------+-------+-----+-----------------+---------------+-----------+-----------------------+-----------------------------+---------------------+---------------------------------------------+--------------------------+------------------+-----------+-----------+-----------
88          | 4 | name | text | 4.1 | 4.1 | 2 | ((4.1,4.1),(3.1,3.1)) | Mon May 01 00:30:30 1995 PDT | c | {"Mon May 01 00:30:30 1995 PDT","Mon Aug 24 14:43:07 1992 PDT","Wed Dec 31 16:00:00 1969 PST"} | 314159 | (1,1) | 512 | 1 2 3 4 5 6 7 8 | magnetic disk | (1.1,1.1) | [(4.1,4.1),(3.1,3.1)] | ((0,2),(4.1,4.1),(3.1,3.1)) | (4.1,4.1),(3.1,3.1) | ["Wed Dec 31 16:00:00 1969 PST" "infinity"] | Thu Jan 01 00:00:00 1970 | @ 1 hour 10 secs | {1,2,3,4} | {1,2,3,4} | {1,2,3,4}
89 (1 row)
90
91 DROP TABLE tmp;
92 --
93 -- rename -
94 --   should preserve indices, which we can check by seeing if a SELECT
95 --   chooses an indexscan; however, in the absence of vacuum statistics
96 --   it might not.  Therefore, vacuum first.
97 --
98 VACUUM ANALYZE tenk1;
99 ALTER TABLE tenk1 RENAME TO ten_k;
100 -- 20 values, sorted 
101 SELECT unique1 FROM ten_k WHERE unique1 < 20;
102  unique1 
103 ---------
104        0
105        1
106        2
107        3
108        4
109        5
110        6
111        7
112        8
113        9
114       10
115       11
116       12
117       13
118       14
119       15
120       16
121       17
122       18
123       19
124 (20 rows)
125
126 -- 20 values, sorted 
127 SELECT unique2 FROM ten_k WHERE unique2 < 20;
128  unique2 
129 ---------
130        0
131        1
132        2
133        3
134        4
135        5
136        6
137        7
138        8
139        9
140       10
141       11
142       12
143       13
144       14
145       15
146       16
147       17
148       18
149       19
150 (20 rows)
151
152 -- 100 values, sorted 
153 SELECT hundred FROM ten_k WHERE hundred = 50;
154  hundred 
155 ---------
156       50
157       50
158       50
159       50
160       50
161       50
162       50
163       50
164       50
165       50
166       50
167       50
168       50
169       50
170       50
171       50
172       50
173       50
174       50
175       50
176       50
177       50
178       50
179       50
180       50
181       50
182       50
183       50
184       50
185       50
186       50
187       50
188       50
189       50
190       50
191       50
192       50
193       50
194       50
195       50
196       50
197       50
198       50
199       50
200       50
201       50
202       50
203       50
204       50
205       50
206       50
207       50
208       50
209       50
210       50
211       50
212       50
213       50
214       50
215       50
216       50
217       50
218       50
219       50
220       50
221       50
222       50
223       50
224       50
225       50
226       50
227       50
228       50
229       50
230       50
231       50
232       50
233       50
234       50
235       50
236       50
237       50
238       50
239       50
240       50
241       50
242       50
243       50
244       50
245       50
246       50
247       50
248       50
249       50
250       50
251       50
252       50
253       50
254       50
255       50
256 (100 rows)
257
258 ALTER TABLE ten_k RENAME TO tenk1;
259 -- 5 values, sorted 
260 SELECT unique1 FROM tenk1 WHERE unique1 < 5;
261  unique1 
262 ---------
263        0
264        1
265        2
266        3
267        4
268 (5 rows)
269
270 -- ALTER TABLE ... RENAME on non-table relations
271 -- renaming indexes (FIXME: this should probably test the index's functionality)
272 ALTER TABLE onek_unique1 RENAME TO tmp_onek_unique1;
273 ALTER TABLE tmp_onek_unique1 RENAME TO onek_unique1;
274 -- renaming views
275 CREATE VIEW tmp_view (unique1) AS SELECT unique1 FROM tenk1;
276 ALTER TABLE tmp_view RENAME TO tmp_view_new;
277 -- 5 values, sorted 
278 SELECT unique1 FROM tenk1 WHERE unique1 < 5;
279  unique1 
280 ---------
281        0
282        1
283        2
284        3
285        4
286 (5 rows)
287
288 DROP VIEW tmp_view_new;
289 -- renaming sequences
290 CREATE SEQUENCE foo_seq;
291 ALTER TABLE foo_seq RENAME TO foo_seq_new;
292 SELECT * FROM foo_seq_new;
293  sequence_name | last_value | increment_by |      max_value      | min_value | cache_value | log_cnt | is_cycled | is_called 
294 ---------------+------------+--------------+---------------------+-----------+-------------+---------+-----------+-----------
295  foo_seq       |          1 |            1 | 9223372036854775807 |         1 |           1 |       1 | f         | f
296 (1 row)
297
298 DROP SEQUENCE foo_seq_new;
299 -- toast-like relation name
300 alter table stud_emp rename to pg_toast_stud_emp;
301 alter table pg_toast_stud_emp rename to stud_emp;
302 -- FOREIGN KEY CONSTRAINT adding TEST
303 CREATE TABLE tmp2 (a int primary key);
304 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 'tmp2_pkey' for table 'tmp2'
305 CREATE TABLE tmp3 (a int, b int);
306 CREATE TABLE tmp4 (a int, b int, unique(a,b));
307 NOTICE:  CREATE TABLE / UNIQUE will create implicit index 'tmp4_a_key' for table 'tmp4'
308 CREATE TABLE tmp5 (a int, b int);
309 -- Insert rows into tmp2 (pktable)
310 INSERT INTO tmp2 values (1);
311 INSERT INTO tmp2 values (2);
312 INSERT INTO tmp2 values (3);
313 INSERT INTO tmp2 values (4);
314 -- Insert rows into tmp3
315 INSERT INTO tmp3 values (1,10);
316 INSERT INTO tmp3 values (1,20);
317 INSERT INTO tmp3 values (5,50);
318 -- Try (and fail) to add constraint due to invalid source columns
319 ALTER TABLE tmp3 add constraint tmpconstr foreign key(c) references tmp2 match full;
320 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
321 ERROR:  ALTER TABLE: column "c" referenced in foreign key constraint does not exist
322 -- Try (and fail) to add constraint due to invalide destination columns explicitly given
323 ALTER TABLE tmp3 add constraint tmpconstr foreign key(a) references tmp2(b) match full;
324 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
325 ERROR:  UNIQUE constraint matching given keys for referenced table "tmp2" not found
326 -- Try (and fail) to add constraint due to invalid data
327 ALTER TABLE tmp3 add constraint tmpconstr foreign key (a) references tmp2 match full;
328 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
329 ERROR:  tmpconstr referential integrity violation - key referenced from tmp3 not found in tmp2
330 -- Delete failing row
331 DELETE FROM tmp3 where a=5;
332 -- Try (and succeed)
333 ALTER TABLE tmp3 add constraint tmpconstr foreign key (a) references tmp2 match full;
334 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
335 -- Try (and fail) to create constraint from tmp5(a) to tmp4(a) - unique constraint on
336 -- tmp4 is a,b
337 ALTER TABLE tmp5 add constraint tmpconstr foreign key(a) references tmp4(a) match full;
338 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
339 ERROR:  UNIQUE constraint matching given keys for referenced table "tmp4" not found
340 DROP TABLE tmp5;
341 DROP TABLE tmp4;
342 DROP TABLE tmp3;
343 NOTICE:  DROP TABLE implicitly drops referential integrity trigger from table "tmp2"
344 NOTICE:  DROP TABLE implicitly drops referential integrity trigger from table "tmp2"
345 DROP TABLE tmp2;
346 -- Foreign key adding test with mixed types
347 -- Note: these tables are TEMP to avoid name conflicts when this test
348 -- is run in parallel with foreign_key.sql.
349 CREATE TEMP TABLE PKTABLE (ptest1 int PRIMARY KEY);
350 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 'pktable_pkey' for table 'pktable'
351 CREATE TEMP TABLE FKTABLE (ftest1 inet);
352 -- This next should fail, because inet=int does not exist
353 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
354 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
355 ERROR:  Unable to identify an operator '=' for types 'inet' and 'integer'
356         You will have to retype this query using an explicit cast
357 -- This should also fail for the same reason, but here we
358 -- give the column name
359 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable(ptest1);
360 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
361 ERROR:  Unable to identify an operator '=' for types 'inet' and 'integer'
362         You will have to retype this query using an explicit cast
363 -- This should succeed, even though they are different types
364 -- because varchar=int does exist
365 DROP TABLE FKTABLE;
366 CREATE TEMP TABLE FKTABLE (ftest1 varchar);
367 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
368 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
369 -- As should this
370 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable(ptest1);
371 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
372 DROP TABLE pktable;
373 NOTICE:  DROP TABLE implicitly drops referential integrity trigger from table "fktable"
374 NOTICE:  DROP TABLE implicitly drops referential integrity trigger from table "fktable"
375 DROP TABLE fktable;
376 CREATE TEMP TABLE PKTABLE (ptest1 int, ptest2 inet,
377                            PRIMARY KEY(ptest1, ptest2));
378 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 'pktable_pkey' for table 'pktable'
379 -- This should fail, because we just chose really odd types
380 CREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 datetime);
381 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2) references pktable;
382 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
383 ERROR:  Unable to identify an operator '=' for types 'cidr' and 'integer'
384         You will have to retype this query using an explicit cast
385 -- Again, so should this...
386 DROP TABLE FKTABLE;
387 CREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 datetime);
388 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2)
389      references pktable(ptest1, ptest2);
390 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
391 ERROR:  Unable to identify an operator '=' for types 'cidr' and 'integer'
392         You will have to retype this query using an explicit cast
393 -- This fails because we mixed up the column ordering
394 DROP TABLE FKTABLE;
395 CREATE TEMP TABLE FKTABLE (ftest1 int, ftest2 inet);
396 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2)
397      references pktable(ptest2, ptest1);
398 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
399 ERROR:  Unable to identify an operator '=' for types 'integer' and 'inet'
400         You will have to retype this query using an explicit cast
401 -- As does this...
402 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest2, ftest1)
403      references pktable(ptest1, ptest2);
404 NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
405 ERROR:  Unable to identify an operator '=' for types 'inet' and 'integer'
406         You will have to retype this query using an explicit cast
407 -- temp tables should go away by themselves, need not drop them.
408 -- test check constraint adding
409 create table atacc1 ( test int );
410 -- add a check constraint
411 alter table atacc1 add constraint atacc_test1 check (test>3);
412 -- should fail
413 insert into atacc1 (test) values (2);
414 ERROR:  ExecAppend: rejected due to CHECK constraint atacc_test1
415 -- should succeed
416 insert into atacc1 (test) values (4);
417 drop table atacc1;
418 -- let's do one where the check fails when added
419 create table atacc1 ( test int );
420 -- insert a soon to be failing row
421 insert into atacc1 (test) values (2);
422 -- add a check constraint (fails)
423 alter table atacc1 add constraint atacc_test1 check (test>3);
424 ERROR:  AlterTableAddConstraint: rejected due to CHECK constraint atacc_test1
425 insert into atacc1 (test) values (4);
426 drop table atacc1;
427 -- let's do one where the check fails because the column doesn't exist
428 create table atacc1 ( test int );
429 -- add a check constraint (fails)
430 alter table atacc1 add constraint atacc_test1 check (test1>3);
431 ERROR:  Attribute "test1" not found
432 drop table atacc1;
433 -- something a little more complicated
434 create table atacc1 ( test int, test2 int, test3 int);
435 -- add a check constraint (fails)
436 alter table atacc1 add constraint atacc_test1 check (test+test2<test3*4);
437 -- should fail
438 insert into atacc1 (test,test2,test3) values (4,4,2);
439 ERROR:  ExecAppend: rejected due to CHECK constraint atacc_test1
440 -- should succeed
441 insert into atacc1 (test,test2,test3) values (4,4,5);
442 drop table atacc1;
443 -- lets do some naming tests
444 create table atacc1 (test int check (test>3), test2 int);
445 alter table atacc1 add check (test2>test);
446 -- should fail for $2
447 insert into atacc1 (test2, test) values (3, 4);
448 ERROR:  ExecAppend: rejected due to CHECK constraint $2
449 drop table atacc1;
450 -- inheritance related tests
451 create table atacc1 (test int);
452 create table atacc2 (test2 int);
453 create table atacc3 (test3 int) inherits (atacc1, atacc2);
454 alter table atacc2 add constraint foo check (test2>0);
455 -- fail and then succeed on atacc2
456 insert into atacc2 (test2) values (-3);
457 ERROR:  ExecAppend: rejected due to CHECK constraint foo
458 insert into atacc2 (test2) values (3);
459 -- fail and then succeed on atacc3
460 insert into atacc3 (test2) values (-3);
461 ERROR:  ExecAppend: rejected due to CHECK constraint foo
462 insert into atacc3 (test2) values (3);
463 drop table atacc3;
464 drop table atacc2;
465 drop table atacc1;
466 -- let's try only to add only to the parent
467 create table atacc1 (test int);
468 create table atacc2 (test2 int);
469 create table atacc3 (test3 int) inherits (atacc1, atacc2);
470 alter table only atacc2 add constraint foo check (test2>0);
471 -- fail and then succeed on atacc2
472 insert into atacc2 (test2) values (-3);
473 ERROR:  ExecAppend: rejected due to CHECK constraint foo
474 insert into atacc2 (test2) values (3);
475 -- both succeed on atacc3
476 insert into atacc3 (test2) values (-3);
477 insert into atacc3 (test2) values (3);
478 drop table atacc3;
479 drop table atacc2;
480 drop table atacc1;
481 -- test unique constraint adding
482 create table atacc1 ( test int );
483 -- add a unique constraint
484 alter table atacc1 add constraint atacc_test1 unique (test);
485 NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index 'atacc_test1' for table 'atacc1'
486 -- insert first value
487 insert into atacc1 (test) values (2);
488 -- should fail
489 insert into atacc1 (test) values (2);
490 ERROR:  Cannot insert a duplicate key into unique index atacc_test1
491 -- should succeed
492 insert into atacc1 (test) values (4);
493 -- try adding a unique oid constraint
494 alter table atacc1 add constraint atacc_oid1 unique(oid);
495 NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index 'atacc_oid1' for table 'atacc1'
496 drop table atacc1;
497 -- let's do one where the unique constraint fails when added
498 create table atacc1 ( test int );
499 -- insert soon to be failing rows
500 insert into atacc1 (test) values (2);
501 insert into atacc1 (test) values (2);
502 -- add a unique constraint (fails)
503 alter table atacc1 add constraint atacc_test1 unique (test);
504 NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index 'atacc_test1' for table 'atacc1'
505 ERROR:  Cannot create unique index. Table contains non-unique values
506 insert into atacc1 (test) values (3);
507 drop table atacc1;
508 -- let's do one where the unique constraint fails
509 -- because the column doesn't exist
510 create table atacc1 ( test int );
511 -- add a unique constraint (fails)
512 alter table atacc1 add constraint atacc_test1 unique (test1);
513 ERROR:  ALTER TABLE: column "test1" named in key does not exist
514 drop table atacc1;
515 -- something a little more complicated
516 create table atacc1 ( test int, test2 int);
517 -- add a unique constraint
518 alter table atacc1 add constraint atacc_test1 unique (test, test2);
519 NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index 'atacc_test1' for table 'atacc1'
520 -- insert initial value
521 insert into atacc1 (test,test2) values (4,4);
522 -- should fail
523 insert into atacc1 (test,test2) values (4,4);
524 ERROR:  Cannot insert a duplicate key into unique index atacc_test1
525 -- should all succeed
526 insert into atacc1 (test,test2) values (4,5);
527 insert into atacc1 (test,test2) values (5,4);
528 insert into atacc1 (test,test2) values (5,5);
529 drop table atacc1;
530 -- lets do some naming tests
531 create table atacc1 (test int, test2 int, unique(test));
532 NOTICE:  CREATE TABLE / UNIQUE will create implicit index 'atacc1_test_key' for table 'atacc1'
533 alter table atacc1 add unique (test2);
534 NOTICE:  ALTER TABLE / ADD UNIQUE will create implicit index 'atacc1_test2_key' for table 'atacc1'
535 -- should fail for @@ second one @@
536 insert into atacc1 (test2, test) values (3, 3);
537 insert into atacc1 (test2, test) values (2, 3);
538 ERROR:  Cannot insert a duplicate key into unique index atacc1_test_key
539 drop table atacc1;
540 -- test primary key constraint adding
541 create table atacc1 ( test int );
542 -- add a primary key constraint
543 alter table atacc1 add constraint atacc_test1 primary key (test);
544 ERROR:  Existing attribute "test" cannot be a PRIMARY KEY because it is not marked NOT NULL
545 -- insert first value
546 insert into atacc1 (test) values (2);
547 -- should fail
548 insert into atacc1 (test) values (2);
549 -- should succeed
550 insert into atacc1 (test) values (4);
551 -- inserting NULL should fail
552 insert into atacc1 (test) values(NULL);
553 -- try adding a primary key oid constraint
554 alter table atacc1 add constraint atacc_oid1 primary key(oid);
555 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index 'atacc_oid1' for table 'atacc1'
556 drop table atacc1;
557 -- let's do one where the primary key constraint fails when added
558 create table atacc1 ( test int );
559 -- insert soon to be failing rows
560 insert into atacc1 (test) values (2);
561 insert into atacc1 (test) values (2);
562 -- add a primary key (fails)
563 alter table atacc1 add constraint atacc_test1 primary key (test);
564 ERROR:  Existing attribute "test" cannot be a PRIMARY KEY because it is not marked NOT NULL
565 insert into atacc1 (test) values (3);
566 drop table atacc1;
567 -- let's do another one where the primary key constraint fails when added
568 create table atacc1 ( test int );
569 -- insert soon to be failing row
570 insert into atacc1 (test) values (NULL);
571 -- add a primary key (fails)
572 alter table atacc1 add constraint atacc_test1 primary key (test);
573 ERROR:  Existing attribute "test" cannot be a PRIMARY KEY because it is not marked NOT NULL
574 insert into atacc1 (test) values (3);
575 drop table atacc1;
576 -- let's do one where the primary key constraint fails
577 -- because the column doesn't exist
578 create table atacc1 ( test int );
579 -- add a primary key constraint (fails)
580 alter table atacc1 add constraint atacc_test1 primary key (test1);
581 ERROR:  ALTER TABLE: column "test1" named in key does not exist
582 drop table atacc1;
583 -- something a little more complicated
584 create table atacc1 ( test int, test2 int);
585 -- add a primary key constraint
586 alter table atacc1 add constraint atacc_test1 primary key (test, test2);
587 ERROR:  Existing attribute "test" cannot be a PRIMARY KEY because it is not marked NOT NULL
588 -- try adding a second primary key - should fail
589 alter table atacc1 add constraint atacc_test2 primary key (test);
590 ERROR:  Existing attribute "test" cannot be a PRIMARY KEY because it is not marked NOT NULL
591 -- insert initial value
592 insert into atacc1 (test,test2) values (4,4);
593 -- should fail
594 insert into atacc1 (test,test2) values (4,4);
595 insert into atacc1 (test,test2) values (NULL,3);
596 insert into atacc1 (test,test2) values (3, NULL);
597 insert into atacc1 (test,test2) values (NULL,NULL);
598 -- should all succeed
599 insert into atacc1 (test,test2) values (4,5);
600 insert into atacc1 (test,test2) values (5,4);
601 insert into atacc1 (test,test2) values (5,5);
602 drop table atacc1;
603 -- lets do some naming tests
604 create table atacc1 (test int, test2 int, primary key(test));
605 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 'atacc1_pkey' for table 'atacc1'
606 -- only first should succeed
607 insert into atacc1 (test2, test) values (3, 3);
608 insert into atacc1 (test2, test) values (2, 3);
609 ERROR:  Cannot insert a duplicate key into unique index atacc1_pkey
610 insert into atacc1 (test2, test) values (1, NULL);
611 ERROR:  ExecAppend: Fail to add null value in not null attribute test
612 drop table atacc1;
613 -- alter table / alter column [set/drop] not null tests
614 -- try altering system catalogs, should fail
615 alter table pg_class alter column relname drop not null;
616 ERROR:  ALTER TABLE: relation "pg_class" is a system catalog
617 alter table pg_class alter relname set not null;
618 ERROR:  ALTER TABLE: relation "pg_class" is a system catalog
619 -- try altering non-existent table, should fail
620 alter table foo alter column bar set not null;
621 ERROR:  Relation "foo" does not exist
622 alter table foo alter column bar drop not null;
623 ERROR:  Relation "foo" does not exist
624 -- test setting columns to null and not null and vice versa
625 -- test checking for null values and primary key
626 create table atacc1 (test int not null);
627 alter table atacc1 add constraint "atacc1_pkey" primary key (test);
628 NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index 'atacc1_pkey' for table 'atacc1'
629 alter table atacc1 alter column test drop not null;
630 ERROR:  ALTER TABLE: Attribute "test" is in a primary key
631 drop index atacc1_pkey;
632 alter table atacc1 alter column test drop not null;
633 insert into atacc1 values (null);
634 alter table atacc1 alter test set not null;
635 ERROR:  ALTER TABLE: Attribute "test" contains NULL values
636 delete from atacc1;
637 alter table atacc1 alter test set not null;
638 -- try altering a non-existent column, should fail
639 alter table atacc1 alter bar set not null;
640 ERROR:  ALTER TABLE: relation "atacc1" has no column "bar"
641 alter table atacc1 alter bar drop not null;
642 ERROR:  ALTER TABLE: relation "atacc1" has no column "bar"
643 -- try altering the oid column, should fail
644 alter table atacc1 alter oid set not null;
645 ERROR:  ALTER TABLE: Cannot alter system attribute "oid"
646 alter table atacc1 alter oid drop not null;
647 ERROR:  ALTER TABLE: Cannot alter system attribute "oid"
648 -- try creating a view and altering that, should fail
649 create view myview as select * from atacc1;
650 alter table myview alter column test drop not null;
651 ERROR:  ALTER TABLE: relation "myview" is not a table
652 alter table myview alter column test set not null;
653 ERROR:  ALTER TABLE: relation "myview" is not a table
654 drop view myview;
655 drop table atacc1;
656 -- test inheritance
657 create table parent (a int);
658 create table child (b varchar(255)) inherits (parent);
659 alter table parent alter a set not null;
660 insert into parent values (NULL);
661 ERROR:  ExecAppend: Fail to add null value in not null attribute a
662 insert into child (a, b) values (NULL, 'foo');
663 ERROR:  ExecAppend: Fail to add null value in not null attribute a
664 alter table parent alter a drop not null;
665 insert into parent values (NULL);
666 insert into child (a, b) values (NULL, 'foo');
667 alter table only parent alter a set not null;
668 ERROR:  ALTER TABLE: Attribute "a" contains NULL values
669 alter table child alter a set not null;
670 ERROR:  ALTER TABLE: Attribute "a" contains NULL values
671 delete from parent;
672 alter table only parent alter a set not null;
673 insert into parent values (NULL);
674 ERROR:  ExecAppend: Fail to add null value in not null attribute a
675 alter table child alter a set not null;
676 insert into child (a, b) values (NULL, 'foo');
677 ERROR:  ExecAppend: Fail to add null value in not null attribute a
678 delete from child;
679 alter table child alter a set not null;
680 insert into child (a, b) values (NULL, 'foo');
681 ERROR:  ExecAppend: Fail to add null value in not null attribute a
682 drop table child;
683 drop table parent;
684 -- test setting and removing default values
685 create table def_test (
686         c1      int4 default 5,
687         c2      text default 'initial_default'
688 );
689 insert into def_test default values;
690 alter table def_test alter column c1 drop default;
691 insert into def_test default values;
692 alter table def_test alter column c2 drop default;
693 insert into def_test default values;
694 alter table def_test alter column c1 set default 10;
695 alter table def_test alter column c2 set default 'new_default';
696 insert into def_test default values;
697 select * from def_test;
698  c1 |       c2        
699 ----+-----------------
700   5 | initial_default
701     | initial_default
702     | 
703  10 | new_default
704 (4 rows)
705
706 -- set defaults to an incorrect type: this should fail
707 alter table def_test alter column c1 set default 'wrong_datatype';
708 ERROR:  pg_atoi: error in "wrong_datatype": can't parse "wrong_datatype"
709 alter table def_test alter column c2 set default 20;
710 -- set defaults on a non-existent column: this should fail
711 alter table def_test alter column c3 set default 30;
712 ERROR:  ALTER TABLE: relation "def_test" has no column "c3"
713 -- set defaults on views: we need to create a view, add a rule
714 -- to allow insertions into it, and then alter the view to add
715 -- a default
716 create view def_view_test as select * from def_test;
717 create rule def_view_test_ins as
718         on insert to def_view_test
719         do instead insert into def_test select new.*;
720 insert into def_view_test default values;
721 alter table def_view_test alter column c1 set default 45;
722 insert into def_view_test default values;
723 alter table def_view_test alter column c2 set default 'view_default';
724 insert into def_view_test default values;
725 select * from def_view_test;
726  c1 |       c2        
727 ----+-----------------
728   5 | initial_default
729     | initial_default
730     | 
731  10 | new_default
732     | 
733  45 | 
734  45 | view_default
735 (7 rows)
736
737 drop rule def_view_test_ins;
738 drop view def_view_test;
739 drop table def_test;