domain.out
来自「postgresql8.3.4源码,开源数据库」· OUT 代码 · 共 497 行 · 第 1/2 页
OUT
497 行
---- 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;NOTICE: type dependenttypetest depends on type domaindroptestERROR: cannot drop type domaindroptest because other objects depend on itHINT: Use DROP ... CASCADE to drop the dependent objects too.drop domain domaindroptest cascade;NOTICE: drop cascades to type dependenttypetest-- this should fail because already gonedrop domain domaindroptest cascade;ERROR: type "domaindroptest" does not exist-- 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); domainvarchar --------------- 12345(1 row)SELECT cast('12345' as domainvarchar); domainvarchar --------------- 12345(1 row)-- 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 varcharERROR: value too long for type character varying(5)INSERT INTO basictest values ('88', 'haha', 'short', '123.1212'); -- Truncate numeric-- Test copyCOPY basictest (testvarchar) FROM stdin; -- failERROR: value too long for type character varying(5)CONTEXT: COPY basictest, line 1, column testvarchar: "notsoshorttext"COPY basictest (testvarchar) FROM stdin;select * from basictest; testint4 | testtext | testvarchar | testnumeric ----------+----------+-------------+------------- 88 | haha | short | 123.12 88 | haha | short | 123.12 | | short | (3 rows)-- check that domains inherit operations from base typesselect testtext || testvarchar as concat, testnumeric + 42 as sumfrom basictest; concat | sum -----------+-------- hahashort | 165.12 hahashort | 165.12 | (3 rows)-- check that union/case/coalesce type resolution handles domains properlyselect coalesce(4::domainint4, 7) is of (int4) as t; t --- t(1 row)select coalesce(4::domainint4, 7) is of (domainint4) as f; f --- f(1 row)select coalesce(4::domainint4, 7::domainint4) is of (domainint4) as t; t --- t(1 row)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"}}');ERROR: value too long for type character varying(4)select * from domarrtest; testint4arr | testchar4arr ---------------+--------------------- {2,2} | {{a,b},{c,d}} {{2,2},{2,2}} | {{a,b}} {2,2} | {{a,b},{c,d},{e,f}} {2,2} | {{a},{c}} | {{a,b,c},{d,e,f}}(5 rows)select testint4arr[1], testchar4arr[2:2] from domarrtest; testint4arr | testchar4arr -------------+-------------- 2 | {{c,d}} | {} 2 | {{c,d}} 2 | {{c}} | {{d,e,f}}(5 rows)COPY domarrtest FROM stdin;COPY domarrtest FROM stdin; -- failERROR: value too long for type character varying(4)CONTEXT: COPY domarrtest, line 1, column testchar4arr: "{qwerty,w,e}"select * from domarrtest; testint4arr | testchar4arr ---------------+--------------------- {2,2} | {{a,b},{c,d}} {{2,2},{2,2}} | {{a,b}} {2,2} | {{a,b},{c,d},{e,f}} {2,2} | {{a},{c}} | {{a,b,c},{d,e,f}} {3,4} | {q,w,e} | (7 rows)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;ERROR: domain dnotnull does not allow null valuesINSERT INTO nulltest values ('a', 'b', 'c', 'd', 'c'); -- Goodinsert into nulltest values ('a', 'b', 'c', 'd', NULL);ERROR: domain dcheck does not allow null valuesinsert into nulltest values ('a', 'b', 'c', 'd', 'a');ERROR: new row for relation "nulltest" violates check constraint "nulltest_col5_check"INSERT INTO nulltest values (NULL, 'b', 'c', 'd', 'd');ERROR: domain dnotnull does not allow null valuesINSERT INTO nulltest values ('a', NULL, 'c', 'd', 'c');ERROR: domain dnotnull does not allow null valuesINSERT INTO nulltest values ('a', 'b', NULL, 'd', 'c');ERROR: null value in column "col3" violates not-null constraintINSERT INTO nulltest values ('a', 'b', 'c', NULL, 'd'); -- Good-- Test copyCOPY nulltest FROM stdin; --failERROR: null value in column "col3" violates not-null constraintCONTEXT: COPY nulltest, line 1: "a b \N d d"COPY nulltest FROM stdin; --failERROR: domain dcheck does not allow null valuesCONTEXT: COPY nulltest, line 1, column col5: null input-- Last row is badCOPY nulltest FROM stdin;ERROR: new row for relation "nulltest" violates check constraint "nulltest_col5_check"CONTEXT: COPY nulltest, line 3: "a b c \N a"select * from nulltest; col1 | col2 | col3 | col4 | col5 ------+------+------+------+------ a | b | c | d | c a | b | c | | d(2 rows)-- Test out coerced (casted) constraintsSELECT cast('1' as dnotnull); dnotnull ---------- 1(1 row)SELECT cast(NULL as dnotnull); -- failERROR: domain dnotnull does not allow null valuesSELECT cast(cast(NULL as dnull) as dnotnull); -- failERROR: domain dnotnull does not allow null valuesSELECT cast(col4 as dnotnull) from nulltest; -- failERROR: domain dnotnull does not allow null values-- 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 );NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "defaulttest_pkey" for table "defaulttest"insert into defaulttest(col4) values(0); -- fails, col5 defaults to nullERROR: null value in column "col5" violates not-null constraintalter 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); -- failsERROR: null value in column "col5" violates not-null constraintalter 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;select * from defaulttest; col1 | col2 | col3 | col4 | col5 | col6 | col7 | col8 ------+------+------+------+------+------+------+------- 3 | 12 | 5 | 1 | 3 | 88 | 8000 | 12.12 3 | 12 | 5 | 2 | 3 | 88 | 8000 | 12.12 3 | 12 | 5 | 3 | 3 | 88 | 8000 | 12.12 3 | 12 | 5 | 4 | 42 | 88 | 8000 | 12.12(4 rows)drop table defaulttest cascade;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?