Repository navigation
Expand file tree
/
Copy pathdb_training_0x05.sql
More file actions
202 lines (153 loc) · 5.3 KB
/
Copy pathdb_training_0x05.sql
File metadata and controls
202 lines (153 loc) · 5.3 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
-- (C) 2025 A.Voß, a.voss@fh-aachen.de, info@codebasedlearning.dev
-- SQL-Commands Unit 0x05
-- default schema, i.e. unqualified table names refer to ami_zone
SET SEARCH_PATH = ami_zone;
-- Query schemas
SELECT schema_name
FROM information_schema.schemata;
-- Create schema, list, delete
CREATE SCHEMA ami_test;
SELECT schema_name
FROM information_schema.schemata
WHERE schema_name = 'ami_test';
DROP SCHEMA ami_test;
-- Query tables in a schema
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'ami_zone' AND table_name LIKE 'shop%';
-- Query table structure
SELECT column_name, data_type, is_nullable, column_default
FROM information_schema.columns
WHERE table_schema = 'ami_zone' AND table_name = 'div_department'
ORDER BY ordinal_position;
--
-- Create schema for the table commands (if it is left over from a previous run, delete it first)
DROP SCHEMA IF EXISTS ami_example CASCADE;
CREATE SCHEMA ami_example;
SELECT schema_name
FROM information_schema.schemata
WHERE schema_name LIKE 'ami_%';
SET SEARCH_PATH = ami_example;
-- Schema is still empty
SELECT table_name
FROM information_schema.tables
WHERE table_schema = current_schema();
--
-- Create 'objects' table
CREATE TABLE objects (
id int primary key,
name varchar(10) unique not null,
comment varchar(255),
number int,
floating decimal(8,3) default 0.0,
created timestamp default now(),
important boolean not null default true
);
SELECT column_name, data_type, is_nullable, column_default
FROM information_schema.columns
WHERE table_schema = current_schema() AND table_name = 'objects'
ORDER BY ordinal_position;
-- Create sample data
INSERT INTO objects (id,name) VALUES (1,'mueller');
INSERT INTO objects (id,name,number) VALUES (2,'meier',3);
SELECT * FROM objects;
-- Change table, add attributes
ALTER TABLE objects
ADD COLUMN image bytea,
ADD COLUMN eps double precision DEFAULT 0.01;
SELECT * FROM objects;
-- Change type and default value
ALTER TABLE objects
ALTER COLUMN eps TYPE real,
ALTER COLUMN eps SET DEFAULT 0.002;
SELECT * FROM objects;
-- Change name and default value
ALTER TABLE objects
RENAME COLUMN eps TO feps;
ALTER TABLE objects
ALTER COLUMN feps SET DEFAULT 0.003;
SELECT * FROM objects;
-- Delete attributes
ALTER TABLE objects DROP COLUMN feps;
ALTER TABLE objects DROP COLUMN image;
SELECT * FROM objects;
-- Rename table
ALTER TABLE objects RENAME TO elements;
SELECT * FROM elements;
-- Remove all elements
TRUNCATE TABLE elements;
SELECT * FROM elements;
-- Remove table itself
DROP TABLE elements;
SELECT table_name
FROM information_schema.tables
WHERE table_schema = current_schema();
--
-- Create tables with table constraints
CREATE TABLE person (
person_id INT NOT NULL,
name VARCHAR(50) NOT NULL,
PRIMARY KEY (person_id)
);
-- Some data
INSERT INTO person (person_id,name) VALUES (11,'MIA');
INSERT INTO person (person_id,name) VALUES (12,'LEA');
SELECT * FROM person;
-- Create table with foreign key, check constraint and index.
-- The column itself would allow 50 characters, the CHECK constraint only 10.
CREATE TABLE pet (
pet_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
person_id INT NULL,
CONSTRAINT name_length_check CHECK (char_length(name) <= 10),
CONSTRAINT fk_person
FOREIGN KEY (person_id) REFERENCES person (person_id)
);
-- PostgreSQL does not create an index for a foreign key automatically (MySQL does)
CREATE INDEX person_idx ON pet (person_id); -- ASC is the default sort order
-- Some data and a join
INSERT INTO pet (pet_id,name,person_id) VALUES (1,'Wuff',11);
INSERT INTO pet (pet_id,name) VALUES (2,'Bello');
SELECT * FROM pet M LEFT OUTER JOIN person F ON M.person_id=F.person_id;
-- insert with check that succeeds ('Mr. Rob' has 7 characters)
INSERT INTO pet (pet_id,name,person_id) VALUES (3,'Mr. Rob',11);
-- expect-error: 'Mr. Robinson' has 12 characters, violates name_length_check
INSERT INTO pet (pet_id,name,person_id) VALUES (4,'Mr. Robinson',11);
-- expect-error: person 99 does not exist, violates fk_person
INSERT INTO pet (pet_id,name,person_id) VALUES (5,'Mini',99);
SELECT * FROM pet M;
-- Clean-up: we keep schema ami_example with person and pet for unit 0x06.
-- DROP SCHEMA ami_example CASCADE;
SET SEARCH_PATH = ami_zone;
-- Data for ami_sport (task 5.2) - adapt the column names to your own tables
/*
INSERT INTO athlete (id, name, birthday, is_male) VALUES
(101, 'Anna', '1990-02-01', false),
(102, 'Olga', '1991-03-01', false);
INSERT INTO athlete (id, name, birthday, prize_money, is_male) VALUES
(111, 'Enie', '1992-04-01', 100, false),
(112, 'Antje', '1993-05-01', 200, false),
(121, 'Boris', '1990-06-01', 3000, true),
(122, 'Ivan', '1991-07-01', 4000, true);
INSERT INTO team (id, name) VALUES
(12345, 'Team NL'),
(98765, 'Team PL');
INSERT INTO competition (id, description, for_male) VALUES
(56, 'Tennis Preliminary Round - Doubles', false),
(98, 'Tennis Final - Singles', true),
(99, 'Tennis Final - Singles', false);
INSERT INTO attends (id, athlete_id, team_id, competition_id) VALUES
(1, 101, 98765, 56),
(2, 102, 98765, 56),
(3, 111, 12345, 56),
(4, 112, 12345, 56);
INSERT INTO attends (id, athlete_id, competition_id) VALUES
(5, 101, 99),
(6, 111, 99),
(7, 121, 98),
(8, 122, 98);
INSERT INTO referees (id, athlete_id, competition_id) VALUES
(1, 121, 56),
(2, 122, 99),
(3, 101, 98);
*/