alter_table.sql

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

SQL
914
字号
insert into atacc1 (test,test2) values (NULL,NULL);-- 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, primary key(test));-- only first should succeedinsert into atacc1 (test2, test) values (3, 3);insert into atacc1 (test2, test) values (2, 3);insert into atacc1 (test2, test) values (1, NULL);drop table atacc1;-- alter table / alter column [set/drop] not null tests-- try altering system catalogs, should failalter table pg_class alter column relname drop not null;alter table pg_class alter relname set not null;-- try altering non-existent table, should failalter table non_existent alter column bar set not null;alter table non_existent alter column bar drop not null;-- test setting columns to null and not null and vice versa-- test checking for null values and primary keycreate table atacc1 (test int not null);alter table atacc1 add constraint "atacc1_pkey" primary key (test);alter table atacc1 alter column test drop not null;alter table atacc1 drop constraint "atacc1_pkey";alter table atacc1 alter column test drop not null;insert into atacc1 values (null);alter table atacc1 alter test set not null;delete from atacc1;alter table atacc1 alter test set not null;-- try altering a non-existent column, should failalter table atacc1 alter bar set not null;alter table atacc1 alter bar drop not null;-- try altering the oid column, should failalter table atacc1 alter oid set not null;alter table atacc1 alter oid drop not null;-- try creating a view and altering that, should failcreate view myview as select * from atacc1;alter table myview alter column test drop not null;alter table myview alter column test set not null;drop view myview;drop table atacc1;-- test inheritancecreate table parent (a int);create table child (b varchar(255)) inherits (parent);alter table parent alter a set not null;insert into parent values (NULL);insert into child (a, b) values (NULL, 'foo');alter table parent alter a drop not null;insert into parent values (NULL);insert into child (a, b) values (NULL, 'foo');alter table only parent alter a set not null;alter table child alter a set not null;delete from parent;alter table only parent alter a set not null;insert into parent values (NULL);alter table child alter a set not null;insert into child (a, b) values (NULL, 'foo');delete from child;alter table child alter a set not null;insert into child (a, b) values (NULL, 'foo');drop table child;drop table parent;-- test setting and removing default valuescreate table def_test (	c1	int4 default 5,	c2	text default 'initial_default');insert into def_test default values;alter table def_test alter column c1 drop default;insert into def_test default values;alter table def_test alter column c2 drop default;insert into def_test default values;alter table def_test alter column c1 set default 10;alter table def_test alter column c2 set default 'new_default';insert into def_test default values;select * from def_test;-- set defaults to an incorrect type: this should failalter table def_test alter column c1 set default 'wrong_datatype';alter table def_test alter column c2 set default 20;-- set defaults on a non-existent column: this should failalter table def_test alter column c3 set default 30;-- set defaults on views: we need to create a view, add a rule-- to allow insertions into it, and then alter the view to add-- a defaultcreate view def_view_test as select * from def_test;create rule def_view_test_ins as	on insert to def_view_test	do instead insert into def_test select new.*;insert into def_view_test default values;alter table def_view_test alter column c1 set default 45;insert into def_view_test default values;alter table def_view_test alter column c2 set default 'view_default';insert into def_view_test default values;select * from def_view_test;drop rule def_view_test_ins on def_view_test;drop view def_view_test;drop table def_test;-- alter table / drop column tests-- try altering system catalogs, should failalter table pg_class drop column relname;-- try altering non-existent table, should failalter table nosuchtable drop column bar;-- test dropping columnscreate table atacc1 (a int4 not null, b int4, c int4 not null, d int4);insert into atacc1 values (1, 2, 3, 4);alter table atacc1 drop a;alter table atacc1 drop a;-- SELECTsselect * from atacc1;select * from atacc1 order by a;select * from atacc1 order by "........pg.dropped.1........";select * from atacc1 group by a;select * from atacc1 group by "........pg.dropped.1........";select atacc1.* from atacc1;select a from atacc1;select atacc1.a from atacc1;select b,c,d from atacc1;select a,b,c,d from atacc1;select * from atacc1 where a = 1;select "........pg.dropped.1........" from atacc1;select atacc1."........pg.dropped.1........" from atacc1;select "........pg.dropped.1........",b,c,d from atacc1;select * from atacc1 where "........pg.dropped.1........" = 1;-- UPDATEsupdate atacc1 set a = 3;update atacc1 set b = 2 where a = 3;update atacc1 set "........pg.dropped.1........" = 3;update atacc1 set b = 2 where "........pg.dropped.1........" = 3;-- INSERTsinsert into atacc1 values (10, 11, 12, 13);insert into atacc1 values (default, 11, 12, 13);insert into atacc1 values (11, 12, 13);insert into atacc1 (a) values (10);insert into atacc1 (a) values (default);insert into atacc1 (a,b,c,d) values (10,11,12,13);insert into atacc1 (a,b,c,d) values (default,11,12,13);insert into atacc1 (b,c,d) values (11,12,13);insert into atacc1 ("........pg.dropped.1........") values (10);insert into atacc1 ("........pg.dropped.1........") values (default);insert into atacc1 ("........pg.dropped.1........",b,c,d) values (10,11,12,13);insert into atacc1 ("........pg.dropped.1........",b,c,d) values (default,11,12,13);-- DELETEsdelete from atacc1 where a = 3;delete from atacc1 where "........pg.dropped.1........" = 3;delete from atacc1;-- try dropping a non-existent column, should failalter table atacc1 drop bar;-- try dropping the oid column, should failalter table atacc1 drop oid;-- try creating a view and altering that, should failcreate view myview as select * from atacc1;select * from myview;alter table myview drop d;drop view myview;-- test some commands to make sure they fail on the dropped columnanalyze atacc1(a);analyze atacc1("........pg.dropped.1........");vacuum analyze atacc1(a);vacuum analyze atacc1("........pg.dropped.1........");comment on column atacc1.a is 'testing';comment on column atacc1."........pg.dropped.1........" is 'testing';alter table atacc1 alter a set storage plain;alter table atacc1 alter "........pg.dropped.1........" set storage plain;alter table atacc1 alter a set statistics 0;alter table atacc1 alter "........pg.dropped.1........" set statistics 0;alter table atacc1 alter a set default 3;alter table atacc1 alter "........pg.dropped.1........" set default 3;alter table atacc1 alter a drop default;alter table atacc1 alter "........pg.dropped.1........" drop default;alter table atacc1 alter a set not null;alter table atacc1 alter "........pg.dropped.1........" set not null;alter table atacc1 alter a drop not null;alter table atacc1 alter "........pg.dropped.1........" drop not null;alter table atacc1 rename a to x;alter table atacc1 rename "........pg.dropped.1........" to x;alter table atacc1 add primary key(a);alter table atacc1 add primary key("........pg.dropped.1........");alter table atacc1 add unique(a);alter table atacc1 add unique("........pg.dropped.1........");alter table atacc1 add check (a > 3);alter table atacc1 add check ("........pg.dropped.1........" > 3);create table atacc2 (id int4 unique);alter table atacc1 add foreign key (a) references atacc2(id);alter table atacc1 add foreign key ("........pg.dropped.1........") references atacc2(id);alter table atacc2 add foreign key (id) references atacc1(a);alter table atacc2 add foreign key (id) references atacc1("........pg.dropped.1........");drop table atacc2;create index "testing_idx" on atacc1(a);create index "testing_idx" on atacc1("........pg.dropped.1........");-- test create as and select intoinsert into atacc1 values (21, 22, 23);create table test1 as select * from atacc1;select * from test1;drop table test1;select * into test2 from atacc1;select * from test2;drop table test2;-- try dropping all columnsalter table atacc1 drop c;alter table atacc1 drop d;alter table atacc1 drop b;select * from atacc1;drop table atacc1;-- test inheritancecreate table parent (a int, b int, c int);insert into parent values (1, 2, 3);alter table parent drop a;create table child (d varchar(255)) inherits (parent);insert into child values (12, 13, 'testing');select * from parent;select * from child;alter table parent drop c;select * from parent;select * from child;drop table child;drop table parent;-- test copy in/outcreate table test (a int4, b int4, c int4);insert into test values (1,2,3);alter table test drop a;copy test to stdout;copy test(a) to stdout;copy test("........pg.dropped.1........") to stdout;copy test from stdin;10	11	12\.select * from test;copy test from stdin;21	22\.select * from test;copy test(a) from stdin;copy test("........pg.dropped.1........") from stdin;copy test(b,c) from stdin;31	32\.select * from test;drop table test;-- test inheritancecreate table dropColumn (a int, b int, e int);create table dropColumnChild (c int) inherits (dropColumn);create table dropColumnAnother (d int) inherits (dropColumnChild);-- these two should failalter table dropColumnchild drop column a;alter table only dropColumnChild drop column b;-- these three should workalter table only dropColumn drop column e;alter table dropColumnChild drop column c;alter table dropColumn drop column a;create table renameColumn (a int);create table renameColumnChild (b int) inherits (renameColumn);create table renameColumnAnother (c int) inherits (renameColumnChild);-- these three should failalter table renameColumnChild rename column a to d;alter table only renameColumnChild rename column a to d;alter table only renameColumn rename column a to d;-- these should workalter table renameColumn rename column a to d;alter table renameColumnChild rename column b to a;-- this should workalter table renameColumn add column w int;-- this should failalter table only renameColumn add column x int;-- Test corner cases in dropping of inherited columnscreate table p1 (f1 int, f2 int);create table c1 (f1 int not null) inherits(p1);-- should be rejected since c1.f1 is inheritedalter table c1 drop column f1;-- should workalter table p1 drop column f1;-- c1.f1 is still there, but no longer inheritedselect f1 from c1;alter table c1 drop column f1;select f1 from c1;drop table p1 cascade;create table p1 (f1 int, f2 int);create table c1 () inherits(p1);-- should be rejected since c1.f1 is inheritedalter table c1 drop column f1;alter table p1 drop column f1;-- c1.f1 is dropped now, since there is no local definition for itselect f1 from c1;drop table p1 cascade;create table p1 (f1 int, f2 int);create table c1 () inherits(p1);-- should be rejected since c1.f1 is inheritedalter table c1 drop column f1;alter table only p1 drop column f1;-- c1.f1 is NOT dropped, but must now be considered non-inheritedalter table c1 drop column f1;drop table p1 cascade;create table p1 (f1 int, f2 int);create table c1 (f1 int not null) inherits(p1);-- should be rejected since c1.f1 is inheritedalter table c1 drop column f1;alter table only p1 drop column f1;-- c1.f1 is still there, but no longer inheritedalter table c1 drop column f1;drop table p1 cascade;create table p1(id int, name text);create table p2(id2 int, name text, height int);create table c1(age int) inherits(p1,p2);create table gc1() inherits (c1);select relname, attname, attinhcount, attislocalfrom pg_class join pg_attribute on (pg_class.oid = pg_attribute.attrelid)where relname in ('p1','p2','c1','gc1') and attnum > 0 and not attisdroppedorder by relname, attnum;-- should workalter table only p1 drop column name;-- should work. Now c1.name is local and inhcount is 0.alter table p2 drop column name;-- should be rejected since its inheritedalter table gc1 drop column name;-- should work, and drop gc1.name alongalter table c1 drop column name;-- should fail: column does not existalter table gc1 drop column name;-- should work and drop the attribute in all tablesalter table p2 drop column height;select relname, attname, attinhcount, attislocalfrom pg_class join pg_attribute on (pg_class.oid = pg_attribute.attrelid)where relname in ('p1','p2','c1','gc1') and attnum > 0 and not attisdroppedorder by relname, attnum;drop table p1, p2 cascade;---- Test the ALTER TABLE WITHOUT OIDS command--create table altstartwith (col integer) with oids;insert into altstartwith values (1);select oid > 0, * from altstartwith;alter table altstartwith set without oids;select oid > 0, * from altstartwith; -- failsselect * from altstartwith;-- Run inheritance testscreate table altwithoid (col integer) with oids;-- Inherits parents oid columncreate table altinhoid () inherits (altwithoid) without oids;insert into altinhoid values (1);select oid > 0, * from altwithoid;select oid > 0, * from altinhoid;alter table altwithoid set without oids;alter table altinhoid set without oids;select oid > 0, * from altwithoid; -- failsselect oid > 0, * from altinhoid; -- failsselect * from altwithoid;select * from altinhoid;-- test renumbering of child-table columns in inherited operationscreate table p1 (f1 int);create table c1 (f2 text, f3 int) inherits (p1);alter table p1 add column a1 int check (a1 > 0);alter table p1 add column f2 text;insert into p1 values (1,2,'abc');insert into c1 values(11,'xyz',33,0); -- should failinsert into c1 values(11,'xyz',33,22);select * from p1;update p1 set a1 = a1 + 1, f2 = upper(f2);select * from p1;drop table p1 cascade;-- test that operations with a dropped column do not try to reference-- its datatypecreate domain mytype as text;create temp table foo (f1 text, f2 mytype, f3 text);insert into foo values('aa','bb','cc');select * from foo;drop domain mytype cascade;select * from foo;insert into foo values('qq','rr');select * from foo;update foo set f3 = 'zz';select * from foo;select f3,max(f1) from foo group by f3;

⌨️ 快捷键说明

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