-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTablesCreation_1.sql
160 lines (121 loc) · 3.95 KB
/
TablesCreation_1.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
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
/*
Purpose : Movies and series Table creation script
Descr : Tables need foreign keys
Author : zerobits01
Date : 2019-10-18
on : MoviesAndSeries
*/
use [MoviesAndSeries]
-- Table MovieDirection(dir_id,mov_id)
create table moviesdirection(
-- columns :
dir_id int,
mov_id int,
-- constraints :
constraint movdir_pk primary key (dir_id,mov_id),
constraint movdir_fk0 foreign key (dir_id) references directors,
constraint movdir_fk1 foreign key (mov_id) references movies,
);
-- Table MoviesCast(act_id,mov_id,role)
create table moviescast(
-- columns :
act_id int,
mov_id int,
rol varchar(55) Not Null,
-- constraints :
constraint movcas_pk primary key (act_id,mov_id),
constraint movcas_fk0 foreign key (act_id) references actors,
constraint movcas_fk1 foreign key (mov_id) references movies,
);
-- Table MoviesRating(mov_id,rev_id,rate)
create table moviesrate(
-- columns :
rev_id int,
mov_id int,
rate float(24) Not Null,
-- constraints :
constraint movrat_pk primary key (rev_id,mov_id),
constraint movrat_fk0 foreign key (rev_id) references reviewers,
constraint movrat_fk1 foreign key (mov_id) references movies,
);
-- Table MovieGenres(mov_id,gen_id)
create table moviesgen(
-- columns :
gen_id int,
mov_id int,
-- constraints :
constraint movgen_pk primary key (gen_id,mov_id),
constraint movgen_fk0 foreign key (gen_id) references genres,
constraint movgen_fk1 foreign key (mov_id) references movies,
);
-- Table SeriesDirection(dir_id,ser_id)
create table seriesdir(
-- columns :
dir_id int,
ser_id int,
-- constraints :
constraint serdir_pk primary key (ser_id,dir_id),
constraint serdir_fk0 foreign key (ser_id) references series,
constraint serdir_fk1 foreign key (dir_id) references directors,
);
-- Table SeriesCast(act_id,ser_id,seas_id,role)
create table seriescast(
-- columns :
act_id int,
ser_id int,
rol varchar(255) not null,
-- constraints :
constraint sercas_pk primary key (act_id,ser_id),
constraint sercas_fk0 foreign key (ser_id) references series,
constraint sercas_fk1 foreign key (act_id) references actors,
);
-- Table SeriesRating(ser_id,rev_id,rate)
create table seriesrate(
-- columns :
rev_id int,
ser_id int,
rate float(24) Not Null,
-- constraints :
constraint serrat_pk primary key (rev_id,ser_id),
constraint serrat_fk0 foreign key (rev_id) references reviewers,
constraint serrat_fk1 foreign key (ser_id) references series,
);
-- Table SeriesGenres(ser_id,gen_id)
create table seriesgen(
-- columns :
gen_id int,
ser_id int,
-- constraints :
constraint sergen_pk primary key (gen_id,ser_id),
constraint sergen_fk0 foreign key (gen_id) references genres,
constraint sergen_fk1 foreign key (ser_id) references series,
);
-- Table Seasons(season_id,ser_id,season_name,episode_count)
create Table seasons(
season_id int identity,
ser_id int Not Null ,
season_name varchar(55) Not Null ,
episode_count int Not Null,
-- constraints :
constraint season_pk primary key (season_id),
constraint season_fk0 foreign key (ser_id) references series,
);
/*
poits
ALTER TABLE tblname ALTER COLUMN colname datatype {[Not] NULL} ; -- for making nullable
ALTER TABLE tblname add {CONSTRAINT nameforconstraint UNIQUE (firstcol[,secondcol[,...]])}; -- adding unique
ALTER TABLE tblname ADD CONSTRAINT nameforconstraint primary key (colname[,secondcolname])
ALTER TABLE tblname DROP CONSTRAINT nameforconstraint
in the table : Foreign Key (colname) references TableOfPk on delete cascade on update cascade
in the table : fk int FOREIGN KEY REFERENCES tblPk(pkforfk)
in the table : CONSTRAINT fk_name FOREIGN KEY (col) REFERENCES table(pk)
in the table : CHECK (P_Id>0)
in the table : CHECK (P_Id>0 AND City='Sandnes')
ALTER TABLE tblname ADD CHECK (colname > 0)
ALTER TABLE tblname ADD CONSTRAINT chk_name CHECK (col1 > 0 AND col2 = 'sit..')
in the table : colname varchar(255) DEFAULT 'Sandnes'
in the table : OrderDate date DEFAULT GETDATE()
ALTER TABLE tblname ADD CONSTRAINT PK_DEF DEFAULT ('SANDNES‘) For colname
auto increment : identity
P_Id int PRIMARY KEY IDENTITY
*/