⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rules.sql

📁 关系型数据库 Postgresql 6.5.2
💻 SQL
📖 第 1 页 / 共 2 页
字号:
---- Check that the ordering of rules fired is correct--insert into rtest_order1 values (1);select * from rtest_order2;---- Check if instead nothing w/without qualification works--insert into rtest_nothn1 values (1, 'want this');insert into rtest_nothn1 values (2, 'want this');insert into rtest_nothn1 values (10, 'don''t want this');insert into rtest_nothn1 values (19, 'don''t want this');insert into rtest_nothn1 values (20, 'want this');insert into rtest_nothn1 values (29, 'want this');insert into rtest_nothn1 values (30, 'don''t want this');insert into rtest_nothn1 values (39, 'don''t want this');insert into rtest_nothn1 values (40, 'want this');insert into rtest_nothn1 values (50, 'want this');insert into rtest_nothn1 values (60, 'want this');select * from rtest_nothn1;insert into rtest_nothn2 values (10, 'too small');insert into rtest_nothn2 values (50, 'too small');insert into rtest_nothn2 values (100, 'OK');insert into rtest_nothn2 values (200, 'OK');select * from rtest_nothn2;select * from rtest_nothn3;delete from rtest_nothn1;delete from rtest_nothn2;delete from rtest_nothn3;insert into rtest_nothn4 values (1, 'want this');insert into rtest_nothn4 values (2, 'want this');insert into rtest_nothn4 values (10, 'don''t want this');insert into rtest_nothn4 values (19, 'don''t want this');insert into rtest_nothn4 values (20, 'want this');insert into rtest_nothn4 values (29, 'want this');insert into rtest_nothn4 values (30, 'don''t want this');insert into rtest_nothn4 values (39, 'don''t want this');insert into rtest_nothn4 values (40, 'want this');insert into rtest_nothn4 values (50, 'want this');insert into rtest_nothn4 values (60, 'want this');insert into rtest_nothn1 select * from rtest_nothn4;select * from rtest_nothn1;delete from rtest_nothn4;insert into rtest_nothn4 values (10, 'too small');insert into rtest_nothn4 values (50, 'too small');insert into rtest_nothn4 values (100, 'OK');insert into rtest_nothn4 values (200, 'OK');insert into rtest_nothn2 select * from rtest_nothn4;select * from rtest_nothn2;select * from rtest_nothn3;create table rtest_view1 (a int4, b text, v bool);create table rtest_view2 (a int4);create table rtest_view3 (a int4, b text);create table rtest_view4 (a int4, b text, c int4);create view rtest_vview1 as select a, b from rtest_view1 X 	where 0 < (select count(*) from rtest_view2 Y where Y.a = X.a);create view rtest_vview2 as select a, b from rtest_view1 where v;create view rtest_vview3 as select a, b from rtest_vview2 X	where 0 < (select count(*) from rtest_view2 Y where Y.a = X.a);create view rtest_vview4 as select X.a, X.b, count(Y.a) as refcount	from rtest_view1 X, rtest_view2 Y	where X.a = Y.a	group by X.a, X.b;create function rtest_viewfunc1(int4) returns int4 as	'select count(*) from rtest_view2 where a = $1'	language 'sql';create view rtest_vview5 as select a, b, rtest_viewfunc1(a) as refcount	from rtest_view1;insert into rtest_view1 values (1, 'item 1', 't');insert into rtest_view1 values (2, 'item 2', 't');insert into rtest_view1 values (3, 'item 3', 't');insert into rtest_view1 values (4, 'item 4', 'f');insert into rtest_view1 values (5, 'item 5', 't');insert into rtest_view1 values (6, 'item 6', 'f');insert into rtest_view1 values (7, 'item 7', 't');insert into rtest_view1 values (8, 'item 8', 't');insert into rtest_view2 values (2);insert into rtest_view2 values (2);insert into rtest_view2 values (4);insert into rtest_view2 values (5);insert into rtest_view2 values (7);insert into rtest_view2 values (7);insert into rtest_view2 values (7);insert into rtest_view2 values (7);select * from rtest_vview1;select * from rtest_vview2;select * from rtest_vview3;select * from rtest_vview4;select * from rtest_vview5;insert into rtest_view3 select * from rtest_vview1 where a < 7;select * from rtest_view3;delete from rtest_view3;insert into rtest_view3 select * from rtest_vview2 where a != 5 and b !~ '2';select * from rtest_view3;delete from rtest_view3;insert into rtest_view3 select * from rtest_vview3;select * from rtest_view3;delete from rtest_view3;insert into rtest_view4 select * from rtest_vview4 where 3 > refcount;select * from rtest_view4;delete from rtest_view4;insert into rtest_view4 select * from rtest_vview5 where a > 2 and refcount = 0;select * from rtest_view4;delete from rtest_view4;---- Test for computations in views--create table rtest_comp (	part	text,	unit	char(4),	size	float);create table rtest_unitfact (	unit	char(4),	factor	float);create view rtest_vcomp as 	select X.part, (X.size * Y.factor) as size_in_cm			from rtest_comp X, rtest_unitfact Y			where X.unit = Y.unit;insert into rtest_unitfact values ('m', 100.0);insert into rtest_unitfact values ('cm', 1.0);insert into rtest_unitfact values ('inch', 2.54);insert into rtest_comp values ('p1', 'm', 5.0);insert into rtest_comp values ('p2', 'm', 3.0);insert into rtest_comp values ('p3', 'cm', 5.0);insert into rtest_comp values ('p4', 'cm', 15.0);insert into rtest_comp values ('p5', 'inch', 7.0);insert into rtest_comp values ('p6', 'inch', 4.4);select * from rtest_vcomp order by part;select * from rtest_vcomp where size_in_cm > 10.0 order by size_in_cm using >;---- In addition run the (slightly modified) queries from the-- programmers manual section on the rule system.--CREATE TABLE shoe_data (	shoename   char(10),      -- primary key	sh_avail   integer,       -- available # of pairs	slcolor    char(10),      -- preferred shoelace color	slminlen   float,         -- miminum shoelace length	slmaxlen   float,         -- maximum shoelace length	slunit     char(8)        -- length unit);CREATE TABLE shoelace_data (	sl_name    char(10),      -- primary key	sl_avail   integer,       -- available # of pairs	sl_color   char(10),      -- shoelace color	sl_len     float,         -- shoelace length	sl_unit    char(8)        -- length unit);CREATE TABLE unit (	un_name    char(8),       -- the primary key	un_fact    float          -- factor to transform to cm);CREATE VIEW shoe AS	SELECT sh.shoename,		   sh.sh_avail,		   sh.slcolor,		   sh.slminlen,		   sh.slminlen * un.un_fact AS slminlen_cm,		   sh.slmaxlen,		   sh.slmaxlen * un.un_fact AS slmaxlen_cm,		   sh.slunit	  FROM shoe_data sh, unit un	 WHERE sh.slunit = un.un_name;CREATE VIEW shoelace AS	SELECT s.sl_name,		   s.sl_avail,		   s.sl_color,		   s.sl_len,		   s.sl_unit,		   s.sl_len * u.un_fact AS sl_len_cm	  FROM shoelace_data s, unit u	 WHERE s.sl_unit = u.un_name;CREATE VIEW shoe_ready AS	SELECT rsh.shoename,		   rsh.sh_avail,		   rsl.sl_name,		   rsl.sl_avail,		   int4smaller(rsh.sh_avail, rsl.sl_avail) AS total_avail	  FROM shoe rsh, shoelace rsl	 WHERE rsl.sl_color = rsh.slcolor	   AND rsl.sl_len_cm >= rsh.slminlen_cm	   AND rsl.sl_len_cm <= rsh.slmaxlen_cm;INSERT INTO unit VALUES ('cm', 1.0);INSERT INTO unit VALUES ('m', 100.0);INSERT INTO unit VALUES ('inch', 2.54);INSERT INTO shoe_data VALUES ('sh1', 2, 'black', 70.0, 90.0, 'cm');INSERT INTO shoe_data VALUES ('sh2', 0, 'black', 30.0, 40.0, 'inch');INSERT INTO shoe_data VALUES ('sh3', 4, 'brown', 50.0, 65.0, 'cm');INSERT INTO shoe_data VALUES ('sh4', 3, 'brown', 40.0, 50.0, 'inch');INSERT INTO shoelace_data VALUES ('sl1', 5, 'black', 80.0, 'cm');INSERT INTO shoelace_data VALUES ('sl2', 6, 'black', 100.0, 'cm');INSERT INTO shoelace_data VALUES ('sl3', 0, 'black', 35.0 , 'inch');INSERT INTO shoelace_data VALUES ('sl4', 8, 'black', 40.0 , 'inch');INSERT INTO shoelace_data VALUES ('sl5', 4, 'brown', 1.0 , 'm');INSERT INTO shoelace_data VALUES ('sl6', 0, 'brown', 0.9 , 'm');INSERT INTO shoelace_data VALUES ('sl7', 7, 'brown', 60 , 'cm');INSERT INTO shoelace_data VALUES ('sl8', 1, 'brown', 40 , 'inch');-- SELECTs in docSELECT * FROM shoelace ORDER BY sl_name;SELECT * FROM shoe_ready WHERE total_avail >= 2;    CREATE TABLE shoelace_log (        sl_name    char(10),      -- shoelace changed        sl_avail   integer,       -- new available value        log_who    name,          -- who did it        log_when   datetime       -- when    );-- Want "log_who" to be CURRENT_USER,-- but that is non-portable for the regression test-- - thomas 1999-02-21    CREATE RULE log_shoelace AS ON UPDATE TO shoelace_data        WHERE NEW.sl_avail != OLD.sl_avail        DO INSERT INTO shoelace_log VALUES (                                        NEW.sl_name,                                        NEW.sl_avail,                                        'Al Bundy',                                        'epoch'::text                                    );UPDATE shoelace_data SET sl_avail = 6 WHERE  sl_name = 'sl7';SELECT * FROM shoelace_log;    CREATE RULE shoelace_ins AS ON INSERT TO shoelace        DO INSTEAD        INSERT INTO shoelace_data VALUES (               NEW.sl_name,               NEW.sl_avail,               NEW.sl_color,               NEW.sl_len,               NEW.sl_unit);    CREATE RULE shoelace_upd AS ON UPDATE TO shoelace        DO INSTEAD        UPDATE shoelace_data SET               sl_name = NEW.sl_name,               sl_avail = NEW.sl_avail,               sl_color = NEW.sl_color,               sl_len = NEW.sl_len,               sl_unit = NEW.sl_unit         WHERE sl_name = OLD.sl_name;    CREATE RULE shoelace_del AS ON DELETE TO shoelace        DO INSTEAD        DELETE FROM shoelace_data         WHERE sl_name = OLD.sl_name;    CREATE TABLE shoelace_arrive (        arr_name    char(10),        arr_quant   integer    );    CREATE TABLE shoelace_ok (        ok_name     char(10),        ok_quant    integer    );    CREATE RULE shoelace_ok_ins AS ON INSERT TO shoelace_ok        DO INSTEAD        UPDATE shoelace SET               sl_avail = sl_avail + NEW.ok_quant         WHERE sl_name = NEW.ok_name;INSERT INTO shoelace_arrive VALUES ('sl3', 10);INSERT INTO shoelace_arrive VALUES ('sl6', 20);INSERT INTO shoelace_arrive VALUES ('sl8', 20);SELECT * FROM shoelace ORDER BY sl_name;insert into shoelace_ok select * from shoelace_arrive;SELECT * FROM shoelace ORDER BY sl_name;SELECT * FROM shoelace_log;    CREATE VIEW shoelace_obsolete AS	SELECT * FROM shoelace WHERE NOT EXISTS	    (SELECT shoename FROM shoe WHERE slcolor = sl_color);    CREATE VIEW shoelace_candelete AS	SELECT * FROM shoelace_obsolete WHERE sl_avail = 0;insert into shoelace values ('sl9', 0, 'pink', 35.0, 'inch', 0.0);insert into shoelace values ('sl10', 1000, 'magenta', 40.0, 'inch', 0.0);SELECT * FROM shoelace_obsolete;SELECT * FROM shoelace_candelete;DELETE FROM shoelace WHERE EXISTS    (SELECT * FROM shoelace_candelete             WHERE sl_name = shoelace.sl_name);SELECT * FROM shoelace ORDER BY sl_name;---- Check that ruleutils are working--SELECT viewname, definition FROM pg_views ORDER BY viewname;SELECT tablename, rulename, definition FROM pg_rules 	ORDER BY tablename, rulename;

⌨️ 快捷键说明

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