-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource-tạo-bảng-và-ràng-buộc.sql
192 lines (176 loc) · 5.11 KB
/
source-tạo-bảng-và-ràng-buộc.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
184
185
186
187
188
189
190
191
192
USE master
GO
IF DB_ID('QLBTN') IS NOT NULL
DROP DATABASE QLBTN
GO
CREATE DATABASE QLBTN
GO
USE QLBTN
CREATE TABLE NHABAN
(
MANHA CHAR(5),
MALOAI CHAR(5),
DIACHI NVARCHAR(50),
SOLUONGPHONG INT,
MACHINHANH CHAR(5),
MANVPHUTRACH CHAR(5),
MACHUNHA CHAR(5),
NGAYDANG DATETIME,
NGAYHETHAN DATETIME,
TINHTRANG NVARCHAR(30),
SOLUOTXEM INT,
GIABAN INT,
DIEUKIENBAN NVARCHAR(30)
--KHAI BAO KHOA CHINH
CONSTRAINT PK_NB
PRIMARY KEY (MANHA)
)
CREATE TABLE NHACHOTHUE
(
MANHA CHAR(5),
MALOAI CHAR(5),
DIACHI NVARCHAR(50),
SOLUONGPHONG INT,
MACHINHANH CHAR(5),
MANVPHUTRACH CHAR(5),
MACHUNHA CHAR(5),
TIENTHUE1THANG INT,
NGAYDANG DATETIME,
NGAYHETHAN DATETIME,
TINHTRANG NVARCHAR(30),
SOLUOTXEM INT
--KHAI BAO KHOA CHINH
CONSTRAINT PK_NCT
PRIMARY KEY (MANHA)
)
CREATE TABLE LOAINHA
(
MALOAINHA CHAR(5),
TENLOAINHA NVARCHAR(20)
--KHOA CHINH
CONSTRAINT PK_LN
PRIMARY KEY (MALOAINHA)
)
CREATE TABLE CHUNHA
(
MACHUNHA CHAR(5),
HOTEN NVARCHAR(50),
DIACHI NVARCHAR(50),
SODIENTHOAI CHAR(12) UNIQUE,
--KHOA CHINH
CONSTRAINT PK_CN
PRIMARY KEY (MACHUNHA)
--RANG BUOC SDT LA DUY NHAT
)
CREATE TABLE CHINHANH
(
MACHINHANH CHAR(5),
DIACHI NVARCHAR(50),
SOFAX CHAR(6) UNIQUE,
SODIENTHOAI CHAR(12) UNIQUE,
CONSTRAINT PK_CHINHANH
PRIMARY KEY (MACHINHANH)
)
CREATE TABLE NHANVIEN
(
MANV CHAR(5),
HOTEN NVARCHAR(30),
DIACHI NVARCHAR(50),
DIENTHOAI CHAR(12),
GIOITINH NCHAR(5),
NGAYSINH DATETIME,
LUONG INT,
MACHINHANH CHAR(5)
--
CONSTRAINT PK_NV
PRIMARY KEY (MANV, MACHINHANH)
)
CREATE TABLE KHACHHANG
(
MANGUOITHUE CHAR(5),
MALOAI CHAR(5),
HOTEN NVARCHAR(30),
DIACHI NVARCHAR(50),
SODIENTHOAI CHAR(12) UNIQUE,
MACHINHANH CHAR(5)
CONSTRAINT PK_KH
PRIMARY KEY (MANGUOITHUE)
)
CREATE TABLE THONGBAOXEMNHA
(
MAKHACHHANG CHAR(5),
MANV CHAR(5),
MANHATHUE CHAR(5),
MANHABAN CHAR(5),
MACHINHANH CHAR(5),
NGAYXEM DATETIME,
NHANXET NVARCHAR(30),
CONSTRAINT PK_TBXM
PRIMARY KEY (MAKHACHHANG, MANV, NGAYXEM)
)
CREATE TABLE HOPDONGCHOTHUE
(
MAHOPDONG CHAR(5),
MANHACHOTHUE CHAR(5),
MAKHACHHANG CHAR(5),
MACHUNHA CHAR(5)
CONSTRAINT PK_HDCT
PRIMARY KEY (MAHOPDONG)
)
CREATE TABLE HOPDONGBAN
(
MAHOPDONG CHAR(5),
MANHABAN CHAR(5),
MAKHACHHANG CHAR(5),
MACHUNHA CHAR(5)
CONSTRAINT PK_HDB
PRIMARY KEY (MAHOPDONG)
)
--TẠO CÁC RÀNG BUỘC KHÓA NGOẠI
--KHOA NGOAI NHABAN VA LOAI NHA
ALTER TABLE NHABAN ADD CONSTRAINT FK_NB_LN FOREIGN KEY(MALOAI) REFERENCES LOAINHA
--KHOA NGOAI NHACHOTHUE VA LOAI NHA
ALTER TABLE NHACHOTHUE ADD CONSTRAINT FK_NCT_LN FOREIGN KEY(MALOAI) REFERENCES LOAINHA
--KHOA NGOAI CHU NHA VOI NHABAN
ALTER TABLE NHABAN ADD CONSTRAINT FK_NB_CN FOREIGN KEY (MACHUNHA) REFERENCES CHUNHA
--KHOA NGOAI CHU NHA VOI NHACHOTHUE
ALTER TABLE NHACHOTHUE ADD CONSTRAINT FK_NCT_CN FOREIGN KEY (MACHUNHA) REFERENCES CHUNHA
--KHOA NGOAI VOI NHABAN
ALTER TABLE NHABAN ADD CONSTRAINT FK_NB_CHINHANH FOREIGN KEY (MACHINHANH) REFERENCES CHINHANH
--KHOA NGOAI VOI NHACHOTHUE
ALTER TABLE NHACHOTHUE ADD CONSTRAINT FK_NCT_CHINHANH FOREIGN KEY (MACHINHANH) REFERENCES CHINHANH
--CHECK GIOITINH
ALTER TABLE NHANVIEN
ADD
CONSTRAINT C_GIOITINH
CHECK(GIOITINH IN('Nam', N'Nữ'))
--RANG BUOC KHOA NGOAI NHAN VIEN VOI NHABAN
ALTER TABLE NHABAN ADD CONSTRAINT FK_NB_NV FOREIGN KEY (MANVPHUTRACH, MACHINHANH) REFERENCES NHANVIEN
--RANG BUOC KHOA NGOAI NHAN VIEN VOI NHACHOTHUE
ALTER TABLE NHACHOTHUE ADD CONSTRAINT FK_NCT_NV FOREIGN KEY (MANVPHUTRACH, MACHINHANH) REFERENCES NHANVIEN
--RANG BUOC KHOA NGOAI NHAN VIEN VOI CHI NHANH
ALTER TABLE NHANVIEN ADD CONSTRAINT FK_NV_CHINHANH FOREIGN KEY (MACHINHANH) REFERENCES CHINHANH
--RANG BUOC KHOA NGOAI KHACH HANG VOI CHI NHANH
ALTER TABLE KHACHHANG ADD CONSTRAINT PK_KH_CHINHANH FOREIGN KEY (MACHINHANH) REFERENCES CHINHANH
--RANG BUOC KHACH HANG VOI LOAI NHA
ALTER TABLE KHACHHANG ADD CONSTRAINT PK_KH_LN FOREIGN KEY (MALOAI) REFERENCES LOAINHA
--RANG BUOC THONGBAOXEMNHA VOI KHACHHANG
ALTER TABLE THONGBAOXEMNHA ADD CONSTRAINT PK_TBXN_KH FOREIGN KEY (MAKHACHHANG) REFERENCES KHACHHANG
--RANG BUOC THONGBAOXEMNHA VOI NHANVIEN
ALTER TABLE THONGBAOXEMNHA ADD CONSTRAINT PK_TBXN_NV FOREIGN KEY (MANV, MACHINHANH) REFERENCES NHANVIEN
--RANG BUOC THONG BAO XEM NHA VOI NHABAN
ALTER TABLE THONGBAOXEMNHA ADD CONSTRAINT PK_TBXN_NB FOREIGN KEY (MANHABAN) REFERENCES NHABAN
--RANG BUOC THONG BAO XEM NHA VOI NHACHOTHUE
ALTER TABLE THONGBAOXEMNHA ADD CONSTRAINT PK_TBXN_NCT FOREIGN KEY (MANHATHUE) REFERENCES NHACHOTHUE
--RANG BUOC HOPDONGBAN VOI CHU NHA
ALTER TABLE HOPDONGBAN ADD CONSTRAINT PK_HDB_CN FOREIGN KEY (MACHUNHA) REFERENCES CHUNHA
--RANG BUOC HOPDONGBAN VOI KHACHHANG
ALTER TABLE HOPDONGBAN ADD CONSTRAINT PK_HDB_KH FOREIGN KEY (MAKHACHHANG) REFERENCES KHACHHANG
--RANG BUOC HOPDONGBAN VOI NHA BAN
ALTER TABLE HOPDONGBAN ADD CONSTRAINT PK_HDB_NB FOREIGN KEY (MANHABAN) REFERENCES NHABAN
--RANG BUOC HOPDONGCHOTHUE VOI CHU NHA
ALTER TABLE HOPDONGCHOTHUE ADD CONSTRAINT PK_HDCT_CN FOREIGN KEY (MACHUNHA) REFERENCES CHUNHA
--RANG BUOC HOPDONGCHOTHUE VOI KHACHHANG
ALTER TABLE HOPDONGCHOTHUE ADD CONSTRAINT PK_HDCT_KH FOREIGN KEY (MAKHACHHANG) REFERENCES KHACHHANG
--RANG BUOC HOPDONGCHOTHUE VOI NHACHOTHUE
ALTER TABLE HOPDONGCHOTHUE ADD CONSTRAINT PK_HDCT_NCT FOREIGN KEY (MANHACHOTHUE) REFERENCES NHACHOTHUE