2000-01-09 04:48:39 +01:00
|
|
|
--
|
|
|
|
-- ALTER_TABLE
|
|
|
|
-- add attribute
|
|
|
|
--
|
|
|
|
CREATE TABLE tmp (initial int4);
|
|
|
|
ALTER TABLE tmp ADD COLUMN a int4;
|
|
|
|
ALTER TABLE tmp ADD COLUMN b name;
|
|
|
|
ALTER TABLE tmp ADD COLUMN c text;
|
|
|
|
ALTER TABLE tmp ADD COLUMN d float8;
|
|
|
|
ALTER TABLE tmp ADD COLUMN e float4;
|
|
|
|
ALTER TABLE tmp ADD COLUMN f int2;
|
|
|
|
ALTER TABLE tmp ADD COLUMN g polygon;
|
|
|
|
ALTER TABLE tmp ADD COLUMN h abstime;
|
|
|
|
ALTER TABLE tmp ADD COLUMN i char;
|
|
|
|
ALTER TABLE tmp ADD COLUMN j abstime[];
|
|
|
|
ALTER TABLE tmp ADD COLUMN k dt;
|
1999-02-23 08:31:02 +01:00
|
|
|
ERROR: Unable to locate type name 'dt' in catalog
|
2000-01-09 04:48:39 +01:00
|
|
|
ALTER TABLE tmp ADD COLUMN l tid;
|
|
|
|
ALTER TABLE tmp ADD COLUMN m xid;
|
2000-01-11 06:56:33 +01:00
|
|
|
ALTER TABLE tmp ADD COLUMN n oidvector;
|
2000-01-09 04:48:39 +01:00
|
|
|
--ALTER TABLE tmp ADD COLUMN o lock;
|
|
|
|
ALTER TABLE tmp ADD COLUMN p smgr;
|
|
|
|
ALTER TABLE tmp ADD COLUMN q point;
|
|
|
|
ALTER TABLE tmp ADD COLUMN r lseg;
|
|
|
|
ALTER TABLE tmp ADD COLUMN s path;
|
|
|
|
ALTER TABLE tmp ADD COLUMN t box;
|
|
|
|
ALTER TABLE tmp ADD COLUMN u tinterval;
|
|
|
|
ALTER TABLE tmp ADD COLUMN v datetime;
|
|
|
|
ALTER TABLE tmp ADD COLUMN w timespan;
|
|
|
|
ALTER TABLE tmp ADD COLUMN x float8[];
|
|
|
|
ALTER TABLE tmp ADD COLUMN y float4[];
|
|
|
|
ALTER TABLE tmp ADD COLUMN z int2[];
|
|
|
|
INSERT INTO tmp (a, b, c, d, e, f, g, h, i, j, k, l, m, n, p, q, r, s, t, u,
|
1997-04-29 16:23:51 +02:00
|
|
|
v, w, x, y, z)
|
2000-01-09 04:48:39 +01:00
|
|
|
VALUES (4, 'name', 'text', 4.1, 4.1, 2, '(4.1,4.1,3.1,3.1)',
|
|
|
|
'Mon May 1 00:30:30 1995', 'c', '{Mon May 1 00:30:30 1995, Monday Aug 24 14:43:07 1992, epoch}',
|
1997-04-29 16:23:51 +02:00
|
|
|
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)', '["current" "infinity"]',
|
1998-04-26 06:12:15 +02:00
|
|
|
'1/3', '1,name', '{1.0,2.0,3.0,4.0}', '{1.0,2.0,3.0,4.0}', '{1,2,3,4}');
|
1999-02-23 08:31:02 +01:00
|
|
|
ERROR: Relation 'tmp' does not have attribute 'k'
|
2000-01-09 04:48:39 +01:00
|
|
|
SELECT * FROM tmp;
|
|
|
|
initial | a | b | c | d | e | f | g | h | i | j | l | m | n | p | q | r | s | t | u | v | w | x | y | z
|
|
|
|
---------+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---
|
1997-04-29 16:23:51 +02:00
|
|
|
(0 rows)
|
|
|
|
|
2000-01-09 04:48:39 +01:00
|
|
|
DROP TABLE tmp;
|
|
|
|
-- the wolf bug - schema mods caused inconsistent row descriptors
|
|
|
|
CREATE TABLE tmp (
|
1997-04-29 16:23:51 +02:00
|
|
|
initial int4
|
1997-12-01 03:44:53 +01:00
|
|
|
);
|
2000-01-09 04:48:39 +01:00
|
|
|
ALTER TABLE tmp ADD COLUMN a int4;
|
|
|
|
ALTER TABLE tmp ADD COLUMN b name;
|
|
|
|
ALTER TABLE tmp ADD COLUMN c text;
|
|
|
|
ALTER TABLE tmp ADD COLUMN d float8;
|
|
|
|
ALTER TABLE tmp ADD COLUMN e float4;
|
|
|
|
ALTER TABLE tmp ADD COLUMN f int2;
|
|
|
|
ALTER TABLE tmp ADD COLUMN g polygon;
|
|
|
|
ALTER TABLE tmp ADD COLUMN h abstime;
|
|
|
|
ALTER TABLE tmp ADD COLUMN i char;
|
|
|
|
ALTER TABLE tmp ADD COLUMN j abstime[];
|
|
|
|
ALTER TABLE tmp ADD COLUMN k dt;
|
1999-02-23 08:31:02 +01:00
|
|
|
ERROR: Unable to locate type name 'dt' in catalog
|
2000-01-09 04:48:39 +01:00
|
|
|
ALTER TABLE tmp ADD COLUMN l tid;
|
|
|
|
ALTER TABLE tmp ADD COLUMN m xid;
|
2000-01-11 06:56:33 +01:00
|
|
|
ALTER TABLE tmp ADD COLUMN n oidvector;
|
2000-01-09 04:48:39 +01:00
|
|
|
--ALTER TABLE tmp ADD COLUMN o lock;
|
|
|
|
ALTER TABLE tmp ADD COLUMN p smgr;
|
|
|
|
ALTER TABLE tmp ADD COLUMN q point;
|
|
|
|
ALTER TABLE tmp ADD COLUMN r lseg;
|
|
|
|
ALTER TABLE tmp ADD COLUMN s path;
|
|
|
|
ALTER TABLE tmp ADD COLUMN t box;
|
|
|
|
ALTER TABLE tmp ADD COLUMN u tinterval;
|
|
|
|
ALTER TABLE tmp ADD COLUMN v datetime;
|
|
|
|
ALTER TABLE tmp ADD COLUMN w timespan;
|
|
|
|
ALTER TABLE tmp ADD COLUMN x float8[];
|
|
|
|
ALTER TABLE tmp ADD COLUMN y float4[];
|
|
|
|
ALTER TABLE tmp ADD COLUMN z int2[];
|
|
|
|
INSERT INTO tmp (a, b, c, d, e, f, g, h, i, j, k, l, m, n, p, q, r, s, t, u,
|
1997-04-29 16:23:51 +02:00
|
|
|
v, w, x, y, z)
|
2000-01-09 04:48:39 +01:00
|
|
|
VALUES (4, 'name', 'text', 4.1, 4.1, 2, '(4.1,4.1,3.1,3.1)',
|
1997-04-29 16:23:51 +02:00
|
|
|
'Mon May 1 00:30:30 1995', 'c', '{Mon May 1 00:30:30 1995, Monday Aug 24 14:43:07 1992, epoch}',
|
|
|
|
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)', '["current" "infinity"]',
|
1998-04-26 06:12:15 +02:00
|
|
|
'1/3', '1,name', '{1.0,2.0,3.0,4.0}', '{1.0,2.0,3.0,4.0}', '{1,2,3,4}');
|
1999-02-23 08:31:02 +01:00
|
|
|
ERROR: Relation 'tmp' does not have attribute 'k'
|
2000-01-09 04:48:39 +01:00
|
|
|
SELECT * FROM tmp;
|
|
|
|
initial | a | b | c | d | e | f | g | h | i | j | l | m | n | p | q | r | s | t | u | v | w | x | y | z
|
|
|
|
---------+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---
|
1997-04-29 16:23:51 +02:00
|
|
|
(0 rows)
|
|
|
|
|
2000-01-09 04:48:39 +01:00
|
|
|
DROP TABLE tmp;
|
|
|
|
--
|
|
|
|
-- rename -
|
2000-02-15 21:49:31 +01:00
|
|
|
-- should preserve indices, which we can check by seeing if a SELECT
|
|
|
|
-- chooses an indexscan; however, in the absence of vacuum statistics
|
|
|
|
-- it might not. Therefore, vacuum first.
|
2000-01-09 04:48:39 +01:00
|
|
|
--
|
2000-02-15 21:49:31 +01:00
|
|
|
VACUUM ANALYZE tenk1;
|
2000-01-09 04:48:39 +01:00
|
|
|
ALTER TABLE tenk1 RENAME TO ten_k;
|
|
|
|
-- 20 values, sorted
|
|
|
|
SELECT unique1 FROM ten_k WHERE unique1 < 20;
|
|
|
|
unique1
|
|
|
|
---------
|
|
|
|
0
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
|
6
|
|
|
|
7
|
|
|
|
8
|
|
|
|
9
|
|
|
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
1997-04-29 16:23:51 +02:00
|
|
|
(20 rows)
|
|
|
|
|
2000-01-09 04:48:39 +01:00
|
|
|
-- 20 values, sorted
|
|
|
|
SELECT unique2 FROM ten_k WHERE unique2 < 20;
|
|
|
|
unique2
|
|
|
|
---------
|
|
|
|
0
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
|
6
|
|
|
|
7
|
|
|
|
8
|
|
|
|
9
|
|
|
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
1997-04-29 16:23:51 +02:00
|
|
|
(20 rows)
|
|
|
|
|
2000-01-09 04:48:39 +01:00
|
|
|
-- 100 values, sorted
|
|
|
|
SELECT hundred FROM ten_k WHERE hundred = 50;
|
|
|
|
hundred
|
|
|
|
---------
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
|
|
|
50
|
1997-04-29 16:23:51 +02:00
|
|
|
(100 rows)
|
|
|
|
|
2000-01-09 04:48:39 +01:00
|
|
|
ALTER TABLE ten_k RENAME TO tenk1;
|
|
|
|
-- 5 values, sorted
|
|
|
|
SELECT unique1 FROM tenk1 WHERE unique1 < 5;
|
|
|
|
unique1
|
|
|
|
---------
|
|
|
|
0
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
1997-04-29 16:23:51 +02:00
|
|
|
(5 rows)
|
|
|
|
|
2000-02-22 21:58:31 +01:00
|
|
|
-- FOREIGN KEY CONSTRAINT adding TEST
|
|
|
|
CREATE TABLE tmp2 (a int primary key);
|
2001-10-31 06:14:33 +01:00
|
|
|
NOTICE: CREATE TABLE / ADD PRIMARY KEY will create implicit index 'tmp2_pkey' for table 'tmp2'
|
2000-02-22 21:58:31 +01:00
|
|
|
CREATE TABLE tmp3 (a int, b int);
|
2000-12-05 20:57:56 +01:00
|
|
|
CREATE TABLE tmp4 (a int, b int, unique(a,b));
|
2001-10-31 05:49:44 +01:00
|
|
|
NOTICE: CREATE TABLE / ADD UNIQUE will create implicit index 'tmp4_a_key' for table 'tmp4'
|
2000-12-05 20:57:56 +01:00
|
|
|
CREATE TABLE tmp5 (a int, b int);
|
2000-02-22 21:58:31 +01:00
|
|
|
-- Insert rows into tmp2 (pktable)
|
|
|
|
INSERT INTO tmp2 values (1);
|
|
|
|
INSERT INTO tmp2 values (2);
|
|
|
|
INSERT INTO tmp2 values (3);
|
|
|
|
INSERT INTO tmp2 values (4);
|
|
|
|
-- Insert rows into tmp3
|
|
|
|
INSERT INTO tmp3 values (1,10);
|
|
|
|
INSERT INTO tmp3 values (1,20);
|
|
|
|
INSERT INTO tmp3 values (5,50);
|
2000-08-29 06:20:47 +02:00
|
|
|
-- Try (and fail) to add constraint due to invalid source columns
|
|
|
|
ALTER TABLE tmp3 add constraint tmpconstr foreign key(c) references tmp2 match full;
|
2001-10-12 02:07:15 +02:00
|
|
|
NOTICE: ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
|
|
|
|
ERROR: ALTER TABLE: column "c" referenced in foreign key constraint does not exist
|
2000-08-29 06:20:47 +02:00
|
|
|
-- Try (and fail) to add constraint due to invalide destination columns explicitly given
|
|
|
|
ALTER TABLE tmp3 add constraint tmpconstr foreign key(a) references tmp2(b) match full;
|
2001-10-12 02:07:15 +02:00
|
|
|
NOTICE: ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
|
2000-08-29 06:20:47 +02:00
|
|
|
ERROR: UNIQUE constraint matching given keys for referenced table "tmp2" not found
|
2000-02-22 21:58:31 +01:00
|
|
|
-- Try (and fail) to add constraint due to invalid data
|
|
|
|
ALTER TABLE tmp3 add constraint tmpconstr foreign key (a) references tmp2 match full;
|
2001-10-12 02:07:15 +02:00
|
|
|
NOTICE: ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
|
2000-07-16 21:27:29 +02:00
|
|
|
ERROR: tmpconstr referential integrity violation - key referenced from tmp3 not found in tmp2
|
2000-02-22 21:58:31 +01:00
|
|
|
-- Delete failing row
|
|
|
|
DELETE FROM tmp3 where a=5;
|
|
|
|
-- Try (and succeed)
|
|
|
|
ALTER TABLE tmp3 add constraint tmpconstr foreign key (a) references tmp2 match full;
|
2001-10-12 02:07:15 +02:00
|
|
|
NOTICE: ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
|
2000-12-05 20:57:56 +01:00
|
|
|
-- Try (and fail) to create constraint from tmp5(a) to tmp4(a) - unique constraint on
|
|
|
|
-- tmp4 is a,b
|
|
|
|
ALTER TABLE tmp5 add constraint tmpconstr foreign key(a) references tmp4(a) match full;
|
2001-10-12 02:07:15 +02:00
|
|
|
NOTICE: ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
|
2000-12-05 20:57:56 +01:00
|
|
|
ERROR: UNIQUE constraint matching given keys for referenced table "tmp4" not found
|
|
|
|
DROP TABLE tmp5;
|
|
|
|
DROP TABLE tmp4;
|
2000-03-15 00:06:59 +01:00
|
|
|
DROP TABLE tmp3;
|
|
|
|
NOTICE: DROP TABLE implicitly drops referential integrity trigger from table "tmp2"
|
|
|
|
NOTICE: DROP TABLE implicitly drops referential integrity trigger from table "tmp2"
|
|
|
|
DROP TABLE tmp2;
|
2001-05-09 23:10:39 +02:00
|
|
|
-- Foreign key adding test with mixed types
|
2001-05-11 07:09:03 +02:00
|
|
|
-- Note: these tables are TEMP to avoid name conflicts when this test
|
|
|
|
-- is run in parallel with foreign_key.sql.
|
|
|
|
CREATE TEMP TABLE PKTABLE (ptest1 int PRIMARY KEY);
|
2001-10-31 06:14:33 +01:00
|
|
|
NOTICE: CREATE TABLE / ADD PRIMARY KEY will create implicit index 'pktable_pkey' for table 'pktable'
|
2001-05-11 07:09:03 +02:00
|
|
|
CREATE TEMP TABLE FKTABLE (ftest1 text);
|
2001-05-09 23:10:39 +02:00
|
|
|
-- This next should fail, because text=int does not exist
|
|
|
|
ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
|
2001-10-12 02:07:15 +02:00
|
|
|
NOTICE: ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
|
2001-08-09 20:28:18 +02:00
|
|
|
ERROR: Unable to identify an operator '=' for types 'text' and 'integer'
|
2001-05-09 23:10:39 +02:00
|
|
|
You will have to retype this query using an explicit cast
|
|
|
|
-- This should also fail for the same reason, but here we
|
|
|
|
-- give the column name
|
|
|
|
ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable(ptest1);
|
2001-10-12 02:07:15 +02:00
|
|
|
NOTICE: ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
|
2001-08-09 20:28:18 +02:00
|
|
|
ERROR: Unable to identify an operator '=' for types 'text' and 'integer'
|
2001-05-09 23:10:39 +02:00
|
|
|
You will have to retype this query using an explicit cast
|
|
|
|
-- This should succeed, even though they are different types
|
|
|
|
-- because varchar=int does exist
|
|
|
|
DROP TABLE FKTABLE;
|
2001-05-11 07:09:03 +02:00
|
|
|
CREATE TEMP TABLE FKTABLE (ftest1 varchar);
|
2001-05-09 23:10:39 +02:00
|
|
|
ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;
|
2001-10-12 02:07:15 +02:00
|
|
|
NOTICE: ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
|
2001-05-09 23:10:39 +02:00
|
|
|
-- As should this
|
|
|
|
ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable(ptest1);
|
2001-10-12 02:07:15 +02:00
|
|
|
NOTICE: ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
|
2001-05-09 23:10:39 +02:00
|
|
|
DROP TABLE pktable;
|
|
|
|
NOTICE: DROP TABLE implicitly drops referential integrity trigger from table "fktable"
|
|
|
|
NOTICE: DROP TABLE implicitly drops referential integrity trigger from table "fktable"
|
|
|
|
DROP TABLE fktable;
|
2001-05-11 07:09:03 +02:00
|
|
|
CREATE TEMP TABLE PKTABLE (ptest1 int, ptest2 text,
|
|
|
|
PRIMARY KEY(ptest1, ptest2));
|
2001-10-31 06:14:33 +01:00
|
|
|
NOTICE: CREATE TABLE / ADD PRIMARY KEY will create implicit index 'pktable_pkey' for table 'pktable'
|
2001-05-09 23:10:39 +02:00
|
|
|
-- This should fail, because we just chose really odd types
|
2001-05-11 07:09:03 +02:00
|
|
|
CREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 datetime);
|
2001-05-09 23:10:39 +02:00
|
|
|
ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2) references pktable;
|
2001-10-12 02:07:15 +02:00
|
|
|
NOTICE: ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
|
2001-08-09 20:28:18 +02:00
|
|
|
ERROR: Unable to identify an operator '=' for types 'cidr' and 'integer'
|
2001-05-09 23:10:39 +02:00
|
|
|
You will have to retype this query using an explicit cast
|
|
|
|
-- Again, so should this...
|
|
|
|
DROP TABLE FKTABLE;
|
2001-05-11 07:09:03 +02:00
|
|
|
CREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 datetime);
|
|
|
|
ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2)
|
|
|
|
references pktable(ptest1, ptest2);
|
2001-10-12 02:07:15 +02:00
|
|
|
NOTICE: ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
|
2001-08-09 20:28:18 +02:00
|
|
|
ERROR: Unable to identify an operator '=' for types 'cidr' and 'integer'
|
2001-05-09 23:10:39 +02:00
|
|
|
You will have to retype this query using an explicit cast
|
|
|
|
-- This fails because we mixed up the column ordering
|
|
|
|
DROP TABLE FKTABLE;
|
2001-05-11 07:09:03 +02:00
|
|
|
CREATE TEMP TABLE FKTABLE (ftest1 int, ftest2 text);
|
|
|
|
ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2)
|
|
|
|
references pktable(ptest2, ptest1);
|
2001-10-12 02:07:15 +02:00
|
|
|
NOTICE: ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
|
2001-08-09 20:28:18 +02:00
|
|
|
ERROR: Unable to identify an operator '=' for types 'integer' and 'text'
|
2001-05-09 23:10:39 +02:00
|
|
|
You will have to retype this query using an explicit cast
|
|
|
|
-- As does this...
|
2001-05-11 07:09:03 +02:00
|
|
|
ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest2, ftest1)
|
|
|
|
references pktable(ptest1, ptest2);
|
2001-10-12 02:07:15 +02:00
|
|
|
NOTICE: ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
|
2001-08-09 20:28:18 +02:00
|
|
|
ERROR: Unable to identify an operator '=' for types 'text' and 'integer'
|
2001-05-09 23:10:39 +02:00
|
|
|
You will have to retype this query using an explicit cast
|
2001-05-11 07:09:03 +02:00
|
|
|
-- temp tables should go away by themselves, need not drop them.
|
2001-05-30 15:00:03 +02:00
|
|
|
-- test check constraint adding
|
|
|
|
create table atacc1 ( test int );
|
|
|
|
-- add a check constraint
|
|
|
|
alter table atacc1 add constraint atacc_test1 check (test>3);
|
|
|
|
-- should fail
|
|
|
|
insert into atacc1 (test) values (2);
|
|
|
|
ERROR: ExecAppend: rejected due to CHECK constraint atacc_test1
|
|
|
|
-- should succeed
|
|
|
|
insert into atacc1 (test) values (4);
|
|
|
|
drop table atacc1;
|
|
|
|
-- let's do one where the check fails when added
|
|
|
|
create table atacc1 ( test int );
|
|
|
|
-- insert a soon to be failing row
|
|
|
|
insert into atacc1 (test) values (2);
|
|
|
|
-- add a check constraint (fails)
|
|
|
|
alter table atacc1 add constraint atacc_test1 check (test>3);
|
|
|
|
ERROR: AlterTableAddConstraint: rejected due to CHECK constraint atacc_test1
|
|
|
|
insert into atacc1 (test) values (4);
|
|
|
|
drop table atacc1;
|
|
|
|
-- let's do one where the check fails because the column doesn't exist
|
|
|
|
create table atacc1 ( test int );
|
|
|
|
-- add a check constraint (fails)
|
|
|
|
alter table atacc1 add constraint atacc_test1 check (test1>3);
|
|
|
|
ERROR: Attribute 'test1' not found
|
|
|
|
drop table atacc1;
|
|
|
|
-- something a little more complicated
|
|
|
|
create table atacc1 ( test int, test2 int, test3 int);
|
|
|
|
-- add a check constraint (fails)
|
|
|
|
alter table atacc1 add constraint atacc_test1 check (test+test2<test3*4);
|
|
|
|
-- should fail
|
|
|
|
insert into atacc1 (test,test2,test3) values (4,4,2);
|
|
|
|
ERROR: ExecAppend: rejected due to CHECK constraint atacc_test1
|
|
|
|
-- should succeed
|
|
|
|
insert into atacc1 (test,test2,test3) values (4,4,5);
|
|
|
|
drop table atacc1;
|
|
|
|
-- lets do some naming tests
|
|
|
|
create table atacc1 (test int check (test>3), test2 int);
|
|
|
|
alter table atacc1 add check (test2>test);
|
|
|
|
-- should fail for $2
|
|
|
|
insert into atacc1 (test2, test) values (3, 4);
|
|
|
|
ERROR: ExecAppend: rejected due to CHECK constraint $2
|
|
|
|
drop table atacc1;
|
|
|
|
-- inheritance related tests
|
|
|
|
create table atacc1 (test int);
|
|
|
|
create table atacc2 (test2 int);
|
|
|
|
create table atacc3 (test3 int) inherits (atacc1, atacc2);
|
|
|
|
alter table atacc2 add constraint foo check (test2>0);
|
|
|
|
-- fail and then succeed on atacc2
|
|
|
|
insert into atacc2 (test2) values (-3);
|
|
|
|
ERROR: ExecAppend: rejected due to CHECK constraint foo
|
|
|
|
insert into atacc2 (test2) values (3);
|
|
|
|
-- fail and then succeed on atacc3
|
|
|
|
insert into atacc3 (test2) values (-3);
|
|
|
|
ERROR: ExecAppend: rejected due to CHECK constraint foo
|
|
|
|
insert into atacc3 (test2) values (3);
|
|
|
|
drop table atacc3;
|
|
|
|
drop table atacc2;
|
|
|
|
drop table atacc1;
|
|
|
|
-- let's try only to add only to the parent
|
|
|
|
create table atacc1 (test int);
|
|
|
|
create table atacc2 (test2 int);
|
|
|
|
create table atacc3 (test3 int) inherits (atacc1, atacc2);
|
|
|
|
alter table only atacc2 add constraint foo check (test2>0);
|
|
|
|
-- fail and then succeed on atacc2
|
|
|
|
insert into atacc2 (test2) values (-3);
|
|
|
|
ERROR: ExecAppend: rejected due to CHECK constraint foo
|
|
|
|
insert into atacc2 (test2) values (3);
|
|
|
|
-- both succeed on atacc3
|
|
|
|
insert into atacc3 (test2) values (-3);
|
|
|
|
insert into atacc3 (test2) values (3);
|
|
|
|
drop table atacc3;
|
|
|
|
drop table atacc2;
|
|
|
|
drop table atacc1;
|
2001-10-31 05:49:44 +01:00
|
|
|
-- test unique constraint adding
|
|
|
|
create table atacc1 ( test int );
|
|
|
|
-- add a unique constraint
|
|
|
|
alter table atacc1 add constraint atacc_test1 unique (test);
|
2001-10-31 06:14:33 +01:00
|
|
|
NOTICE: ALTER TABLE / ADD UNIQUE will create implicit index 'atacc_test1' for table 'atacc1'
|
2001-10-31 05:49:44 +01:00
|
|
|
-- 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);
|
2001-10-31 06:14:33 +01:00
|
|
|
NOTICE: ALTER TABLE / ADD UNIQUE will create implicit index 'atacc_oid1' for table 'atacc1'
|
2001-10-31 05:49:44 +01:00
|
|
|
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);
|
2001-10-31 06:14:33 +01:00
|
|
|
NOTICE: ALTER TABLE / ADD UNIQUE will create implicit index 'atacc_test1' for table 'atacc1'
|
2001-10-31 05:49:44 +01:00
|
|
|
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);
|
2001-10-31 06:14:33 +01:00
|
|
|
NOTICE: ALTER TABLE / ADD UNIQUE will create implicit index 'atacc_test1' for table 'atacc1'
|
2001-10-31 05:49:44 +01:00
|
|
|
-- 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));
|
2001-10-31 06:14:33 +01:00
|
|
|
NOTICE: CREATE TABLE / ADD UNIQUE will create implicit index 'atacc1_test_key' for table 'atacc1'
|
2001-10-31 05:49:44 +01:00
|
|
|
alter table atacc1 add unique (test2);
|
2001-10-31 06:14:33 +01:00
|
|
|
NOTICE: ALTER TABLE / ADD UNIQUE will create implicit index 'atacc1_test2_key' for table 'atacc1'
|
2001-10-31 05:49:44 +01:00
|
|
|
-- 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;
|