-
Notifications
You must be signed in to change notification settings - Fork 0
/
tables.sql
80 lines (56 loc) · 2.05 KB
/
tables.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
/******** Create Categories table with primary key ********/
Use [empowerid]
CREATE TABLE [dbo].[Categories](
[category_id] [int] IDENTITY(1,1) NOT NULL,
[category_name] [nvarchar](128) NULL,
CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED
(
[category_id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Create Products table with primary key and foreign key ********/
Use [empowerid]
CREATE TABLE [dbo].[Products](
[product_id] [int] IDENTITY(1,1) NOT NULL,
[product_name] [nvarchar](256) NOT NULL,
[category_id] [int] NOT NULL,
[price] [decimal](12, 2) NOT NULL,
[description] [nvarchar](max) NULL,
[image_url] [nvarchar](512) NULL,
[date_added] [datetime] NOT NULL,
CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED
(
[product_id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[Products] ADD CONSTRAINT [DF_Products_date_added] DEFAULT (getdate()) FOR [date_added]
GO
ALTER TABLE [dbo].[Products] WITH CHECK ADD CONSTRAINT [FK_Products_Categories] FOREIGN KEY([category_id])
REFERENCES [dbo].[Categories] ([category_id])
GO
ALTER TABLE [dbo].[Products] CHECK CONSTRAINT [FK_Products_Categories]
GO
/******** Create Orders table with primary key and foreign key ********/
Use [empowerid]
CREATE TABLE [dbo].[Orders](
[order_id] [int] IDENTITY(1,1) NOT NULL,
[product_id] [int] NOT NULL,
[order_date] [datetime] NOT NULL,
[customer_name] [nvarchar](128) NULL,
[quantity] [smallint] NOT NULL,
[amount] [decimal](12, 2) NOT NULL,
CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED
(
[order_id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Orders] ADD CONSTRAINT [DF_Orders_order_date] DEFAULT (getdate()) FOR [order_date]
GO
ALTER TABLE [dbo].[Orders] WITH CHECK ADD CONSTRAINT [FK_Orders_Products] FOREIGN KEY([product_id])
REFERENCES [dbo].[Products] ([product_id])
GO
ALTER TABLE [dbo].[Orders] CHECK CONSTRAINT [FK_Orders_Products]
GO