@@ -473,7 +473,7 @@ NOTICE: ALTER TABLE / ADD UNIQUE will create implicit index 'atacc_test1' for t
473473ERROR: Cannot create unique index. Table contains non-unique values
474474insert into atacc1 (test) values (3);
475475drop table atacc1;
476- -- let's do one where the uniquecontsraint fails
476+ -- let's do one where the uniqueconstraint fails
477477-- because the column doesn't exist
478478create table atacc1 ( test int );
479479-- add a unique constraint (fails)
@@ -505,3 +505,76 @@ insert into atacc1 (test2, test) values (3, 3);
505505insert into atacc1 (test2, test) values (2, 3);
506506ERROR: Cannot insert a duplicate key into unique index atacc1_test_key
507507drop table atacc1;
508+ -- test primary key constraint adding
509+ create table atacc1 ( test int );
510+ -- add a primary key constraint
511+ alter table atacc1 add constraint atacc_test1 primary key (test);
512+ ERROR: Existing attribute "test" cannot be a PRIMARY KEY because it is not marked NOT NULL
513+ -- insert first value
514+ insert into atacc1 (test) values (2);
515+ -- should fail
516+ insert into atacc1 (test) values (2);
517+ -- should succeed
518+ insert into atacc1 (test) values (4);
519+ -- inserting NULL should fail
520+ insert into atacc1 (test) values(NULL);
521+ -- try adding a primary key oid constraint
522+ alter table atacc1 add constraint atacc_oid1 primary key(oid);
523+ NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index 'atacc_oid1' for table 'atacc1'
524+ drop table atacc1;
525+ -- let's do one where the primary key constraint fails when added
526+ create table atacc1 ( test int );
527+ -- insert soon to be failing rows
528+ insert into atacc1 (test) values (2);
529+ insert into atacc1 (test) values (2);
530+ -- add a primary key (fails)
531+ alter table atacc1 add constraint atacc_test1 primary key (test);
532+ ERROR: Existing attribute "test" cannot be a PRIMARY KEY because it is not marked NOT NULL
533+ insert into atacc1 (test) values (3);
534+ drop table atacc1;
535+ -- let's do another one where the primary key constraint fails when added
536+ create table atacc1 ( test int );
537+ -- insert soon to be failing row
538+ insert into atacc1 (test) values (NULL);
539+ -- add a primary key (fails)
540+ alter table atacc1 add constraint atacc_test1 primary key (test);
541+ ERROR: Existing attribute "test" cannot be a PRIMARY KEY because it is not marked NOT NULL
542+ insert into atacc1 (test) values (3);
543+ drop table atacc1;
544+ -- let's do one where the primary key constraint fails
545+ -- because the column doesn't exist
546+ create table atacc1 ( test int );
547+ -- add a primary key constraint (fails)
548+ alter table atacc1 add constraint atacc_test1 primary key (test1);
549+ ERROR: ALTER TABLE: column "test1" named in key does not exist
550+ drop table atacc1;
551+ -- something a little more complicated
552+ create table atacc1 ( test int, test2 int);
553+ -- add a primary key constraint
554+ alter table atacc1 add constraint atacc_test1 primary key (test, test2);
555+ ERROR: Existing attribute "test" cannot be a PRIMARY KEY because it is not marked NOT NULL
556+ -- try adding a second primary key - should fail
557+ alter table atacc1 add constraint atacc_test2 primary key (test);
558+ ERROR: Existing attribute "test" cannot be a PRIMARY KEY because it is not marked NOT NULL
559+ -- insert initial value
560+ insert into atacc1 (test,test2) values (4,4);
561+ -- should fail
562+ insert into atacc1 (test,test2) values (4,4);
563+ insert into atacc1 (test,test2) values (NULL,3);
564+ insert into atacc1 (test,test2) values (3, NULL);
565+ insert into atacc1 (test,test2) values (NULL,NULL);
566+ -- should all succeed
567+ insert into atacc1 (test,test2) values (4,5);
568+ insert into atacc1 (test,test2) values (5,4);
569+ insert into atacc1 (test,test2) values (5,5);
570+ drop table atacc1;
571+ -- lets do some naming tests
572+ create table atacc1 (test int, test2 int, primary key(test));
573+ NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'atacc1_pkey' for table 'atacc1'
574+ -- only first should succeed
575+ insert into atacc1 (test2, test) values (3, 3);
576+ insert into atacc1 (test2, test) values (2, 3);
577+ ERROR: Cannot insert a duplicate key into unique index atacc1_pkey
578+ insert into atacc1 (test2, test) values (1, NULL);
579+ ERROR: ExecAppend: Fail to add null value in not null attribute test
580+ drop table atacc1;