@@ -271,10 +271,10 @@ SELECT unique1 FROM tenk1 WHERE unique1 < 5;
271
271
272
272
-- FOREIGN KEY CONSTRAINT adding TEST
273
273
CREATE TABLE tmp2 (a int primary key);
274
- NOTICE: CREATE TABLE/ PRIMARY KEY will create implicit index 'tmp2_pkey' for table 'tmp2'
274
+ NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'tmp2_pkey' for table 'tmp2'
275
275
CREATE TABLE tmp3 (a int, b int);
276
276
CREATE TABLE tmp4 (a int, b int, unique(a,b));
277
- NOTICE: CREATE TABLE/ UNIQUE will create implicit index 'tmp4_a_key' for table 'tmp4'
277
+ NOTICE: CREATE TABLE / ADD UNIQUE will create implicit index 'tmp4_a_key' for table 'tmp4'
278
278
CREATE TABLE tmp5 (a int, b int);
279
279
-- Insert rows into tmp2 (pktable)
280
280
INSERT INTO tmp2 values (1);
@@ -317,7 +317,7 @@ DROP TABLE tmp2;
317
317
-- Note: these tables are TEMP to avoid name conflicts when this test
318
318
-- is run in parallel with foreign_key.sql.
319
319
CREATE TEMP TABLE PKTABLE (ptest1 int PRIMARY KEY);
320
- NOTICE: CREATE TABLE/ PRIMARY KEY will create implicit index 'pktable_pkey' for table 'pktable'
320
+ NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'pktable_pkey' for table 'pktable'
321
321
CREATE TEMP TABLE FKTABLE (ftest1 text);
322
322
-- This next should fail, because text=int does not exist
323
323
ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
@@ -345,7 +345,7 @@ NOTICE: DROP TABLE implicitly drops referential integrity trigger from table "f
345
345
DROP TABLE fktable;
346
346
CREATE TEMP TABLE PKTABLE (ptest1 int, ptest2 text,
347
347
PRIMARY KEY(ptest1, ptest2));
348
- NOTICE: CREATE TABLE/ PRIMARY KEY will create implicit index 'pktable_pkey' for table 'pktable'
348
+ NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'pktable_pkey' for table 'pktable'
349
349
-- This should fail, because we just chose really odd types
350
350
CREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 datetime);
351
351
ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2) references pktable;
@@ -448,3 +448,62 @@ insert into atacc3 (test2) values (3);
448
448
drop table atacc3;
449
449
drop table atacc2;
450
450
drop table atacc1;
451
+ -- test unique constraint adding
452
+ create table atacc1 ( test int );
453
+ -- add a unique constraint
454
+ alter table atacc1 add constraint atacc_test1 unique (test);
455
+ NOTICE: ALTER TABLE/UNIQUE will create implicit index 'atacc_test1' for table 'atacc1'
456
+ -- insert first value
457
+ insert into atacc1 (test) values (2);
458
+ -- should fail
459
+ insert into atacc1 (test) values (2);
460
+ ERROR: Cannot insert a duplicate key into unique index atacc_test1
461
+ -- should succeed
462
+ insert into atacc1 (test) values (4);
463
+ -- try adding a unique oid constraint
464
+ alter table atacc1 add constraint atacc_oid1 unique(oid);
465
+ NOTICE: ALTER TABLE/UNIQUE will create implicit index 'atacc_oid1' for table 'atacc1'
466
+ drop table atacc1;
467
+ -- let's do one where the unique constraint fails when added
468
+ create table atacc1 ( test int );
469
+ -- insert soon to be failing rows
470
+ insert into atacc1 (test) values (2);
471
+ insert into atacc1 (test) values (2);
472
+ -- add a unique constraint (fails)
473
+ alter table atacc1 add constraint atacc_test1 unique (test);
474
+ NOTICE: ALTER TABLE/UNIQUE will create implicit index 'atacc_test1' for table 'atacc1'
475
+ ERROR: Cannot create unique index. Table contains non-unique values
476
+ insert into atacc1 (test) values (3);
477
+ drop table atacc1;
478
+ -- let's do one where the unique contsraint fails
479
+ -- because the column doesn't exist
480
+ create table atacc1 ( test int );
481
+ -- add a unique constraint (fails)
482
+ alter table atacc1 add constraint atacc_test1 unique (test1);
483
+ ERROR: ALTER TABLE: column "test1" named in key does not exist
484
+ drop table atacc1;
485
+ -- something a little more complicated
486
+ create table atacc1 ( test int, test2 int);
487
+ -- add a unique constraint
488
+ alter table atacc1 add constraint atacc_test1 unique (test, test2);
489
+ NOTICE: ALTER TABLE/UNIQUE will create implicit index 'atacc_test1' for table 'atacc1'
490
+ -- insert initial value
491
+ insert into atacc1 (test,test2) values (4,4);
492
+ -- should fail
493
+ insert into atacc1 (test,test2) values (4,4);
494
+ ERROR: Cannot insert a duplicate key into unique index atacc_test1
495
+ -- should all succeed
496
+ insert into atacc1 (test,test2) values (4,5);
497
+ insert into atacc1 (test,test2) values (5,4);
498
+ insert into atacc1 (test,test2) values (5,5);
499
+ drop table atacc1;
500
+ -- lets do some naming tests
501
+ create table atacc1 (test int, test2 int, unique(test));
502
+ NOTICE: CREATE TABLE/UNIQUE will create implicit index 'atacc1_test_key' for table 'atacc1'
503
+ alter table atacc1 add unique (test2);
504
+ NOTICE: ALTER TABLE/UNIQUE will create implicit index 'atacc1_test2_key' for table 'atacc1'
505
+ -- should fail for @@ second one @@
506
+ insert into atacc1 (test2, test) values (3, 3);
507
+ insert into atacc1 (test2, test) values (2, 3);
508
+ ERROR: Cannot insert a duplicate key into unique index atacc1_test_key
509
+ drop table atacc1;
0 commit comments