-
Notifications
You must be signed in to change notification settings - Fork 0
/
CREATE_requests.sql
125 lines (97 loc) · 3.77 KB
/
CREATE_requests.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
-- 1 вариант
CREATE TABLE IF NOT EXISTS Performers (
performer_id SERIAL PRIMARY KEY,
alias VARCHAR(40) UNIQUE NOT NULL
);
CREATE TABLE IF NOT EXISTS Genres (
genre_id SERIAL PRIMARY KEY,
name VARCHAR(40) UNIQUE NOT NULL
);
CREATE TABLE IF NOT EXISTS Performers_Genres (
performer_id INTEGER REFERENCES Performers(performer_id),
genre_id INTEGER REFERENCES Genres(genre_id),
CONSTRAINT pk_PG PRIMARY KEY (performer_id, genre_id)
);
CREATE TABLE IF NOT EXISTS Albums (
album_id SERIAL PRIMARY KEY,
name VARCHAR(60) NOT NULL,
date DATE NOT NULL
);
CREATE TABLE IF NOT EXISTS Performers_Albums (
performer_id INTEGER REFERENCES Performers(performer_id),
album_id INTEGER REFERENCES Albums(album_id),
CONSTRAINT pk_PA PRIMARY KEY (performer_id, album_id)
);
CREATE TABLE IF NOT EXISTS Tracks (
track_id SERIAL PRIMARY KEY,
name VARCHAR(60) NOT NULL,
duration INTEGER NOT NULL,
album_id INTEGER REFERENCES Albums(album_id)
);
CREATE TABLE IF NOT EXISTS Collections (
collection_id SERIAL PRIMARY KEY,
name VARCHAR(60) NOT NULL,
date DATE NOT NULL
);
CREATE TABLE IF NOT EXISTS Collections_Tracks (
collection_id INTEGER REFERENCES Collections(collection_id),
track_id INTEGER REFERENCES Tracks(track_id),
CONSTRAINT pk_CT PRIMARY KEY (collection_id, track_id)
);
---------------------------------------------------------------
-- 2 вариант
CREATE TABLE IF NOT EXISTS Performers (
performer_id INTEGER PRIMARY KEY
GENERATED ALWAYS AS IDENTITY,
alias VARCHAR(40) UNIQUE NOT NULL
);
CREATE TABLE IF NOT EXISTS Genres (
genre_id INTEGER PRIMARY KEY
GENERATED ALWAYS AS IDENTITY,
name_genre VARCHAR(40) UNIQUE NOT NULL
);
CREATE TABLE IF NOT EXISTS Performers_Genres (
performer_id INTEGER REFERENCES Performers(performer_id),
genre_id INTEGER REFERENCES Genres(genre_id),
CONSTRAINT pk_PG PRIMARY KEY (performer_id, genre_id)
);
CREATE TABLE IF NOT EXISTS Albums (
album_id INTEGER PRIMARY KEY
GENERATED ALWAYS AS IDENTITY,
name_album VARCHAR(60) UNIQUE NOT NULL,
year_album INTEGER NOT NULL
CHECK(year_album BETWEEN 1980 AND 2030)
);
CREATE TABLE IF NOT EXISTS Performers_Albums (
performer_id INTEGER REFERENCES Performers(performer_id),
album_id INTEGER REFERENCES Albums(album_id),
CONSTRAINT pk_PA PRIMARY KEY (performer_id, album_id)
);
CREATE TABLE IF NOT EXISTS Tracks (
track_id INTEGER PRIMARY KEY
GENERATED ALWAYS AS IDENTITY,
name_track VARCHAR(60) UNIQUE NOT NULL,
duration INTEGER NOT NULL
CHECK(duration BETWEEN 30 AND 1000),
album_id INTEGER REFERENCES Albums(album_id)
);
CREATE TABLE IF NOT EXISTS Collections (
collection_id INTEGER PRIMARY KEY
GENERATED ALWAYS AS IDENTITY,
name_collection VARCHAR(60) UNIQUE NOT NULL,
year_collection INTEGER NOT NULL
CHECK(year_collection BETWEEN 1980 AND 2030)
);
CREATE TABLE IF NOT EXISTS Collections_Tracks (
collection_id INTEGER REFERENCES Collections(collection_id),
track_id INTEGER REFERENCES Tracks(track_id),
CONSTRAINT pk_CT PRIMARY KEY (collection_id, track_id)
);
-- Заметки:
-- Удаление данных из таблицы (my_table) и сброс счетчика (ID),
-- CASCADE для каскадного удаления всех связаных таблиц с my_table
-- TRUNCATE TABLE my_table RESTART IDENTITY [CASCADE];
-- Создание стобца ID (используется вместо SERIAL)
--CREATE TABLE IF NOT EXISTS my_table (
-- my_id INTEGER PRIMARY KEY
-- GENERATED ALWAYS AS IDENTITY;