-
Notifications
You must be signed in to change notification settings - Fork 0
/
---SQL-Constraints.js
77 lines (37 loc) · 1.44 KB
/
---SQL-Constraints.js
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
/*
---------------------------------------------------------------------------
More advanced items
CONSTRAINTS
---------------------------------------------------------------------------
referencing 2 tables
Promotions => id name category
Movies => id Title
-------------------------------
Do this (standard):
Promotions => id movie_id name category
|
foreign key
---------------------------------------------------------
CREATE TABLE Movies(
id int PRIMARY KEY,
title varchar(20) NOT NULL UNIQUE
);
CREATE TABLE Promotions(
id int PRIMARY KEY,
movie_id int, => change to => movie_id int REFERENCES movies(id),
name varchar(50), This makes sure that the field associates to existing ids
caregory varchar(15) within the Movies table
);
can also do this:
movie_id int, => change to => movie_id int REFERENCES movies,
will know to reference the primary key
or
movie_id int,
FOREIGN KEY (movie_id) REFERENCES movies,
---------------------------------------------------------
CREATE TABLE Movies(
id int PRIMARY KEY,
title varchar(20) NOT NULL UNIQUE
duration int CHECK (duration > 0) <-- The CHECK constraint
);
*/