-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.sql
More file actions
147 lines (122 loc) · 3.27 KB
/
base.sql
File metadata and controls
147 lines (122 loc) · 3.27 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
CREATE DATABASE testdb;
DROP DATABASE testdb;
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'testdb'
AND pid <> pg_backend_pid()
--Отношение один ко многим
CREATE TABLE publisher
(
publisher_id integer PRIMARY KEY,
org_name varchar(128) NOT NULL,
address text NOT NULL
);
CREATE TABLE book
(
book_id integer PRIMARY KEY,
title text NOT NULL,
isbn varchar(32) NOT NULL
);
INSERT INTO book
VALUES
(1, 'The Diary of a Young Girl', '0199535566'),
(2, 'Pride and Prejudice', '9780307594006'),
(3, 'To Kill a Mockingbird', '0446310786'),
(4, 'The Book of Gutsy Women: Favorite Stories of Courage and Resilience', '1501178415'),
(5, 'War and Peace', '1788886526');
INSERT INTO publisher
VALUES
(1, 'Everyman''s Library', 'NY'),
(2, 'Oxford University Press', 'NY'),
(3, 'Grand Central Publishing', 'Washington'),
(4, 'Simon & Schuster', 'Chicago');
SELECT * FROM publisher;
----
ALTER TABLE book
ADD COLUMN fk_publisher_id;
ALTER TABLE book
ADD CONSTRAINT fk_book_publisher
FOREIGN KEY(fk_publisher_id) REFERENCES publisher(publisher_id);
----
DROP TABLE book;
CREATE TABLE book
(
book_id integer PRIMARY KEY,
title text NOT NULL,
isbn varchar(32) NOT NULL,
fk_publisher_id integer REFERENCES publisher(publisher_id) NOT NULL
);
INSERT INTO book
VALUES
(1, 'The Diary of a Young Girl', '0199535566', 1),
(2, 'Pride and Prejudice', '9780307594006', 1),
(3, 'To Kill a Mockingbird', '0446310786', 2),
(4, 'The Book of Gutsy Women: Favorite Stories of Courage and Resilience', '1501178415', 2),
(5, 'War and Peace', '1788886526', 2);
--Отношение один к одному
CREATE TABLE person
(
person_id int PRIMARY KEY,
first_name varchar(64) NOT NULL,
last_name varchar(64) NOT NULL
);
CREATE TABLE passport
(
passport_id int PRIMARY KEY,
serial_number int NOT NULL,
fk_passport_person int UNIQUE REFERENCES person(person_id)
);
INSERT INTO person VALUES (1, 'John', 'Snow');
INSERT INTO person VALUES (2, 'Ned', 'Stark');
INSERT INTO person VALUES (3, 'Rob', 'Baratheon');
ALTER TABLE passport
ADD COLUMN registration text NOT NULL;
INSERT INTO passport VALUES (1, 123456, 1, 'Winterfell');
INSERT INTO passport VALUES (2, 789012, 2, 'Winterfell');
INSERT INTO passport VALUES (3, 345678, 3, 'King''s Landing');
--Отношение многие ко многим
DROP TABLE IF EXISTS book;
DROP TABLE IF EXISTS author;
CREATE TABLE book
(
id int PRIMARY KEY,
title text NOT NULL,
isbn text NOT NULL
);
CREATE TABLE author
(
id int PRIMARY KEY,
name text NOT NULL,
rating real
);
CREATE TABLE book_author
(
book_id int REFERENCES book(id),
author_id int REFERENCES author(id),
CONSTRAINT book_author_pkey PRIMARY KEY (book_id, author_id) --composite key ключ состоит более чем из 1 колонки
);
INSERT INTO book
VALUES
(1, 'The Diary of a Young Girl', '6'),
(2, 'Pride and Prejudice', '6'),
(3, 'To Kill a Mockingbird', '86'),
(4, 'The Book of Gutsy Women: Favorite Stories of Courage and Resilience', '15'),
(5, 'War and Peace', '178');
INSERT INTO author
VALUES
(1, 'Alice', 4.92),
(2, 'Vincent', 4.98),
(3, 'Sara', 4.63);
INSERT INTO book_author
VALUES
(1, 1),
(2, 1),
(3, 1),
(3, 2),
(3, 3),
(4, 2),
(4, 3),
(5, 1);
SELECT * FROM book;
SELECT * FROM author;
SELECT * FROM book_author;