-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsakila_table_creation.sql
187 lines (159 loc) · 4.15 KB
/
sakila_table_creation.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
create database sakila;
use sakila;
create table actor (
actor_id int ,
first_name varchar(100),
last_name varchar(100),
last_update timestamp,
primary key(actor_id)
);
create table category (
category_id int,
name varchar(50),
last_update timestamp,
primary key(category_id)
);
create table language (
language_id int,
name varchar(50),
last_update timestamp,
primary key (language_id)
);
create table country(
country_id int,
country_name varchar(255),
last_update timestamp,
primary key (country_id)
);
create table city (
city_id int,
city_name varchar(255),
country_id int,
last_update timestamp,
primary key (city_id),
foreign key(country_id) references country(country_id)
);
create table film (
film_id int,
title varchar(255),
description text,
release_year year,
language_id int,
original_language_id int,
rental_duration int,
rental_rate float,
length int,
replacement_cost float,
rating varchar(50),
special_features text,
last_update timestamp,
primary key(film_id),
foreign key(language_id) references language(language_id)
);
create table film_category(
film_id int,
category_id int,
last_update timestamp,
primary key (film_id),
foreign key (category_id) references category(category_id)
);
alter table film_category drop primary key ;
alter table film_category add constraint primary key(category_id, film_id);
create table address(
address_id int,
address text,
address2 text,
district varchar(255),
city_id int,
postal_code int,
phone int,
last_update timestamp,
primary key (address_id),
foreign key(city_id) references city(city_id)
);
create table inventory (
inventory_id int,
film_id int,
store_id int,
last_update timestamp);
alter table inventory
add constraint inventory primary key(inventory_id),
add constraint inventory foreign key(film_id) references film(film_id);
create table staff (
staff_id int,
first_name varchar(100),
last_name varchar(100),
address_id int,
picture varchar(255),
email varchar(100),
store_id int,
active int,
username varchar(100),
password varchar(100),
last_update timestamp,
primary key(staff_id),
foreign key (address_id) references address(address_id)
);
create table store (
store_id int,
manager_staff_id int,
address_id int,
last_update timestamp,
primary key(store_id),
foreign key (address_id) references address(address_id),
foreign key (manager_staff_id) references staff(staff_id)
);
alter table inventory
add constraint foreign key (store_id) references store(store_id);
alter table staff
add constraint foreign key (staff_id) references store(store_id);
create table customer(
customer_id int,
store_id int,
first_name varchar(100),
last_name varchar(100),
email varchar(100),
address_id int,
active int,
create_date timestamp,
last_update timestamp,
primary key (customer_id),
foreign key (store_id) references store(store_id),
foreign key (address_id) references address(address_id)
);
create table rental (
rental_id int,
rental_date timestamp,
inventory_id int,
customer_id int,
return_date timestamp,
staff_id int,
last_update timestamp,
primary key (rental_id),
foreign key (inventory_id) references inventory(inventory_id),
foreign key (customer_id) references customer(customer_id),
foreign key (staff_id) references staff(staff_id)
);
create table payment(
payment_id int,
customer_id int,
staff_id int,
rental_id int,
amount float,
payment_date timestamp,
last_update timestamp,
primary key (payment_id),
foreign key (customer_id) references customer(customer_id),
foreign key (staff_id) references staff(staff_id),
foreign key (rental_id) references rental(rental_id)
);
create table film_actor(
actor_id int,
film_id int,
last_update timestamp,
foreign key(film_id) references film(film_id),
foreign key(actor_id) references actor(actor_id)
);
alter table film_actor
add constraint primary key (actor_id, film_id);
show tables;