-
Notifications
You must be signed in to change notification settings - Fork 24
/
regressdatabase.sql
133 lines (112 loc) · 3.66 KB
/
regressdatabase.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
--
-- $Id: regressdatabase.sql,v 1.2 2006/02/13 01:15:56 rbt Exp $
--
BEGIN;
--
-- Foreign key'd structure, check constraints, primary keys
-- and duplicate table names in different schemas
--
CREATE SCHEMA product
CREATE TABLE product
( product_id SERIAL PRIMARY KEY
, product_code text NOT NULL UNIQUE
CHECK(product_code = upper(product_code))
, product_description text
);
CREATE SCHEMA store
CREATE TABLE store
( store_id SERIAL PRIMARY KEY
, store_code text NOT NULL UNIQUE
CHECK(store_code = upper(store_code))
, store_description text
)
CREATE TABLE inventory
( store_id integer REFERENCES store
ON UPDATE CASCADE ON DELETE RESTRICT
, product_id integer REFERENCES product.product
ON UPDATE CASCADE ON DELETE RESTRICT
, PRIMARY KEY(store_id, product_id)
, quantity integer NOT NULL CHECK(quantity > 0)
);
--
-- Another schema with
--
CREATE SCHEMA warehouse
CREATE TABLE warehouse
( warehouse_id SERIAL PRIMARY KEY
, warehouse_code text NOT NULL UNIQUE
CHECK(warehouse_code = upper(warehouse_code))
, warehouse_manager text NOT NULL
, warehouse_supervisor text UNIQUE
, warehouse_description text
, CHECK (upper(warehouse_manager) != upper(warehouse_supervisor))
)
CREATE TABLE inventory
( warehouse_id integer REFERENCES warehouse
ON UPDATE CASCADE
ON DELETE RESTRICT
, product_id integer REFERENCES product.product
ON UPDATE CASCADE
ON DELETE RESTRICT
, PRIMARY KEY(warehouse_id, product_id)
, quantity integer NOT NULL
CHECK(quantity > 0)
)
CREATE VIEW products AS
SELECT DISTINCT product.*
FROM inventory
JOIN product.product USING (product_id);
-- Sample index
CREATE INDEX quantity_index ON warehouse.inventory (quantity);
--
-- Simple text comments
--
--COMMENT ON DATABASE IS
--'This database has been created for the purpose of simple
-- tests on PostgreSQL Autodoc.';
COMMENT ON SCHEMA product IS
'This schema stores a list of products and information
about the product';
COMMENT ON SCHEMA warehouse IS
'A list of warehouses and information on warehouses';
COMMENT ON TABLE warehouse.inventory IS
'Warehouse inventory';
COMMENT ON TABLE store.inventory IS
'Store inventory';
COMMENT ON COLUMN warehouse.warehouse.warehouse_code IS
'Internal code which represents warehouses for
invoice purposes';
COMMENT ON COLUMN warehouse.warehouse.warehouse_supervisor IS
'Supervisors name for a warehouse when one
has been assigned. The same supervisor may not
be assigned to more than one warehouse, per company
policy XYZ.';
COMMENT ON COLUMN warehouse.warehouse.warehouse_manager IS
'Name of Warehouse Manager';
--
-- A few simple functions
--
CREATE FUNCTION product.worker(integer, integer) RETURNS integer AS
'SELECT $1 + $1;' LANGUAGE sql;
CREATE FUNCTION warehouse.worker(integer, integer) RETURNS integer AS
'SELECT $1 * $1;' LANGUAGE sql;
COMMENT ON FUNCTION product.worker(integer, integer) IS
'Worker function appropriate for products';
COMMENT ON FUNCTION warehouse.worker(integer, integer) IS
'Worker function appropriate for warehouses.';
create or replace function product.worker2 (i_1 integer, i_2 integer) returns integer as $$
begin
return i_1 + i_2;
end
$$ language plpgsql;
comment on function product.worker2 (i_1 integer, i_2 integer)
is 'worker function that uses named parameters';
--
-- Inheritance
--
CREATE SCHEMA inherit
CREATE TABLE taba (cola integer)
CREATE TABLE tabb (colb integer) inherits(taba)
CREATE TABLE tab1 (col1 integer)
CREATE TABLE tab1b (col1b integer) inherits(tab1, tabb);
COMMIT;