-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09. Table relations Homework P5.sql
185 lines (144 loc) · 3.84 KB
/
09. Table relations Homework P5.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
/*
05 ôåâðóàðè 2019 ã.20:47:39
User:
Server: M812\SQLEXPRESS
Database: OnlineStore
Application:
*/
/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
CREATE TABLE dbo.ItemTypes
(
ItemTypeID int NOT NULL,
Name varbinary(50) NOT NULL
) ON [PRIMARY]
ALTER TABLE dbo.ItemTypes ADD CONSTRAINT
PK_ItemTypes PRIMARY KEY CLUSTERED
(
ItemTypeID
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
ALTER TABLE dbo.ItemTypes SET (LOCK_ESCALATION = TABLE)
COMMIT
BEGIN TRANSACTION
CREATE TABLE dbo.Items
(
ItemID int NOT NULL,
Name nchar(50) NOT NULL,
ItemTypeID int NOT NULL
) ON [PRIMARY]
ALTER TABLE dbo.Items ADD CONSTRAINT
PK_Items PRIMARY KEY CLUSTERED
(
ItemID
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
ALTER TABLE dbo.Items ADD CONSTRAINT
FK_Items_ItemTypes FOREIGN KEY
(
ItemTypeID
) REFERENCES dbo.ItemTypes
(
ItemTypeID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
ALTER TABLE dbo.Items SET (LOCK_ESCALATION = TABLE)
COMMIT
BEGIN TRANSACTION
CREATE TABLE dbo.Cities
(
CityID int NOT NULL IDENTITY (1, 1),
Name nchar(50) NOT NULL
) ON [PRIMARY]
ALTER TABLE dbo.Cities ADD CONSTRAINT
PK_Cities PRIMARY KEY CLUSTERED
(
CityID
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
ALTER TABLE dbo.Cities SET (LOCK_ESCALATION = TABLE)
COMMIT
BEGIN TRANSACTION
CREATE TABLE dbo.Customers
(
CustomerID int NOT NULL,
Name nchar(50) NOT NULL,
Birthday nchar(10) NOT NULL,
CityID int NULL
) ON [PRIMARY]
ALTER TABLE dbo.Customers ADD CONSTRAINT
PK_Customers PRIMARY KEY CLUSTERED
(
CustomerID
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
ALTER TABLE dbo.Customers ADD CONSTRAINT
FK_Customers_Cities FOREIGN KEY
(
CityID
) REFERENCES dbo.Cities
(
CityID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
ALTER TABLE dbo.Customers SET (LOCK_ESCALATION = TABLE)
COMMIT
BEGIN TRANSACTION
CREATE TABLE dbo.Orders
(
OrderID int NOT NULL,
CustomerID int NOT NULL
) ON [PRIMARY]
ALTER TABLE dbo.Orders ADD CONSTRAINT
PK_Orders PRIMARY KEY CLUSTERED
(
OrderID
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
ALTER TABLE dbo.Orders ADD CONSTRAINT
FK_Orders_Customers FOREIGN KEY
(
CustomerID
) REFERENCES dbo.Customers
(
CustomerID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
ALTER TABLE dbo.Orders SET (LOCK_ESCALATION = TABLE)
COMMIT
BEGIN TRANSACTION
CREATE TABLE dbo.OrderItems
(
OrderID int NOT NULL,
ItemID int NOT NULL
) ON [PRIMARY]
ALTER TABLE dbo.OrderItems ADD CONSTRAINT
PK_OrderItems PRIMARY KEY CLUSTERED
(
OrderID,
ItemID
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
ALTER TABLE dbo.OrderItems ADD CONSTRAINT
FK_OrderItems_Items FOREIGN KEY
(
ItemID
) REFERENCES dbo.Items
(
ItemID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
ALTER TABLE dbo.OrderItems ADD CONSTRAINT
FK_OrderItems_Orders FOREIGN KEY
(
OrderID
) REFERENCES dbo.Orders
(
OrderID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
ALTER TABLE dbo.OrderItems SET (LOCK_ESCALATION = TABLE)
COMMIT