📄 alter_table.sql
字号:
insert 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 ) with oids;-- 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 ) with oids;-- 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);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) with oids;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) with oids;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 succeedalter table atacc1 drop oid;-- try dropping the xmin column, should failalter table atacc1 drop xmin;-- 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -