Commit 4911c85e authored by Bruce Momjian's avatar Bruce Momjian

Add ALTER TABLE ADD UNIQUE regression tests from Christopher Kings-Lynne.

Add space between slash for ALTER TABLE / ADD ....

Regression and *.po updates to follow.
parent 74c2f8e7
<!-- <!--
$Header: /cvsroot/pgsql/doc/src/sgml/ref/cluster.sgml,v 1.11 2001/09/03 12:57:49 petere Exp $ $Header: /cvsroot/pgsql/doc/src/sgml/ref/cluster.sgml,v 1.12 2001/10/31 04:49:43 momjian Exp $
Postgres documentation Postgres documentation
--> -->
...@@ -189,7 +189,7 @@ SELECT <replaceable class="parameter">columnlist</replaceable> INTO TABLE <repla ...@@ -189,7 +189,7 @@ SELECT <replaceable class="parameter">columnlist</replaceable> INTO TABLE <repla
which uses the <productname>Postgres</productname> sorting code in which uses the <productname>Postgres</productname> sorting code in
the ORDER BY clause to match the index, and which is much faster for the ORDER BY clause to match the index, and which is much faster for
unordered data. You then drop the old table, use unordered data. You then drop the old table, use
<command>ALTER TABLE/RENAME</command> <command>ALTER TABLE...RENAME</command>
to rename <replaceable class="parameter">temp</replaceable> to the old name, and to rename <replaceable class="parameter">temp</replaceable> to the old name, and
recreate any indexes. The only problem is that <acronym>OID</acronym>s recreate any indexes. The only problem is that <acronym>OID</acronym>s
will not be preserved. From then on, <command>CLUSTER</command> should be will not be preserved. From then on, <command>CLUSTER</command> should be
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.147 2001/10/28 06:25:42 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.148 2001/10/31 04:49:43 momjian Exp $
* *
* NOTES * NOTES
* The PerformAddAttribute() code, like most of the relation * The PerformAddAttribute() code, like most of the relation
...@@ -1432,7 +1432,7 @@ AlterTableAddConstraint(char *relationName, ...@@ -1432,7 +1432,7 @@ AlterTableAddConstraint(char *relationName,
0, 0, 0); 0, 0, 0);
if (!HeapTupleIsValid(indexTuple)) if (!HeapTupleIsValid(indexTuple))
elog(ERROR, "ALTER TABLE/ADD CONSTRAINT: Index \"%u\" not found", elog(ERROR, "ALTER TABLE / ADD CONSTRAINT: Index \"%u\" not found",
indexoid); indexoid);
indexStruct = (Form_pg_index) GETSTRUCT(indexTuple); indexStruct = (Form_pg_index) GETSTRUCT(indexTuple);
...@@ -1510,7 +1510,7 @@ AlterTableAddConstraint(char *relationName, ...@@ -1510,7 +1510,7 @@ AlterTableAddConstraint(char *relationName,
NIL); NIL);
/* Issue notice */ /* Issue notice */
elog(NOTICE, "ALTER TABLE/ADD UNIQUE will create implicit index '%s' for table '%s'", elog(NOTICE, "ALTER TABLE / ADD UNIQUE will create implicit index '%s' for table '%s'",
iname, relationName); iname, relationName);
if (index_found) if (index_found)
elog(NOTICE, "Unique constraint supercedes existing index on relation \"%s\". Drop the existing index to remove redundancy.", relationName); elog(NOTICE, "Unique constraint supercedes existing index on relation \"%s\". Drop the existing index to remove redundancy.", relationName);
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.205 2001/10/28 06:25:46 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.206 2001/10/31 04:49:43 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -1062,7 +1062,7 @@ transformIndexConstraints(ParseState *pstate, CreateStmtContext *cxt) ...@@ -1062,7 +1062,7 @@ transformIndexConstraints(ParseState *pstate, CreateStmtContext *cxt)
if (cxt->pkey != NULL || if (cxt->pkey != NULL ||
(OidIsValid(cxt->relOid) && (OidIsValid(cxt->relOid) &&
relationHasPrimaryKey(cxt->relname))) relationHasPrimaryKey(cxt->relname)))
elog(ERROR, "%s/PRIMARY KEY multiple primary keys" elog(ERROR, "%s / PRIMARY KEY multiple primary keys"
" for table '%s' are not allowed", " for table '%s' are not allowed",
cxt->stmtType, cxt->relname); cxt->stmtType, cxt->relname);
cxt->pkey = index; cxt->pkey = index;
...@@ -1291,8 +1291,8 @@ transformIndexConstraints(ParseState *pstate, CreateStmtContext *cxt) ...@@ -1291,8 +1291,8 @@ transformIndexConstraints(ParseState *pstate, CreateStmtContext *cxt)
elog(ERROR, "%s: failed to make implicit index name", elog(ERROR, "%s: failed to make implicit index name",
cxt->stmtType); cxt->stmtType);
elog(NOTICE, "%s/%s will create implicit index '%s' for table '%s'", elog(NOTICE, "%s / %s will create implicit index '%s' for table '%s'",
cxt->stmtType, (index->primary ? "PRIMARY KEY" : "UNIQUE"), cxt->stmtType, (index->primary ? "ADD PRIMARY KEY" : "ADD UNIQUE"),
index->idxname, cxt->relname); index->idxname, cxt->relname);
} }
} }
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.270 2001/10/23 02:50:41 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.271 2001/10/31 04:49:43 momjian Exp $
* *
* HISTORY * HISTORY
* AUTHOR DATE MAJOR EVENT * AUTHOR DATE MAJOR EVENT
...@@ -1300,7 +1300,7 @@ columnDef: ColId Typename ColQualList opt_collate ...@@ -1300,7 +1300,7 @@ columnDef: ColId Typename ColQualList opt_collate
n->constraints = $3; n->constraints = $3;
if ($4 != NULL) if ($4 != NULL)
elog(NOTICE,"CREATE TABLE/COLLATE %s not yet implemented" elog(NOTICE,"CREATE TABLE / COLLATE %s not yet implemented"
"; clause ignored", $4); "; clause ignored", $4);
$$ = (Node *)n; $$ = (Node *)n;
...@@ -1611,7 +1611,7 @@ CreateAsStmt: CREATE OptTemp TABLE relation_name OptCreateAs AS SelectStmt ...@@ -1611,7 +1611,7 @@ CreateAsStmt: CREATE OptTemp TABLE relation_name OptCreateAs AS SelectStmt
*/ */
SelectStmt *n = findLeftmostSelect((SelectStmt *) $7); SelectStmt *n = findLeftmostSelect((SelectStmt *) $7);
if (n->into != NULL) if (n->into != NULL)
elog(ERROR,"CREATE TABLE/AS SELECT may not specify INTO"); elog(ERROR,"CREATE TABLE / AS SELECT may not specify INTO");
n->istemp = $2; n->istemp = $2;
n->into = $4; n->into = $4;
if ($5 != NIL) if ($5 != NIL)
...@@ -2171,7 +2171,7 @@ FetchStmt: FETCH direction fetch_how_many from_in name ...@@ -2171,7 +2171,7 @@ FetchStmt: FETCH direction fetch_how_many from_in name
if ($2 == RELATIVE) if ($2 == RELATIVE)
{ {
if ($3 == 0) if ($3 == 0)
elog(ERROR,"FETCH/RELATIVE at current position is not supported"); elog(ERROR,"FETCH / RELATIVE at current position is not supported");
$2 = FORWARD; $2 = FORWARD;
} }
if ($3 < 0) if ($3 < 0)
...@@ -2299,7 +2299,7 @@ direction: FORWARD { $$ = FORWARD; } ...@@ -2299,7 +2299,7 @@ direction: FORWARD { $$ = FORWARD; }
| RELATIVE { $$ = RELATIVE; } | RELATIVE { $$ = RELATIVE; }
| ABSOLUTE | ABSOLUTE
{ {
elog(NOTICE,"FETCH/ABSOLUTE not supported, using RELATIVE"); elog(NOTICE,"FETCH / ABSOLUTE not supported, using RELATIVE");
$$ = RELATIVE; $$ = RELATIVE;
} }
; ;
...@@ -2605,12 +2605,12 @@ opt_arg: IN ...@@ -2605,12 +2605,12 @@ opt_arg: IN
} }
| OUT | OUT
{ {
elog(ERROR, "CREATE FUNCTION/OUT parameters are not supported"); elog(ERROR, "CREATE FUNCTION / OUT parameters are not supported");
$$ = TRUE; $$ = TRUE;
} }
| INOUT | INOUT
{ {
elog(ERROR, "CREATE FUNCTION/INOUT parameters are not supported"); elog(ERROR, "CREATE FUNCTION / INOUT parameters are not supported");
$$ = FALSE; $$ = FALSE;
} }
; ;
...@@ -2955,7 +2955,7 @@ opt_chain: AND NO CHAIN ...@@ -2955,7 +2955,7 @@ opt_chain: AND NO CHAIN
* if they don't support it. So we can't just ignore it. * if they don't support it. So we can't just ignore it.
* - thomas 2000-08-06 * - thomas 2000-08-06
*/ */
elog(ERROR, "COMMIT/CHAIN not yet supported"); elog(ERROR, "COMMIT / CHAIN not yet supported");
$$ = TRUE; $$ = TRUE;
} }
; ;
...@@ -6113,7 +6113,7 @@ mapTargetColumns(List *src, List *dst) ...@@ -6113,7 +6113,7 @@ mapTargetColumns(List *src, List *dst)
ResTarget *d; ResTarget *d;
if (length(src) != length(dst)) if (length(src) != length(dst))
elog(ERROR,"CREATE TABLE/AS SELECT has mismatched column count"); elog(ERROR,"CREATE TABLE / AS SELECT has mismatched column count");
while ((src != NIL) && (dst != NIL)) while ((src != NIL) && (dst != NIL))
{ {
......
/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/lib/Attic/connect.c,v 1.13 2001/10/25 05:50:11 momjian Exp $ */ /* $Header: /cvsroot/pgsql/src/interfaces/ecpg/lib/Attic/connect.c,v 1.14 2001/10/31 04:49:44 momjian Exp $ */
#include "postgres_fe.h" #include "postgres_fe.h"
...@@ -205,7 +205,7 @@ ECPGnoticeProcessor(void *arg, const char *message) ...@@ -205,7 +205,7 @@ ECPGnoticeProcessor(void *arg, const char *message)
/* these are harmless - do nothing */ /* these are harmless - do nothing */
/* /*
* NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index '*' * NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index '*'
* for table '*' * for table '*'
*/ */
......
...@@ -1087,11 +1087,11 @@ OptTemp: TEMPORARY { $$ = make_str("temporary"); } ...@@ -1087,11 +1087,11 @@ OptTemp: TEMPORARY { $$ = make_str("temporary"); }
| LOCAL TEMPORARY { $$ = make_str("local temporary"); } | LOCAL TEMPORARY { $$ = make_str("local temporary"); }
| LOCAL TEMP { $$ = make_str("local temp"); } | LOCAL TEMP { $$ = make_str("local temp"); }
| GLOBAL TEMPORARY { | GLOBAL TEMPORARY {
mmerror(ET_NOTICE, "Currently unsupported CREATE TABLE/GLOBAL TEMPORARY will be passed to backend"); mmerror(ET_NOTICE, "Currently unsupported CREATE TABLE / GLOBAL TEMPORARY will be passed to backend");
$$ = make_str("global temporary"); $$ = make_str("global temporary");
} }
| GLOBAL TEMP { | GLOBAL TEMP {
mmerror(ET_NOTICE, "Currently unsupported CREATE TABLE/GLOBAL TEMP will be passed to backend"); mmerror(ET_NOTICE, "Currently unsupported CREATE TABLE / GLOBAL TEMP will be passed to backend");
$$ = make_str("global temp"); $$ = make_str("global temp");
} }
| /*EMPTY*/ { $$ = EMPTY; } | /*EMPTY*/ { $$ = EMPTY; }
...@@ -1116,7 +1116,7 @@ columnDef: ColId Typename ColQualList opt_collate ...@@ -1116,7 +1116,7 @@ columnDef: ColId Typename ColQualList opt_collate
{ {
if (strlen($4) > 0) if (strlen($4) > 0)
{ {
sprintf(errortext, "Currently unsupported CREATE TABLE/COLLATE %s will be passed to backend", $4); sprintf(errortext, "Currently unsupported CREATE TABLE / COLLATE %s will be passed to backend", $4);
mmerror(ET_NOTICE, errortext); mmerror(ET_NOTICE, errortext);
} }
$$ = cat_str(4, $1, $2, $3, $4); $$ = cat_str(4, $1, $2, $3, $4);
...@@ -1278,7 +1278,7 @@ CreateAsStmt: CREATE OptTemp TABLE relation_name OptCreateAs AS ...@@ -1278,7 +1278,7 @@ CreateAsStmt: CREATE OptTemp TABLE relation_name OptCreateAs AS
{ FoundInto = 0; } SelectStmt { FoundInto = 0; } SelectStmt
{ {
if (FoundInto == 1) if (FoundInto == 1)
mmerror(ET_ERROR, "CREATE TABLE/AS SELECT may not specify INTO"); mmerror(ET_ERROR, "CREATE TABLE / AS SELECT may not specify INTO");
$$ = cat_str(7, make_str("create"), $2, make_str("table"), $4, $5, make_str("as"), $8); $$ = cat_str(7, make_str("create"), $2, make_str("table"), $4, $5, make_str("as"), $8);
} }
...@@ -2626,12 +2626,12 @@ OptTempTableName: TEMPORARY opt_table relation_name ...@@ -2626,12 +2626,12 @@ OptTempTableName: TEMPORARY opt_table relation_name
} }
| GLOBAL TEMPORARY opt_table relation_name | GLOBAL TEMPORARY opt_table relation_name
{ {
mmerror(ET_NOTICE, "Currently unsupported CREATE TABLE/GLOBAL TEMPORARY will be passed to backend"); mmerror(ET_NOTICE, "Currently unsupported CREATE TABLE / GLOBAL TEMPORARY will be passed to backend");
$$ = cat_str(3, make_str("global temporary"), $3, $4); $$ = cat_str(3, make_str("global temporary"), $3, $4);
} }
| GLOBAL TEMP opt_table relation_name | GLOBAL TEMP opt_table relation_name
{ {
mmerror(ET_NOTICE, "Currently unsupported CREATE TABLE/GLOBAL TEMP will be passed to backend"); mmerror(ET_NOTICE, "Currently unsupported CREATE TABLE / GLOBAL TEMP will be passed to backend");
$$ = cat_str(3, make_str("global temp"), $3, $4); $$ = cat_str(3, make_str("global temp"), $3, $4);
} }
| TABLE relation_name | TABLE relation_name
......
...@@ -271,10 +271,10 @@ SELECT unique1 FROM tenk1 WHERE unique1 < 5; ...@@ -271,10 +271,10 @@ SELECT unique1 FROM tenk1 WHERE unique1 < 5;
-- FOREIGN KEY CONSTRAINT adding TEST -- FOREIGN KEY CONSTRAINT adding TEST
CREATE TABLE tmp2 (a int primary key); CREATE TABLE tmp2 (a int primary key);
NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index 'tmp2_pkey' for table 'tmp2' NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'tmp2_pkey' for table 'tmp2'
CREATE TABLE tmp3 (a int, b int); CREATE TABLE tmp3 (a int, b int);
CREATE TABLE tmp4 (a int, b int, unique(a,b)); CREATE TABLE tmp4 (a int, b int, unique(a,b));
NOTICE: CREATE TABLE/UNIQUE will create implicit index 'tmp4_a_key' for table 'tmp4' NOTICE: CREATE TABLE / ADD UNIQUE will create implicit index 'tmp4_a_key' for table 'tmp4'
CREATE TABLE tmp5 (a int, b int); CREATE TABLE tmp5 (a int, b int);
-- Insert rows into tmp2 (pktable) -- Insert rows into tmp2 (pktable)
INSERT INTO tmp2 values (1); INSERT INTO tmp2 values (1);
...@@ -317,7 +317,7 @@ DROP TABLE tmp2; ...@@ -317,7 +317,7 @@ DROP TABLE tmp2;
-- Note: these tables are TEMP to avoid name conflicts when this test -- Note: these tables are TEMP to avoid name conflicts when this test
-- is run in parallel with foreign_key.sql. -- is run in parallel with foreign_key.sql.
CREATE TEMP TABLE PKTABLE (ptest1 int PRIMARY KEY); CREATE TEMP TABLE PKTABLE (ptest1 int PRIMARY KEY);
NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index 'pktable_pkey' for table 'pktable' NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'pktable_pkey' for table 'pktable'
CREATE TEMP TABLE FKTABLE (ftest1 text); CREATE TEMP TABLE FKTABLE (ftest1 text);
-- This next should fail, because text=int does not exist -- This next should fail, because text=int does not exist
ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable; 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,7 +345,7 @@ NOTICE: DROP TABLE implicitly drops referential integrity trigger from table "f
DROP TABLE fktable; DROP TABLE fktable;
CREATE TEMP TABLE PKTABLE (ptest1 int, ptest2 text, CREATE TEMP TABLE PKTABLE (ptest1 int, ptest2 text,
PRIMARY KEY(ptest1, ptest2)); PRIMARY KEY(ptest1, ptest2));
NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index 'pktable_pkey' for table 'pktable' NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'pktable_pkey' for table 'pktable'
-- This should fail, because we just chose really odd types -- This should fail, because we just chose really odd types
CREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 datetime); CREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 datetime);
ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2) references pktable; ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2) references pktable;
...@@ -448,3 +448,62 @@ insert into atacc3 (test2) values (3); ...@@ -448,3 +448,62 @@ insert into atacc3 (test2) values (3);
drop table atacc3; drop table atacc3;
drop table atacc2; drop table atacc2;
drop table atacc1; drop table atacc1;
-- test unique constraint adding
create table atacc1 ( test int );
-- add a unique constraint
alter table atacc1 add constraint atacc_test1 unique (test);
NOTICE: ALTER TABLE/UNIQUE will create implicit index 'atacc_test1' for table 'atacc1'
-- insert first value
insert into atacc1 (test) values (2);
-- should fail
insert into atacc1 (test) values (2);
ERROR: Cannot insert a duplicate key into unique index atacc_test1
-- should succeed
insert into atacc1 (test) values (4);
-- try adding a unique oid constraint
alter table atacc1 add constraint atacc_oid1 unique(oid);
NOTICE: ALTER TABLE/UNIQUE will create implicit index 'atacc_oid1' for table 'atacc1'
drop table atacc1;
-- let's do one where the unique constraint fails when added
create table atacc1 ( test int );
-- insert soon to be failing rows
insert into atacc1 (test) values (2);
insert into atacc1 (test) values (2);
-- add a unique constraint (fails)
alter table atacc1 add constraint atacc_test1 unique (test);
NOTICE: ALTER TABLE/UNIQUE will create implicit index 'atacc_test1' for table 'atacc1'
ERROR: Cannot create unique index. Table contains non-unique values
insert into atacc1 (test) values (3);
drop table atacc1;
-- let's do one where the unique contsraint fails
-- because the column doesn't exist
create table atacc1 ( test int );
-- add a unique constraint (fails)
alter table atacc1 add constraint atacc_test1 unique (test1);
ERROR: ALTER TABLE: column "test1" named in key does not exist
drop table atacc1;
-- something a little more complicated
create table atacc1 ( test int, test2 int);
-- add a unique constraint
alter table atacc1 add constraint atacc_test1 unique (test, test2);
NOTICE: ALTER TABLE/UNIQUE will create implicit index 'atacc_test1' for table 'atacc1'
-- insert initial value
insert into atacc1 (test,test2) values (4,4);
-- should fail
insert into atacc1 (test,test2) values (4,4);
ERROR: Cannot insert a duplicate key into unique index atacc_test1
-- should all succeed
insert into atacc1 (test,test2) values (4,5);
insert into atacc1 (test,test2) values (5,4);
insert into atacc1 (test,test2) values (5,5);
drop table atacc1;
-- lets do some naming tests
create table atacc1 (test int, test2 int, unique(test));
NOTICE: CREATE TABLE/UNIQUE will create implicit index 'atacc1_test_key' for table 'atacc1'
alter table atacc1 add unique (test2);
NOTICE: ALTER TABLE/UNIQUE will create implicit index 'atacc1_test2_key' for table 'atacc1'
-- should fail for @@ second one @@
insert into atacc1 (test2, test) values (3, 3);
insert into atacc1 (test2, test) values (2, 3);
ERROR: Cannot insert a duplicate key into unique index atacc1_test_key
drop table atacc1;
...@@ -137,7 +137,7 @@ INSERT INTO iportaltest (i, d, p) ...@@ -137,7 +137,7 @@ INSERT INTO iportaltest (i, d, p)
--- ---
CREATE TABLE serialTest (f1 text, f2 serial); CREATE TABLE serialTest (f1 text, f2 serial);
NOTICE: CREATE TABLE will create implicit sequence 'serialtest_f2_seq' for SERIAL column 'serialtest.f2' NOTICE: CREATE TABLE will create implicit sequence 'serialtest_f2_seq' for SERIAL column 'serialtest.f2'
NOTICE: CREATE TABLE/UNIQUE will create implicit index 'serialtest_f2_key' for table 'serialtest' NOTICE: CREATE TABLE / ADD UNIQUE will create implicit index 'serialtest_f2_key' for table 'serialtest'
INSERT INTO serialTest VALUES ('foo'); INSERT INTO serialTest VALUES ('foo');
INSERT INTO serialTest VALUES ('bar'); INSERT INTO serialTest VALUES ('bar');
INSERT INTO serialTest VALUES ('force', 100); INSERT INTO serialTest VALUES ('force', 100);
......
This diff is collapsed.
...@@ -286,7 +286,7 @@ SELECT * FROM COPY_TBL; ...@@ -286,7 +286,7 @@ SELECT * FROM COPY_TBL;
-- Primary keys -- Primary keys
-- --
CREATE TABLE PRIMARY_TBL (i int PRIMARY KEY, t text); CREATE TABLE PRIMARY_TBL (i int PRIMARY KEY, t text);
NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index 'primary_tbl_pkey' for table 'primary_tbl' NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'primary_tbl_pkey' for table 'primary_tbl'
INSERT INTO PRIMARY_TBL VALUES (1, 'one'); INSERT INTO PRIMARY_TBL VALUES (1, 'one');
INSERT INTO PRIMARY_TBL VALUES (2, 'two'); INSERT INTO PRIMARY_TBL VALUES (2, 'two');
INSERT INTO PRIMARY_TBL VALUES (1, 'three'); INSERT INTO PRIMARY_TBL VALUES (1, 'three');
...@@ -307,7 +307,7 @@ SELECT '' AS four, * FROM PRIMARY_TBL; ...@@ -307,7 +307,7 @@ SELECT '' AS four, * FROM PRIMARY_TBL;
DROP TABLE PRIMARY_TBL; DROP TABLE PRIMARY_TBL;
CREATE TABLE PRIMARY_TBL (i int, t text, CREATE TABLE PRIMARY_TBL (i int, t text,
PRIMARY KEY(i,t)); PRIMARY KEY(i,t));
NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index 'primary_tbl_pkey' for table 'primary_tbl' NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'primary_tbl_pkey' for table 'primary_tbl'
INSERT INTO PRIMARY_TBL VALUES (1, 'one'); INSERT INTO PRIMARY_TBL VALUES (1, 'one');
INSERT INTO PRIMARY_TBL VALUES (2, 'two'); INSERT INTO PRIMARY_TBL VALUES (2, 'two');
INSERT INTO PRIMARY_TBL VALUES (1, 'three'); INSERT INTO PRIMARY_TBL VALUES (1, 'three');
...@@ -330,7 +330,7 @@ DROP TABLE PRIMARY_TBL; ...@@ -330,7 +330,7 @@ DROP TABLE PRIMARY_TBL;
-- Unique keys -- Unique keys
-- --
CREATE TABLE UNIQUE_TBL (i int UNIQUE, t text); CREATE TABLE UNIQUE_TBL (i int UNIQUE, t text);
NOTICE: CREATE TABLE/UNIQUE will create implicit index 'unique_tbl_i_key' for table 'unique_tbl' NOTICE: CREATE TABLE / ADD UNIQUE will create implicit index 'unique_tbl_i_key' for table 'unique_tbl'
INSERT INTO UNIQUE_TBL VALUES (1, 'one'); INSERT INTO UNIQUE_TBL VALUES (1, 'one');
INSERT INTO UNIQUE_TBL VALUES (2, 'two'); INSERT INTO UNIQUE_TBL VALUES (2, 'two');
INSERT INTO UNIQUE_TBL VALUES (1, 'three'); INSERT INTO UNIQUE_TBL VALUES (1, 'three');
...@@ -353,7 +353,7 @@ SELECT '' AS five, * FROM UNIQUE_TBL; ...@@ -353,7 +353,7 @@ SELECT '' AS five, * FROM UNIQUE_TBL;
DROP TABLE UNIQUE_TBL; DROP TABLE UNIQUE_TBL;
CREATE TABLE UNIQUE_TBL (i int, t text, CREATE TABLE UNIQUE_TBL (i int, t text,
UNIQUE(i,t)); UNIQUE(i,t));
NOTICE: CREATE TABLE/UNIQUE will create implicit index 'unique_tbl_i_key' for table 'unique_tbl' NOTICE: CREATE TABLE / ADD UNIQUE will create implicit index 'unique_tbl_i_key' for table 'unique_tbl'
INSERT INTO UNIQUE_TBL VALUES (1, 'one'); INSERT INTO UNIQUE_TBL VALUES (1, 'one');
INSERT INTO UNIQUE_TBL VALUES (2, 'two'); INSERT INTO UNIQUE_TBL VALUES (2, 'two');
INSERT INTO UNIQUE_TBL VALUES (1, 'three'); INSERT INTO UNIQUE_TBL VALUES (1, 'three');
......
...@@ -329,3 +329,56 @@ drop table atacc3; ...@@ -329,3 +329,56 @@ drop table atacc3;
drop table atacc2; drop table atacc2;
drop table atacc1; drop table atacc1;
-- test unique constraint adding
create table atacc1 ( test int );
-- add a unique constraint
alter table atacc1 add constraint atacc_test1 unique (test);
-- insert first value
insert into atacc1 (test) values (2);
-- should fail
insert into atacc1 (test) values (2);
-- should succeed
insert into atacc1 (test) values (4);
-- try adding a unique oid constraint
alter table atacc1 add constraint atacc_oid1 unique(oid);
drop table atacc1;
-- let's do one where the unique constraint fails when added
create table atacc1 ( test int );
-- insert soon to be failing rows
insert into atacc1 (test) values (2);
insert into atacc1 (test) values (2);
-- add a unique constraint (fails)
alter table atacc1 add constraint atacc_test1 unique (test);
insert into atacc1 (test) values (3);
drop table atacc1;
-- let's do one where the unique contsraint fails
-- because the column doesn't exist
create table atacc1 ( test int );
-- add a unique constraint (fails)
alter table atacc1 add constraint atacc_test1 unique (test1);
drop table atacc1;
-- something a little more complicated
create table atacc1 ( test int, test2 int);
-- add a unique constraint
alter table atacc1 add constraint atacc_test1 unique (test, test2);
-- insert initial value
insert into atacc1 (test,test2) values (4,4);
-- should fail
insert into atacc1 (test,test2) values (4,4);
-- should all succeed
insert into atacc1 (test,test2) values (4,5);
insert into atacc1 (test,test2) values (5,4);
insert into atacc1 (test,test2) values (5,5);
drop table atacc1;
-- lets do some naming tests
create table atacc1 (test int, test2 int, unique(test));
alter table atacc1 add unique (test2);
-- should fail for @@ second one @@
insert into atacc1 (test2, test) values (3, 3);
insert into atacc1 (test2, test) values (2, 3);
drop table atacc1;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment