-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathSQL Query Model.sql
311 lines (275 loc) · 10 KB
/
SQL Query Model.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*==============================================================*/
/* Author: Soumyadip Chowdhury Github: Soumyadip007 */
/*==============================================================*/
if exists (select 1
from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
where r.fkeyid = object_id('"Order"') and o.name = 'FK_ORDER_REFERENCE_CUSTOMER')
alter table "Order"
drop constraint FK_ORDER_REFERENCE_CUSTOMER
go
if exists (select 1
from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
where r.fkeyid = object_id('OrderItem') and o.name = 'FK_ORDERITE_REFERENCE_ORDER')
alter table OrderItem
drop constraint FK_ORDERITE_REFERENCE_ORDER
go
if exists (select 1
from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
where r.fkeyid = object_id('OrderItem') and o.name = 'FK_ORDERITE_REFERENCE_PRODUCT')
alter table OrderItem
drop constraint FK_ORDERITE_REFERENCE_PRODUCT
go
if exists (select 1
from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
where r.fkeyid = object_id('Product') and o.name = 'FK_PRODUCT_REFERENCE_SUPPLIER')
alter table Product
drop constraint FK_PRODUCT_REFERENCE_SUPPLIER
go
if exists (select 1
from sysindexes
where id = object_id('Customer')
and name = 'IndexCustomerName'
and indid > 0
and indid < 255)
drop index Customer.IndexCustomerName
go
if exists (select 1
from sysobjects
where id = object_id('Customer')
and type = 'U')
drop table Customer
go
if exists (select 1
from sysindexes
where id = object_id('"Order"')
and name = 'IndexOrderOrderDate'
and indid > 0
and indid < 255)
drop index "Order".IndexOrderOrderDate
go
if exists (select 1
from sysindexes
where id = object_id('"Order"')
and name = 'IndexOrderCustomerId'
and indid > 0
and indid < 255)
drop index "Order".IndexOrderCustomerId
go
if exists (select 1
from sysobjects
where id = object_id('"Order"')
and type = 'U')
drop table "Order"
go
if exists (select 1
from sysindexes
where id = object_id('OrderItem')
and name = 'IndexOrderItemProductId'
and indid > 0
and indid < 255)
drop index OrderItem.IndexOrderItemProductId
go
if exists (select 1
from sysindexes
where id = object_id('OrderItem')
and name = 'IndexOrderItemOrderId'
and indid > 0
and indid < 255)
drop index OrderItem.IndexOrderItemOrderId
go
if exists (select 1
from sysobjects
where id = object_id('OrderItem')
and type = 'U')
drop table OrderItem
go
if exists (select 1
from sysindexes
where id = object_id('Product')
and name = 'IndexProductName'
and indid > 0
and indid < 255)
drop index Product.IndexProductName
go
if exists (select 1
from sysindexes
where id = object_id('Product')
and name = 'IndexProductSupplierId'
and indid > 0
and indid < 255)
drop index Product.IndexProductSupplierId
go
if exists (select 1
from sysobjects
where id = object_id('Product')
and type = 'U')
drop table Product
go
if exists (select 1
from sysindexes
where id = object_id('Supplier')
and name = 'IndexSupplierCountry'
and indid > 0
and indid < 255)
drop index Supplier.IndexSupplierCountry
go
if exists (select 1
from sysindexes
where id = object_id('Supplier')
and name = 'IndexSupplierName'
and indid > 0
and indid < 255)
drop index Supplier.IndexSupplierName
go
if exists (select 1
from sysobjects
where id = object_id('Supplier')
and type = 'U')
drop table Supplier
go
/*==============================================================*/
/* Table: Customer */
/*==============================================================*/
create table Customer (
Id int identity,
FirstName nvarchar(40) not null,
LastName nvarchar(40) not null,
City nvarchar(40) null,
Country nvarchar(40) null,
Phone nvarchar(20) null,
constraint PK_CUSTOMER primary key (Id)
)
go
/*==============================================================*/
/* Index: IndexCustomerName */
/*==============================================================*/
create index IndexCustomerName on Customer (
LastName ASC,
FirstName ASC
)
go
/*==============================================================*/
/* Table: "Order" */
/*==============================================================*/
create table "Order" (
Id int identity,
OrderDate datetime not null default getdate(),
OrderNumber nvarchar(10) null,
CustomerId int not null,
TotalAmount decimal(12,2) null default 0,
constraint PK_ORDER primary key (Id)
)
go
/*==============================================================*/
/* Index: IndexOrderCustomerId */
/*==============================================================*/
create index IndexOrderCustomerId on "Order" (
CustomerId ASC
)
go
/*==============================================================*/
/* Index: IndexOrderOrderDate */
/*==============================================================*/
create index IndexOrderOrderDate on "Order" (
OrderDate ASC
)
go
/*==============================================================*/
/* Table: OrderItem */
/*==============================================================*/
create table OrderItem (
Id int identity,
OrderId int not null,
ProductId int not null,
UnitPrice decimal(12,2) not null default 0,
Quantity int not null default 1,
constraint PK_ORDERITEM primary key (Id)
)
go
/*==============================================================*/
/* Index: IndexOrderItemOrderId */
/*==============================================================*/
create index IndexOrderItemOrderId on OrderItem (
OrderId ASC
)
go
/*==============================================================*/
/* Index: IndexOrderItemProductId */
/*==============================================================*/
create index IndexOrderItemProductId on OrderItem (
ProductId ASC
)
go
/*==============================================================*/
/* Table: Product */
/*==============================================================*/
create table Product (
Id int identity,
ProductName nvarchar(50) not null,
SupplierId int not null,
UnitPrice decimal(12,2) null default 0,
Package nvarchar(30) null,
IsDiscontinued bit not null default 0,
constraint PK_PRODUCT primary key (Id)
)
go
/*==============================================================*/
/* Index: IndexProductSupplierId */
/*==============================================================*/
create index IndexProductSupplierId on Product (
SupplierId ASC
)
go
/*==============================================================*/
/* Index: IndexProductName */
/*==============================================================*/
create index IndexProductName on Product (
ProductName ASC
)
go
/*==============================================================*/
/* Table: Supplier */
/*==============================================================*/
create table Supplier (
Id int identity,
CompanyName nvarchar(40) not null,
ContactName nvarchar(50) null,
ContactTitle nvarchar(40) null,
City nvarchar(40) null,
Country nvarchar(40) null,
Phone nvarchar(30) null,
Fax nvarchar(30) null,
constraint PK_SUPPLIER primary key (Id)
)
go
/*==============================================================*/
/* Index: IndexSupplierName */
/*==============================================================*/
create index IndexSupplierName on Supplier (
CompanyName ASC
)
go
/*==============================================================*/
/* Index: IndexSupplierCountry */
/*==============================================================*/
create index IndexSupplierCountry on Supplier (
Country ASC
)
go
alter table "Order"
add constraint FK_ORDER_REFERENCE_CUSTOMER foreign key (CustomerId)
references Customer (Id)
go
alter table OrderItem
add constraint FK_ORDERITE_REFERENCE_ORDER foreign key (OrderId)
references "Order" (Id)
go
alter table OrderItem
add constraint FK_ORDERITE_REFERENCE_PRODUCT foreign key (ProductId)
references Product (Id)
go
alter table Product
add constraint FK_PRODUCT_REFERENCE_SUPPLIER foreign key (SupplierId)
references Supplier (Id)
go