-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLQuery CREATE.sql
167 lines (144 loc) · 4.29 KB
/
SQLQuery CREATE.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
--CREATE all tables
CREATE TABLE Account(
userID INT IDENTITY(1,1),
username VARCHAR(50),
userpassword VARCHAR(50),
isAdmin BIT,
PRIMARY KEY(userID)
);
CREATE TABLE Product(
productID INT IDENTITY(1,1),
itemNumber VARCHAR(20),
productName VARCHAR(30),
productCategory VARCHAR(40),
productColor VARCHAR(20),
size CHAR(10),
price FLOAT,
isInStock BIT,
stock INT,
taxes FLOAT,
studentDiscount FLOAT,
newDiscountedPrice FLOAT,
PRIMARY KEY (productID)
);
CREATE TABLE Customer(
customerID INT IDENTITY(1,1),
userID INT,
ssn VARCHAR(30),
firstName VARCHAR(50),
lastName VARCHAR(50),
phoneNumber VARCHAR(10),
email VARCHAR(50),
PRIMARY KEY(customerID),
FOREIGN KEY (userID) REFERENCES Account(userID),
);
CREATE TABLE [Order](
orderID INT IDENTITY(1,1),
customerID INT,
orderDate DATETIME,
shippingDate datetime,
shippingStatus VARCHAR(50),
quantity INT,
PRIMARY KEY(orderID),
FOREIGN KEY (customerID) REFERENCES Customer(customerID),
);
--middle table between Product and Orders
CREATE TABLE ProductToOrder(
productID INT,
orderID INT,
FOREIGN KEY (productID) REFERENCES Product (productID),
FOREIGN KEY (orderID) REFERENCES [Order] (orderID),
);
--Inner join Middle table Product & Order (ProductToOrder)
SELECT *
FROM Product
INNER JOIN ProductToOrder po ON product.productID = po.productID
INNER JOIN "order" AS o ON o.orderID = po.orderID
CREATE TABLE ProductCategory(
productCategoryID INT IDENTITY(1,1),
categoryName VARCHAR(40),
PRIMARY KEY(productCategoryID)
);
--middle table between product and productCategory
CREATE TABLE ProductToProductCategory(
productCategoryID INT IDENTITY(1,1),
productID INT,
FOREIGN KEY (productCategoryID) REFERENCES ProductCategory (productCategoryID),
FOREIGN KEY (productID) REFERENCES Product (productID)
);
--Inner join Middle table Product & ProductCategory (ProductToProductCategory)
SELECT *
FROM Product
INNER JOIN ProductToProductCategory pc ON product.productID =pc.productID
INNER JOIN ProductCategory ON productCategory.productCategoryID=pc.productCategoryID
CREATE TABLE ProductQuantity(
productQuantityID INT,
productID INT,
registrationDate DATETIME,
quantity INT,
PRIMARY KEY (productQuantityID),
FOREIGN KEY (productID) REFERENCES Product (productID)
);
CREATE TABLE Shipping(
shippingID INT IDENTITY(1,1),
shippingDate DATETIME NOT NULL,
shippingStatus VARCHAR(30),
shippingStreet VARCHAR(40),
shippingCity VARCHAR(40),
shippingCountry VARCHAR(40),
shippingZip INT,
PRIMARY KEY (shippingID)
);
--middle table between Order and Shipping
CREATE TABLE OrderToShipping(
orderID INT,
shippingID INT,
FOREIGN KEY (orderID) REFERENCES [Order] (orderID),
FOREIGN KEY (shippingID) REFERENCES Shipping (shippingID),
);
--Inner join Middle table orders & shipping (ordersToShipping)
SELECT* FROM [Order]
INNER JOIN OrderToShipping ots ON [order].orderID = ots.orderID
INNER JOIN shipping ON shipping.shippingID = ots.shippingID
CREATE TABLE Payment(
paymentID INT IDENTITY(1,1),
orderID INT,
paymentDetails VARCHAR(50),
totalPrice FLOAT,
timeOfPayment DATETIME,
PRIMARY KEY(paymentID),
FOREIGN KEY (orderID) REFERENCES [Order] (orderID)
);
-- middle table between orders and customers
CREATE TABLE OrderToCustomer(
orderID INT,
customerID INT,
FOREIGN KEY (orderID) REFERENCES [Order] (orderID),
FOREIGN KEY (customerID) REFERENCES Customer (customerID),
);
--Inner join Middle table orders & customer (ordersToCustomer)
SELECT* FROM [Order]
INNER JOIN OrderToCustomer otc ON [order].orderID = otc.orderID
INNER JOIN customer ON customer.customerID = otc.customerID
CREATE TABLE Address(
addressID INT IDENTITY(1,1),
customerID INT,
street VARCHAR(50),
city CHAR(40),
country CHAR(40),
zip INT,
date DATETIME,
PRIMARY KEY (addressID),
FOREIGN KEY (customerID) REFERENCES Customer(customerID)
);
--middle table between Address and Customer
CREATE TABLE AddressToCustomer(
addressID INT,
customerID INT,
FOREIGN KEY (addressID) REFERENCES [Address](addressID),
FOREIGN KEY (customerID)REFERENCES Customer(customerID)
);
--Inner join Middle table address & customer (AddressToCustomer)
SELECT* FROM [Address]
INNER JOIN AddressToCustomer atc ON [address].addressID= atc.addressID
INNER JOIN customer AS c ON c.customerID = atc.customerID