inherit.out

来自「postgresql8.3.4源码,开源数据库」· OUT 代码 · 共 695 行 · 第 1/2 页

OUT
695
字号
SELECT relname, a.* FROM ONLY a, pg_class where a.tableoid = pg_class.oid; relname |   aa   ---------+-------- a       | zzzz a       | zzzzz a       | zzzzzz a       | zzzzzz a       | zzzzzz a       | zzzzzz(6 rows)SELECT relname, b.* FROM ONLY b, pg_class where b.tableoid = pg_class.oid; relname | aa  | bb ---------+-----+---- b       | new |  b       | new |  b       | new |  b       | new |  b       | new |  b       | new | (6 rows)SELECT relname, c.* FROM ONLY c, pg_class where c.tableoid = pg_class.oid; relname |    aa    | cc ---------+----------+---- c       | ccc      |  c       | cccc     |  c       | ccccc    |  c       | cccccc   |  c       | ccccccc  |  c       | cccccccc | (6 rows)SELECT relname, d.* FROM ONLY d, pg_class where d.tableoid = pg_class.oid; relname | aa  | bb | cc | dd ---------+-----+----+----+---- d       | new |    |    |  d       | new |    |    |  d       | new |    |    |  d       | new |    |    |  d       | new |    |    |  d       | new |    |    | (6 rows)UPDATE a SET aa='new';DELETE FROM ONLY c WHERE aa='new';SELECT relname, a.* FROM a, pg_class where a.tableoid = pg_class.oid; relname | aa  ---------+----- a       | new a       | new a       | new a       | new a       | new a       | new b       | new b       | new b       | new b       | new b       | new b       | new d       | new d       | new d       | new d       | new d       | new d       | new(18 rows)SELECT relname, b.* FROM b, pg_class where b.tableoid = pg_class.oid; relname | aa  | bb ---------+-----+---- b       | new |  b       | new |  b       | new |  b       | new |  b       | new |  b       | new |  d       | new |  d       | new |  d       | new |  d       | new |  d       | new |  d       | new | (12 rows)SELECT relname, c.* FROM c, pg_class where c.tableoid = pg_class.oid; relname | aa  | cc ---------+-----+---- d       | new |  d       | new |  d       | new |  d       | new |  d       | new |  d       | new | (6 rows)SELECT relname, d.* FROM d, pg_class where d.tableoid = pg_class.oid; relname | aa  | bb | cc | dd ---------+-----+----+----+---- d       | new |    |    |  d       | new |    |    |  d       | new |    |    |  d       | new |    |    |  d       | new |    |    |  d       | new |    |    | (6 rows)SELECT relname, a.* FROM ONLY a, pg_class where a.tableoid = pg_class.oid; relname | aa  ---------+----- a       | new a       | new a       | new a       | new a       | new a       | new(6 rows)SELECT relname, b.* FROM ONLY b, pg_class where b.tableoid = pg_class.oid; relname | aa  | bb ---------+-----+---- b       | new |  b       | new |  b       | new |  b       | new |  b       | new |  b       | new | (6 rows)SELECT relname, c.* FROM ONLY c, pg_class where c.tableoid = pg_class.oid; relname | aa | cc ---------+----+----(0 rows)SELECT relname, d.* FROM ONLY d, pg_class where d.tableoid = pg_class.oid; relname | aa  | bb | cc | dd ---------+-----+----+----+---- d       | new |    |    |  d       | new |    |    |  d       | new |    |    |  d       | new |    |    |  d       | new |    |    |  d       | new |    |    | (6 rows)DELETE FROM a;SELECT relname, a.* FROM a, pg_class where a.tableoid = pg_class.oid; relname | aa ---------+----(0 rows)SELECT relname, b.* FROM b, pg_class where b.tableoid = pg_class.oid; relname | aa | bb ---------+----+----(0 rows)SELECT relname, c.* FROM c, pg_class where c.tableoid = pg_class.oid; relname | aa | cc ---------+----+----(0 rows)SELECT relname, d.* FROM d, pg_class where d.tableoid = pg_class.oid; relname | aa | bb | cc | dd ---------+----+----+----+----(0 rows)SELECT relname, a.* FROM ONLY a, pg_class where a.tableoid = pg_class.oid; relname | aa ---------+----(0 rows)SELECT relname, b.* FROM ONLY b, pg_class where b.tableoid = pg_class.oid; relname | aa | bb ---------+----+----(0 rows)SELECT relname, c.* FROM ONLY c, pg_class where c.tableoid = pg_class.oid; relname | aa | cc ---------+----+----(0 rows)SELECT relname, d.* FROM ONLY d, pg_class where d.tableoid = pg_class.oid; relname | aa | bb | cc | dd ---------+----+----+----+----(0 rows)-- Confirm PRIMARY KEY adds NOT NULL constraint to child tableCREATE TEMP TABLE z (b TEXT, PRIMARY KEY(aa, b)) inherits (a);NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "z_pkey" for table "z"INSERT INTO z VALUES (NULL, 'text'); -- should failERROR:  null value in column "aa" violates not-null constraint-- Check UPDATE with inherited target and an inherited source tablecreate temp table foo(f1 int, f2 int);create temp table foo2(f3 int) inherits (foo);create temp table bar(f1 int, f2 int);create temp table bar2(f3 int) inherits (bar);insert into foo values(1,1);insert into foo values(3,3);insert into foo2 values(2,2,2);insert into foo2 values(3,3,3);insert into bar values(1,1);insert into bar values(2,2);insert into bar values(3,3);insert into bar values(4,4);insert into bar2 values(1,1,1);insert into bar2 values(2,2,2);insert into bar2 values(3,3,3);insert into bar2 values(4,4,4);update bar set f2 = f2 + 100 where f1 in (select f1 from foo);SELECT relname, bar.* FROM bar, pg_class where bar.tableoid = pg_class.oidorder by 1,2; relname | f1 | f2  ---------+----+----- bar     |  1 | 101 bar     |  2 | 102 bar     |  3 | 103 bar     |  4 |   4 bar2    |  1 | 101 bar2    |  2 | 102 bar2    |  3 | 103 bar2    |  4 |   4(8 rows)/* Test inheritance of structure (LIKE) */CREATE TABLE inhx (xx text DEFAULT 'text');/* * Test double inheritance * * Ensure that defaults are NOT included unless * INCLUDING DEFAULTS is specified  */CREATE TABLE inhe (ee text, LIKE inhx) inherits (b);INSERT INTO inhe VALUES ('ee-col1', 'ee-col2', DEFAULT, 'ee-col4');SELECT * FROM inhe; /* Columns aa, bb, xx value NULL, ee */   aa    |   bb    | ee |   xx    ---------+---------+----+--------- ee-col1 | ee-col2 |    | ee-col4(1 row)SELECT * FROM inhx; /* Empty set since LIKE inherits structure only */ xx ----(0 rows)SELECT * FROM b; /* Has ee entry */   aa    |   bb    ---------+--------- ee-col1 | ee-col2(1 row)SELECT * FROM a; /* Has ee entry */   aa    --------- ee-col1(1 row)CREATE TABLE inhf (LIKE inhx, LIKE inhx); /* Throw error */ERROR:  column "xx" specified more than onceCREATE TABLE inhf (LIKE inhx INCLUDING DEFAULTS INCLUDING CONSTRAINTS);INSERT INTO inhf DEFAULT VALUES;SELECT * FROM inhf; /* Single entry with value 'text' */  xx  ------ text(1 row)ALTER TABLE inhx add constraint foo CHECK (xx = 'text');ALTER TABLE inhx ADD PRIMARY KEY (xx);NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "inhx_pkey" for table "inhx"CREATE TABLE inhg (LIKE inhx); /* Doesn't copy constraint */INSERT INTO inhg VALUES ('foo');DROP TABLE inhg;CREATE TABLE inhg (x text, LIKE inhx INCLUDING CONSTRAINTS, y text); /* Copies constraints */INSERT INTO inhg VALUES ('x', 'text', 'y'); /* Succeeds */INSERT INTO inhg VALUES ('x', 'text', 'y'); /* Succeeds -- Unique constraints not copied */INSERT INTO inhg VALUES ('x', 'foo',  'y');  /* fails due to constraint */ERROR:  new row for relation "inhg" violates check constraint "foo"SELECT * FROM inhg; /* Two records with three columns in order x=x, xx=text, y=y */ x |  xx  | y ---+------+--- x | text | y x | text | y(2 rows)DROP TABLE inhg;CREATE TABLE inhg (x text, LIKE inhx INCLUDING INDEXES, y text); /* copies indexes */NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "inhg_pkey" for table "inhg"INSERT INTO inhg VALUES (5, 10);INSERT INTO inhg VALUES (20, 10); -- should failERROR:  duplicate key value violates unique constraint "inhg_pkey"DROP TABLE inhg;/* Multiple primary keys creation should fail */CREATE TABLE inhg (x text, LIKE inhx INCLUDING INDEXES, PRIMARY KEY(x)); /* fails */ERROR:  multiple primary keys for table "inhg" are not allowedCREATE TABLE inhz (xx text DEFAULT 'text', yy int UNIQUE);NOTICE:  CREATE TABLE / UNIQUE will create implicit index "inhz_yy_key" for table "inhz"CREATE UNIQUE INDEX inhz_xx_idx on inhz (xx) WHERE xx <> 'test';/* Ok to create multiple unique indexes */CREATE TABLE inhg (x text UNIQUE, LIKE inhz INCLUDING INDEXES);NOTICE:  CREATE TABLE / UNIQUE will create implicit index "inhg_x_key" for table "inhg"NOTICE:  CREATE TABLE / UNIQUE will create implicit index "inhg_yy_key" for table "inhg"INSERT INTO inhg (xx, yy, x) VALUES ('test', 5, 10);INSERT INTO inhg (xx, yy, x) VALUES ('test', 10, 15);INSERT INTO inhg (xx, yy, x) VALUES ('foo', 10, 15); -- should failERROR:  duplicate key value violates unique constraint "inhg_x_key"DROP TABLE inhg;DROP TABLE inhz;-- Test changing the type of inherited columnsinsert into d values('test','one','two','three');alter table a alter column aa type integer using bit_length(aa);select * from d; aa | bb  | cc  |  dd   ----+-----+-----+------- 32 | one | two | three(1 row)-- Tests for casting between the rowtypes of parent and child-- tables. See the pgsql-hackers thread beginning Dec. 4/04create table base (i integer);create table derived () inherits (base);insert into derived (i) values (0);select derived::base from derived; derived --------- (0)(1 row)drop table derived;drop table base;create table p1(ff1 int);create table p2(f1 text);create function p2text(p2) returns text as 'select $1.f1' language sql;create table c1(f3 int) inherits(p1,p2);insert into c1 values(123456789, 'hi', 42);select p2text(c1.*) from c1; p2text -------- hi(1 row)drop function p2text(p2);drop table c1;drop table p2;drop table p1;

⌨️ 快捷键说明

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