domain.sql

来自「postgresql8.3.4源码,开源数据库」· SQL 代码 · 共 396 行

SQL
396
字号
---- Test domains.---- Test Comment / Dropcreate domain domaindroptest int4;comment on domain domaindroptest is 'About to drop this..';create domain dependenttypetest domaindroptest;-- fail because of dependent typedrop domain domaindroptest;drop domain domaindroptest cascade;-- this should fail because already gonedrop domain domaindroptest cascade;-- Test domain input.-- Note: the point of checking both INSERT and COPY FROM is that INSERT-- exercises CoerceToDomain while COPY exercises domain_in.create domain domainvarchar varchar(5);create domain domainnumeric numeric(8,2);create domain domainint4 int4;create domain domaintext text;-- Test explicit coercions --- these should succeed (and truncate)SELECT cast('123456' as domainvarchar);SELECT cast('12345' as domainvarchar);-- Test tables using domainscreate table basictest           ( testint4 domainint4           , testtext domaintext           , testvarchar domainvarchar           , testnumeric domainnumeric           );INSERT INTO basictest values ('88', 'haha', 'short', '123.12');      -- GoodINSERT INTO basictest values ('88', 'haha', 'short text', '123.12'); -- Bad varcharINSERT INTO basictest values ('88', 'haha', 'short', '123.1212');    -- Truncate numeric-- Test copyCOPY basictest (testvarchar) FROM stdin; -- failnotsoshorttext\.COPY basictest (testvarchar) FROM stdin;short\.select * from basictest;-- check that domains inherit operations from base typesselect testtext || testvarchar as concat, testnumeric + 42 as sumfrom basictest;-- check that union/case/coalesce type resolution handles domains properlyselect coalesce(4::domainint4, 7) is of (int4) as t;select coalesce(4::domainint4, 7) is of (domainint4) as f;select coalesce(4::domainint4, 7::domainint4) is of (domainint4) as t;drop table basictest;drop domain domainvarchar restrict;drop domain domainnumeric restrict;drop domain domainint4 restrict;drop domain domaintext;-- Test domains over array typescreate domain domainint4arr int4[1];create domain domainchar4arr varchar(4)[2][3];create table domarrtest           ( testint4arr domainint4arr           , testchar4arr domainchar4arr            );INSERT INTO domarrtest values ('{2,2}', '{{"a","b"},{"c","d"}}');INSERT INTO domarrtest values ('{{2,2},{2,2}}', '{{"a","b"}}');INSERT INTO domarrtest values ('{2,2}', '{{"a","b"},{"c","d"},{"e","f"}}');INSERT INTO domarrtest values ('{2,2}', '{{"a"},{"c"}}');INSERT INTO domarrtest values (NULL, '{{"a","b","c"},{"d","e","f"}}');INSERT INTO domarrtest values (NULL, '{{"toolong","b","c"},{"d","e","f"}}');select * from domarrtest;select testint4arr[1], testchar4arr[2:2] from domarrtest;COPY domarrtest FROM stdin;{3,4}	{q,w,e}\N	\N\.COPY domarrtest FROM stdin;	-- fail{3,4}	{qwerty,w,e}\.select * from domarrtest;drop table domarrtest;drop domain domainint4arr restrict;drop domain domainchar4arr restrict;create domain dnotnull varchar(15) NOT NULL;create domain dnull    varchar(15);create domain dcheck   varchar(15) NOT NULL CHECK (VALUE = 'a' OR VALUE = 'c' OR VALUE = 'd');create table nulltest           ( col1 dnotnull           , col2 dnotnull NULL  -- NOT NULL in the domain cannot be overridden           , col3 dnull    NOT NULL           , col4 dnull           , col5 dcheck CHECK (col5 IN ('c', 'd'))           );INSERT INTO nulltest DEFAULT VALUES;INSERT INTO nulltest values ('a', 'b', 'c', 'd', 'c');  -- Goodinsert into nulltest values ('a', 'b', 'c', 'd', NULL);insert into nulltest values ('a', 'b', 'c', 'd', 'a');INSERT INTO nulltest values (NULL, 'b', 'c', 'd', 'd');INSERT INTO nulltest values ('a', NULL, 'c', 'd', 'c');INSERT INTO nulltest values ('a', 'b', NULL, 'd', 'c');INSERT INTO nulltest values ('a', 'b', 'c', NULL, 'd'); -- Good-- Test copyCOPY nulltest FROM stdin; --faila	b	\N	d	d\.COPY nulltest FROM stdin; --faila	b	c	d	\N\.-- Last row is badCOPY nulltest FROM stdin;a	b	c	\N	ca	b	c	\N	da	b	c	\N	a\.select * from nulltest;-- Test out coerced (casted) constraintsSELECT cast('1' as dnotnull);SELECT cast(NULL as dnotnull); -- failSELECT cast(cast(NULL as dnull) as dnotnull); -- failSELECT cast(col4 as dnotnull) from nulltest; -- fail-- cleanupdrop table nulltest;drop domain dnotnull restrict;drop domain dnull restrict;drop domain dcheck restrict;create domain ddef1 int4 DEFAULT 3;create domain ddef2 oid DEFAULT '12';-- Type mixing, function returns int8create domain ddef3 text DEFAULT 5;create sequence ddef4_seq;create domain ddef4 int4 DEFAULT nextval('ddef4_seq');create domain ddef5 numeric(8,2) NOT NULL DEFAULT '12.12';create table defaulttest            ( col1 ddef1            , col2 ddef2            , col3 ddef3            , col4 ddef4 PRIMARY KEY            , col5 ddef1 NOT NULL DEFAULT NULL            , col6 ddef2 DEFAULT '88'            , col7 ddef4 DEFAULT 8000            , col8 ddef5            );insert into defaulttest(col4) values(0); -- fails, col5 defaults to nullalter table defaulttest alter column col5 drop default;insert into defaulttest default values; -- succeeds, inserts domain default-- We used to treat SET DEFAULT NULL as equivalent to DROP DEFAULT; wrongalter table defaulttest alter column col5 set default null;insert into defaulttest(col4) values(0); -- failsalter table defaulttest alter column col5 drop default;insert into defaulttest default values;insert into defaulttest default values;-- Test defaults with copyCOPY defaulttest(col5) FROM stdin;42\.select * from defaulttest;drop table defaulttest cascade;-- Test ALTER DOMAIN .. NOT NULLcreate domain dnotnulltest integer;create table domnotnull( col1 dnotnulltest, col2 dnotnulltest);insert into domnotnull default values;alter domain dnotnulltest set not null; -- failsupdate domnotnull set col1 = 5;alter domain dnotnulltest set not null; -- failsupdate domnotnull set col2 = 6;alter domain dnotnulltest set not null;update domnotnull set col1 = null; -- failsalter domain dnotnulltest drop not null;update domnotnull set col1 = null;drop domain dnotnulltest cascade;-- Test ALTER DOMAIN .. DEFAULT ..create table domdeftest (col1 ddef1);insert into domdeftest default values;select * from domdeftest;alter domain ddef1 set default '42';insert into domdeftest default values;select * from domdeftest;alter domain ddef1 drop default;insert into domdeftest default values;select * from domdeftest;drop table domdeftest;-- Test ALTER DOMAIN .. CONSTRAINT ..create domain con as integer;create table domcontest (col1 con);insert into domcontest values (1);insert into domcontest values (2);alter domain con add constraint t check (VALUE < 1); -- failsalter domain con add constraint t check (VALUE < 34);alter domain con add check (VALUE > 0);insert into domcontest values (-5); -- failsinsert into domcontest values (42); -- failsinsert into domcontest values (5);alter domain con drop constraint t;insert into domcontest values (-5); --failsinsert into domcontest values (42);-- Confirm ALTER DOMAIN with RULES.create table domtab (col1 integer);create domain dom as integer;create view domview as select cast(col1 as dom) from domtab;insert into domtab (col1) values (null);insert into domtab (col1) values (5);select * from domview;alter domain dom set not null;select * from domview; -- failalter domain dom drop not null;select * from domview;alter domain dom add constraint domchkgt6 check(value > 6);select * from domview; --failalter domain dom drop constraint domchkgt6 restrict;select * from domview;-- cleanupdrop domain ddef1 restrict;drop domain ddef2 restrict;drop domain ddef3 restrict;drop domain ddef4 restrict;drop domain ddef5 restrict;drop sequence ddef4_seq;-- Test domains over domainscreate domain vchar4 varchar(4);create domain dinter vchar4 check (substring(VALUE, 1, 1) = 'x');create domain dtop dinter check (substring(VALUE, 2, 1) = '1');select 'x123'::dtop;select 'x1234'::dtop; -- explicit coercion should truncateselect 'y1234'::dtop; -- failselect 'y123'::dtop; -- failselect 'yz23'::dtop; -- failselect 'xz23'::dtop; -- failcreate temp table dtest(f1 dtop);insert into dtest values('x123');insert into dtest values('x1234'); -- fail, implicit coercioninsert into dtest values('y1234'); -- fail, implicit coercioninsert into dtest values('y123'); -- failinsert into dtest values('yz23'); -- failinsert into dtest values('xz23'); -- faildrop table dtest;drop domain vchar4 cascade;-- Make sure that constraints of newly-added domain columns are-- enforced correctly, even if there's no default value for the new-- column. Per bug #1433create domain str_domain as text not null;create table domain_test (a int, b int);insert into domain_test values (1, 2);insert into domain_test values (1, 2);-- should failalter table domain_test add column c str_domain;create domain str_domain2 as text check (value <> 'foo') default 'foo';-- should failalter table domain_test add column d str_domain2;-- Check that domain constraints on prepared statement parameters of-- unknown type are enforced correctly.create domain pos_int as int4 check (value > 0) not null;prepare s1 as select $1::pos_int = 10 as "is_ten";execute s1(10);execute s1(0); -- should failexecute s1(NULL); -- should fail-- Check that domain constraints on plpgsql function parameters, results,-- and local variables are enforced correctly.create function doubledecrement(p1 pos_int) returns pos_int as $$declare v pos_int;begin    return p1;end$$ language plpgsql;select doubledecrement(3); -- fail because of implicit null assignmentcreate or replace function doubledecrement(p1 pos_int) returns pos_int as $$declare v pos_int := 0;begin    return p1;end$$ language plpgsql;select doubledecrement(3); -- fail at initialization assignmentcreate or replace function doubledecrement(p1 pos_int) returns pos_int as $$declare v pos_int := 1;begin    v := p1 - 1;    return v - 1;end$$ language plpgsql;select doubledecrement(null); -- fail before callselect doubledecrement(0); -- fail before callselect doubledecrement(1); -- fail at assignment to vselect doubledecrement(2); -- fail at returnselect doubledecrement(3); -- good-- Check that ALTER DOMAIN tests columns of derived typescreate domain posint as int4;-- Currently, this doesn't work for composite types, but verify it complainscreate type ddtest1 as (f1 posint);create table ddtest2(f1 ddtest1);insert into ddtest2 values(row(-1));alter domain posint add constraint c1 check(value >= 0);drop table ddtest2;create table ddtest2(f1 ddtest1[]);insert into ddtest2 values('{(-1)}');alter domain posint add constraint c1 check(value >= 0);drop table ddtest2;alter domain posint add constraint c1 check(value >= 0);create domain posint2 as posint check (value % 2 = 0);create table ddtest2(f1 posint2);insert into ddtest2 values(11); -- failinsert into ddtest2 values(-2); -- failinsert into ddtest2 values(2);alter domain posint add constraint c2 check(value >= 10); -- failalter domain posint add constraint c2 check(value > 0); -- OKdrop table ddtest2;drop type ddtest1;drop domain posint cascade;

⌨️ 快捷键说明

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