📄 alter_table.sql
字号:
-- 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;-- Simple tests for alter table column typealter table foo alter f1 TYPE integer; -- failsalter table foo alter f1 TYPE varchar(10);create table anothertab (atcol1 serial8, atcol2 boolean, constraint anothertab_chk check (atcol1 <= 3));insert into anothertab (atcol1, atcol2) values (default, true);insert into anothertab (atcol1, atcol2) values (default, false);select * from anothertab;alter table anothertab alter column atcol1 type boolean; -- failsalter table anothertab alter column atcol1 type integer;select * from anothertab;insert into anothertab (atcol1, atcol2) values (45, null); -- failsinsert into anothertab (atcol1, atcol2) values (default, null);select * from anothertab;alter table anothertab alter column atcol2 type text using case when atcol2 is true then 'IT WAS TRUE' when atcol2 is false then 'IT WAS FALSE' else 'IT WAS NULL!' end;select * from anothertab;alter table anothertab alter column atcol1 type boolean using case when atcol1 % 2 = 0 then true else false end; -- failsalter table anothertab alter column atcol1 drop default;alter table anothertab alter column atcol1 type boolean using case when atcol1 % 2 = 0 then true else false end; -- failsalter table anothertab drop constraint anothertab_chk;alter table anothertab alter column atcol1 type boolean using case when atcol1 % 2 = 0 then true else false end;select * from anothertab;drop table anothertab;create table another (f1 int, f2 text);insert into another values(1, 'one');insert into another values(2, 'two');insert into another values(3, 'three');select * from another;alter table another alter f1 type text using f2 || ' more', alter f2 type bigint using f1 * 10;select * from another;drop table another;---- alter function--create function test_strict(text) returns text as 'select coalesce($1, ''got passed a null'');' language sql returns null on null input;select test_strict(NULL);alter function test_strict(text) called on null input;select test_strict(NULL);create function non_strict(text) returns text as 'select coalesce($1, ''got passed a null'');' language sql called on null input;select non_strict(NULL);alter function non_strict(text) returns null on null input;select non_strict(NULL);---- alter object set schema--create schema alter1;create schema alter2;create table alter1.t1(f1 serial primary key, f2 int check (f2 > 0));create view alter1.v1 as select * from alter1.t1;create function alter1.plus1(int) returns int as 'select $1+1' language sql;create domain alter1.posint integer check (value > 0);create type alter1.ctype as (f1 int, f2 text);insert into alter1.t1(f2) values(11);insert into alter1.t1(f2) values(12);alter table alter1.t1 set schema alter2;alter table alter1.v1 set schema alter2;alter function alter1.plus1(int) set schema alter2;alter domain alter1.posint set schema alter2;alter type alter1.ctype set schema alter2;-- this should succeed because nothing is left in alter1drop schema alter1;insert into alter2.t1(f2) values(13);insert into alter2.t1(f2) values(14);select * from alter2.t1;select * from alter2.v1;select alter2.plus1(41);-- clean updrop schema alter2 cascade;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -