|
| 1 | +CREATE DATABASE [Blog] |
| 2 | +GO |
| 3 | + |
| 4 | +USE [Blog] |
| 5 | +GO |
| 6 | + |
| 7 | +-- DROP TABLE [User] |
| 8 | +-- DROP TABLE [Role] |
| 9 | +-- DROP TABLE [UserRole] |
| 10 | +-- DROP TABLE [Post] |
| 11 | +-- DROP TABLE [Category] |
| 12 | +-- DROP TABLE [Tag] |
| 13 | +-- DROP TABLE [PostTag] |
| 14 | + |
| 15 | +CREATE TABLE [User] ( |
| 16 | + [Id] INT NOT NULL IDENTITY(1, 1), |
| 17 | + [Name] NVARCHAR(80) NOT NULL, |
| 18 | + [Email] VARCHAR(200) NOT NULL, |
| 19 | + [PasswordHash] VARCHAR(255) NOT NULL, |
| 20 | + [Bio] TEXT NOT NULL, |
| 21 | + [Image] VARCHAR(2000) NOT NULL, |
| 22 | + [Slug] VARCHAR(80) NOT NULL, |
| 23 | + |
| 24 | + CONSTRAINT [PK_User] PRIMARY KEY([Id]), |
| 25 | + CONSTRAINT [UQ_User_Email] UNIQUE([Email]), |
| 26 | + CONSTRAINT [UQ_User_Slug] UNIQUE([Slug]) |
| 27 | +) |
| 28 | +CREATE NONCLUSTERED INDEX [IX_User_Email] ON [User]([Email]) |
| 29 | +CREATE NONCLUSTERED INDEX [IX_User_Slug] ON [User]([Slug]) |
| 30 | + |
| 31 | +CREATE TABLE [Role] ( |
| 32 | + [Id] INT NOT NULL IDENTITY(1, 1), |
| 33 | + [Name] VARCHAR(80) NOT NULL, |
| 34 | + [Slug] VARCHAR(80) NOT NULL, |
| 35 | + |
| 36 | + CONSTRAINT [PK_Role] PRIMARY KEY([Id]), |
| 37 | + CONSTRAINT [UQ_Role_Slug] UNIQUE([Slug]) |
| 38 | +) |
| 39 | +CREATE NONCLUSTERED INDEX [IX_Role_Slug] ON [Role]([Slug]) |
| 40 | + |
| 41 | +CREATE TABLE [UserRole] ( |
| 42 | + [UserId] INT NOT NULL, |
| 43 | + [RoleId] INT NOT NULL, |
| 44 | + |
| 45 | + CONSTRAINT [PK_UserRole] PRIMARY KEY([UserId], [RoleId]) |
| 46 | +) |
| 47 | + |
| 48 | +CREATE TABLE [Category] ( |
| 49 | + [Id] INT NOT NULL IDENTITY(1, 1), |
| 50 | + [Name] VARCHAR(80) NOT NULL, |
| 51 | + [Slug] VARCHAR(80) NOT NULL, |
| 52 | + |
| 53 | + CONSTRAINT [PK_Category] PRIMARY KEY([Id]), |
| 54 | + CONSTRAINT [UQ_Category_Slug] UNIQUE([Slug]) |
| 55 | +) |
| 56 | +CREATE NONCLUSTERED INDEX [IX_Category_Slug] ON [Category]([Slug]) |
| 57 | + |
| 58 | +CREATE TABLE [Post] ( |
| 59 | + [Id] INT NOT NULL IDENTITY(1, 1), |
| 60 | + [CategoryId] INT NOT NULL, |
| 61 | + [AuthorId] INT NOT NULL, |
| 62 | + [Title] VARCHAR(160) NOT NULL, |
| 63 | + [Summary] VARCHAR(255) NOT NULL, |
| 64 | + [Body] TEXT NOT NULL, |
| 65 | + [Slug] VARCHAR(80) NOT NULL, |
| 66 | + [CreateDate] DATETIME NOT NULL DEFAULT(GETDATE()), |
| 67 | + [LastUpdateDate] DATETIME NOT NULL DEFAULT(GETDATE()), |
| 68 | + |
| 69 | + CONSTRAINT [PK_Post] PRIMARY KEY([Id]), |
| 70 | + CONSTRAINT [FK_Post_Category] FOREIGN KEY([CategoryId]) REFERENCES [Category]([Id]), |
| 71 | + CONSTRAINT [FK_Post_Author] FOREIGN KEY([AuthorId]) REFERENCES [User]([Id]), |
| 72 | + CONSTRAINT [UQ_Post_Slug] UNIQUE([Slug]) |
| 73 | +) |
| 74 | +CREATE NONCLUSTERED INDEX [IX_Post_Slug] ON [Post]([Slug]) |
| 75 | + |
| 76 | +CREATE TABLE [Tag] ( |
| 77 | + [Id] INT NOT NULL IDENTITY(1, 1), |
| 78 | + [Name] VARCHAR(80) NOT NULL, |
| 79 | + [Slug] VARCHAR(80) NOT NULL, |
| 80 | + |
| 81 | + CONSTRAINT [PK_Tag] PRIMARY KEY([Id]), |
| 82 | + CONSTRAINT [UQ_Tag_Slug] UNIQUE([Slug]) |
| 83 | +) |
| 84 | +CREATE NONCLUSTERED INDEX [IX_Tag_Slug] ON [Tag]([Slug]) |
| 85 | + |
| 86 | +CREATE TABLE [PostTag] ( |
| 87 | + [PostId] INT NOT NULL, |
| 88 | + [TagId] INT NOT NULL, |
| 89 | + |
| 90 | + CONSTRAINT PK_PostTag PRIMARY KEY([PostId], [TagId]) |
| 91 | +) |
0 commit comments