alter_table.sql

来自「PostgreSQL7.4.6 for Linux」· SQL 代码 · 共 914 行 · 第 1/2 页

SQL
914
字号
---- 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 int4;ALTER TABLE tmp ADD COLUMN l tid;ALTER TABLE tmp ADD COLUMN m xid;ALTER TABLE tmp ADD COLUMN n oidvector;--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 timestamp;ALTER TABLE tmp ADD COLUMN w interval;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,	v, w, x, y, z)   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}', 	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)', '["epoch" "infinity"]',	'epoch', '01:00:10', '{1.0,2.0,3.0,4.0}', '{1.0,2.0,3.0,4.0}', '{1,2,3,4}');SELECT * FROM tmp;DROP TABLE tmp;-- the wolf bug - schema mods caused inconsistent row descriptors 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 int4;ALTER TABLE tmp ADD COLUMN l tid;ALTER TABLE tmp ADD COLUMN m xid;ALTER TABLE tmp ADD COLUMN n oidvector;--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 timestamp;ALTER TABLE tmp ADD COLUMN w interval;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,	v, w, x, y, z)   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}', 	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)', '["epoch" "infinity"]',	'epoch', '01:00:10', '{1.0,2.0,3.0,4.0}', '{1.0,2.0,3.0,4.0}', '{1,2,3,4}');SELECT * FROM tmp;DROP TABLE tmp;---- rename - check on both non-temp and temp tables--CREATE TABLE tmp (regtable int);CREATE TEMP TABLE tmp (tmptable int);ALTER TABLE tmp RENAME TO tmp_new;SELECT * FROM tmp;SELECT * FROM tmp_new;ALTER TABLE tmp RENAME TO tmp_new2;SELECT * FROM tmp;		-- should failSELECT * FROM tmp_new;SELECT * FROM tmp_new2;DROP TABLE tmp_new;DROP TABLE tmp_new2;-- ALTER TABLE ... RENAME on non-table relations-- renaming indexes (FIXME: this should probably test the index's functionality)ALTER TABLE onek_unique1 RENAME TO tmp_onek_unique1;ALTER TABLE tmp_onek_unique1 RENAME TO onek_unique1;-- renaming viewsCREATE VIEW tmp_view (unique1) AS SELECT unique1 FROM tenk1;ALTER TABLE tmp_view RENAME TO tmp_view_new;-- analyze to ensure we get an indexscan hereANALYZE tenk1;-- 5 values, sorted SELECT unique1 FROM tenk1 WHERE unique1 < 5;DROP VIEW tmp_view_new;-- toast-like relation namealter table stud_emp rename to pg_toast_stud_emp;alter table pg_toast_stud_emp rename to stud_emp;-- FOREIGN KEY CONSTRAINT adding TESTCREATE TABLE tmp2 (a int primary key);CREATE TABLE tmp3 (a int, b int);CREATE TABLE tmp4 (a int, b int, unique(a,b));CREATE TABLE tmp5 (a int, b int);-- 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 tmp3INSERT INTO tmp3 values (1,10);INSERT INTO tmp3 values (1,20);INSERT INTO tmp3 values (5,50);-- Try (and fail) to add constraint due to invalid source columnsALTER TABLE tmp3 add constraint tmpconstr foreign key(c) references tmp2 match full;-- Try (and fail) to add constraint due to invalide destination columns explicitly givenALTER TABLE tmp3 add constraint tmpconstr foreign key(a) references tmp2(b) match full;-- Try (and fail) to add constraint due to invalid dataALTER TABLE tmp3 add constraint tmpconstr foreign key (a) references tmp2 match full;-- Delete failing rowDELETE FROM tmp3 where a=5;-- Try (and succeed)ALTER TABLE tmp3 add constraint tmpconstr foreign key (a) references tmp2 match full;-- Try (and fail) to create constraint from tmp5(a) to tmp4(a) - unique constraint on-- tmp4 is a,bALTER TABLE tmp5 add constraint tmpconstr foreign key(a) references tmp4(a) match full;DROP TABLE tmp5;DROP TABLE tmp4;DROP TABLE tmp3;DROP TABLE tmp2;-- Foreign key adding test with mixed types-- 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);CREATE TEMP TABLE FKTABLE (ftest1 inet);-- This next should fail, because inet=int does not existALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;-- This should also fail for the same reason, but here we-- give the column nameALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable(ptest1);-- This should succeed, even though they are different types-- because varchar=int does existDROP TABLE FKTABLE;CREATE TEMP TABLE FKTABLE (ftest1 varchar);ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable;-- As should thisALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1) references pktable(ptest1);DROP TABLE pktable cascade;DROP TABLE fktable;CREATE TEMP TABLE PKTABLE (ptest1 int, ptest2 inet,                           PRIMARY KEY(ptest1, ptest2));-- This should fail, because we just chose really odd typesCREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 timestamp);ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2) references pktable;DROP TABLE FKTABLE;-- Again, so should this...CREATE TEMP TABLE FKTABLE (ftest1 cidr, ftest2 timestamp);ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2)     references pktable(ptest1, ptest2);DROP TABLE FKTABLE;-- This fails because we mixed up the column orderingCREATE TEMP TABLE FKTABLE (ftest1 int, ftest2 inet);ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2)     references pktable(ptest2, ptest1);-- As does this...ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest2, ftest1)     references pktable(ptest1, ptest2);-- temp tables should go away by themselves, need not drop them.-- test check constraint addingcreate table atacc1 ( test int );-- add a check constraintalter table atacc1 add constraint atacc_test1 check (test>3);-- should failinsert into atacc1 (test) values (2);-- should succeedinsert into atacc1 (test) values (4);drop table atacc1;-- let's do one where the check fails when addedcreate table atacc1 ( test int );-- insert a soon to be failing rowinsert into atacc1 (test) values (2);-- add a check constraint (fails)alter table atacc1 add constraint atacc_test1 check (test>3);insert into atacc1 (test) values (4);drop table atacc1;-- let's do one where the check fails because the column doesn't existcreate table atacc1 ( test int );-- add a check constraint (fails)alter table atacc1 add constraint atacc_test1 check (test1>3);drop table atacc1;-- something a little more complicatedcreate 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 failinsert into atacc1 (test,test2,test3) values (4,4,2);-- should succeedinsert into atacc1 (test,test2,test3) values (4,4,5);drop table atacc1;-- lets do some naming testscreate table atacc1 (test int check (test>3), test2 int);alter table atacc1 add check (test2>test);-- should fail for $2insert into atacc1 (test2, test) values (3, 4);drop table atacc1;-- inheritance related testscreate 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 atacc2insert into atacc2 (test2) values (-3);insert into atacc2 (test2) values (3);-- fail and then succeed on atacc3insert into atacc3 (test2) values (-3);insert into atacc3 (test2) values (3);drop table atacc3;drop table atacc2;drop table atacc1;-- let's try only to add only to the parentcreate 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 atacc2insert into atacc2 (test2) values (-3);insert into atacc2 (test2) values (3);-- both succeed on atacc3insert into atacc3 (test2) values (-3);insert into atacc3 (test2) values (3);drop table atacc3;drop table atacc2;drop table atacc1;-- test unique constraint addingcreate table atacc1 ( test int );-- add a unique constraintalter table atacc1 add constraint atacc_test1 unique (test);-- insert first valueinsert into atacc1 (test) values (2);-- should failinsert into atacc1 (test) values (2);-- should succeedinsert into atacc1 (test) values (4);-- try adding a unique oid constraintalter table atacc1 add constraint atacc_oid1 unique(oid);drop table atacc1;-- let's do one where the unique constraint fails when addedcreate table atacc1 ( test int );-- insert soon to be failing rowsinsert 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 constraint fails-- because the column doesn't existcreate 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 complicatedcreate table atacc1 ( test int, test2 int);-- add a unique constraintalter table atacc1 add constraint atacc_test1 unique (test, test2);-- insert initial valueinsert into atacc1 (test,test2) values (4,4);-- should failinsert into atacc1 (test,test2) values (4,4);-- should all succeedinsert 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 testscreate 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;-- test primary key constraint addingcreate table atacc1 ( test int );-- add a primary key constraintalter table atacc1 add constraint atacc_test1 primary key (test);-- insert first valueinsert into atacc1 (test) values (2);-- should failinsert into atacc1 (test) values (2);-- should succeedinsert into atacc1 (test) values (4);-- inserting NULL should failinsert into atacc1 (test) values(NULL);-- try adding a second primary key (should fail)alter table atacc1 add constraint atacc_oid1 primary key(oid);-- drop first primary key constraintalter table atacc1 drop constraint atacc_test1 restrict;-- try adding a primary key on oid (should succeed)alter table atacc1 add constraint atacc_oid1 primary key(oid);drop table atacc1;-- let's do one where the primary key constraint fails when addedcreate table atacc1 ( test int );-- insert soon to be failing rowsinsert into atacc1 (test) values (2);insert into atacc1 (test) values (2);-- add a primary key (fails)alter table atacc1 add constraint atacc_test1 primary key (test);insert into atacc1 (test) values (3);drop table atacc1;-- let's do another one where the primary key constraint fails when addedcreate table atacc1 ( test int );-- insert soon to be failing rowinsert into atacc1 (test) values (NULL);-- add a primary key (fails)alter table atacc1 add constraint atacc_test1 primary key (test);insert into atacc1 (test) values (3);drop table atacc1;-- let's do one where the primary key constraint fails-- because the column doesn't existcreate table atacc1 ( test int );-- add a primary key constraint (fails)alter table atacc1 add constraint atacc_test1 primary key (test1);drop table atacc1;-- something a little more complicatedcreate table atacc1 ( test int, test2 int);-- add a primary key constraintalter table atacc1 add constraint atacc_test1 primary key (test, test2);-- try adding a second primary key - should failalter table atacc1 add constraint atacc_test2 primary key (test);-- insert initial valueinsert into atacc1 (test,test2) values (4,4);-- should failinsert into atacc1 (test,test2) values (4,4);insert into atacc1 (test,test2) values (NULL,3);insert into atacc1 (test,test2) values (3, NULL);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?