-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
82 lines (74 loc) · 2.8 KB
/
Copy pathschema.sql
File metadata and controls
82 lines (74 loc) · 2.8 KB
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
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Drop existing tables if they exist (for re-running the script)
IF OBJECT_ID('dbo.OrderItems', 'U') IS NOT NULL DROP TABLE dbo.OrderItems;
IF OBJECT_ID('dbo.Orders', 'U') IS NOT NULL DROP TABLE dbo.Orders;
IF OBJECT_ID('dbo.Products', 'U') IS NOT NULL DROP TABLE dbo.Products;
IF OBJECT_ID('dbo.Categories', 'U') IS NOT NULL DROP TABLE dbo.Categories;
IF OBJECT_ID('dbo.Customers', 'U') IS NOT NULL DROP TABLE dbo.Customers;
-- Categories table
CREATE TABLE dbo.Categories (
CategoryId INT IDENTITY(1,1) PRIMARY KEY,
Name NVARCHAR(100) NOT NULL,
Description NVARCHAR(500) NULL,
CreatedAt DATETIME2 NOT NULL DEFAULT GETUTCDATE(),
ModifiedAt DATETIME2 NULL
);
-- Products table
CREATE TABLE dbo.Products (
ProductId INT IDENTITY(1,1) PRIMARY KEY,
CategoryId INT NOT NULL,
Name NVARCHAR(200) NOT NULL,
Description NVARCHAR(1000) NULL,
Price DECIMAL(18,2) NOT NULL,
StockQuantity INT NOT NULL DEFAULT 0,
IsActive BIT NOT NULL DEFAULT 1,
CreatedAt DATETIME2 NOT NULL DEFAULT GETUTCDATE(),
ModifiedAt DATETIME2 NULL,
CONSTRAINT FK_Products_Categories FOREIGN KEY (CategoryId)
REFERENCES dbo.Categories(CategoryId)
);
-- Customers table
CREATE TABLE dbo.Customers (
CustomerId INT IDENTITY(1,1) PRIMARY KEY,
FirstName NVARCHAR(50) NOT NULL,
LastName NVARCHAR(50) NOT NULL,
Email NVARCHAR(100) NOT NULL UNIQUE,
Phone NVARCHAR(20) NULL,
CreatedAt DATETIME2 NOT NULL DEFAULT GETUTCDATE(),
ModifiedAt DATETIME2 NULL
);
-- Orders table
CREATE TABLE dbo.Orders (
OrderId INT IDENTITY(1,1) PRIMARY KEY,
CustomerId INT NOT NULL,
OrderDate DATETIME2 NOT NULL DEFAULT GETUTCDATE(),
TotalAmount DECIMAL(18,2) NOT NULL,
Status NVARCHAR(20) NOT NULL DEFAULT 'Pending',
ShippingAddress NVARCHAR(500) NULL,
CreatedAt DATETIME2 NOT NULL DEFAULT GETUTCDATE(),
ModifiedAt DATETIME2 NULL,
CONSTRAINT FK_Orders_Customers FOREIGN KEY (CustomerId)
REFERENCES dbo.Customers(CustomerId)
);
-- OrderItems table
CREATE TABLE dbo.OrderItems (
OrderItemId INT IDENTITY(1,1) PRIMARY KEY,
OrderId INT NOT NULL,
ProductId INT NOT NULL,
Quantity INT NOT NULL,
UnitPrice DECIMAL(18,2) NOT NULL,
Subtotal AS (Quantity * UnitPrice) PERSISTED,
CONSTRAINT FK_OrderItems_Orders FOREIGN KEY (OrderId)
REFERENCES dbo.Orders(OrderId),
CONSTRAINT FK_OrderItems_Products FOREIGN KEY (ProductId)
REFERENCES dbo.Products(ProductId)
);
-- Create indexes for better query performance
CREATE INDEX IX_Products_CategoryId ON dbo.Products(CategoryId);
CREATE INDEX IX_Orders_CustomerId ON dbo.Orders(CustomerId);
CREATE INDEX IX_OrderItems_OrderId ON dbo.OrderItems(OrderId);
CREATE INDEX IX_OrderItems_ProductId ON dbo.OrderItems(ProductId);
PRINT 'Schema created successfully';