반응형
-- This script requires SQL Server 2008
CREATE DATABASE Email;
USE testEmail;
-- We store messages in folders
CREATE TABLE Folder
( Id INT PRIMARY KEY IDENTITY
, ParentId INT FOREIGN KEY REFERENCES Folder(Id)
, DisplayName VARCHAR(64) NOT NULL
, Description TEXT
, CONSTRAINT nk_Folder UNIQUE (ParentId, DisplayName)
);
-- an address is a unique emaill address
CREATE TABLE Address
( Id INT PRIMARY KEY IDENTITY
, FriendlyName VARCHAR(128)
, UserName VARCHAR(64) NOT NULL -- always store in lower case
, DomainName VARCHAR(128) NOT NULL -- always store in lower case
, DisplayName AS
CASE WHEN FriendlyName IS NULL THEN
UserName + '@' + DomainName
ELSE
FriendlyName + ' <' + UserName + '@' + DomainName + '>'
END
, CONSTRAINT nk_Address UNIQUE (UserName, DomainName)
);
-- Each message represents a single email
CREATE TABLE Message
( Id INT PRIMARY KEY IDENTITY
, FolderId INT FOREIGN KEY REFERENCES Folder(Id) NOT NULL
, SenderId INT FOREIGN KEY REFERENCES Address(Id) NOT NULL
, SentOn DATETIME NOT NULL
, Subject VARCHAR(1024) NOT NULL
, EmailHeader TEXT
, EmailBody TEXT
, IsUnread BIT NOT NULL DEFAULT 1
, IsJunk BIT NOT NULL DEFAULT 0
, IsStar BIT NOT NULL DEFAULT 0
, Priority TINYINT NOT NULL DEFAULT 3
CHECK (Priority IN (1,2,3,4,5))
);
-- One entry for each header (to/cc/bcc) on a message
CREATE TABLE MessageAddress
( MessageId INT FOREIGN KEY REFERENCES Message(Id) NOT NULL
, HeaderType CHAR(4) NOT NULL
CHECK (HeaderType IN ('to', 'from', 'cc', 'bcc', 'r-to'))
, AddressId INT FOREIGN KEY REFERENCES Address(Id) NOT NULL
, CONSTRAINT pk_MessageAddress
PRIMARY KEY (MessageId, HeaderType, AddressId)
);
-- An attachment is a single binary file
CREATE TABLE Attachment
( Id INT PRIMARY KEY IDENTITY
, FileName VARCHAR(512) NOT NULL
, Extension VARCHAR(64) NOT NULL
, Content VARBINARY(MAX) NOT NULL
, FileSize BIGINT NOT NULL
, Hash CHAR(32) NOT NULL
, CONSTRAINT nk_Attachment UNIQUE (Hash, FileSize)
);
-- Join Table
CREATE TABLE MessageAttachment
( MessageId INT NOT NULL
FOREIGN KEY REFERENCES Message(Id)
, AttachmentId INT NOT NULL
FOREIGN KEY REFERENCES Attachment(Id)
, CONSTRAINT pk_MessageAttachment
PRIMARY KEY (MessageId, AttachmentId)
);
DECLARE @RootId INT;
SET @RootId = 1;
SET IDENTITY_INSERT Folder ON;
INSERT INTO Folder (Id, DisplayName)
VALUES (@RootId, '');
SET IDENTITY_INSERT Folder OFF;
-- some root level folders
INSERT INTO
Folder (ParentId, DisplayName)
VALUES (@RootId, 'Inbox')
, (@RootId, 'Outbox')
, (@RootId, 'Sent')
, (@RootId, 'Trash')
, (@RootId, 'Drafts')
;
DECLARE @InboxId INT;
SET @InboxId = (
SELECT Id
FROM Folder
WHERE ParentId = @RootId
AND DisplayName = 'Inbox'
);
-- a few sub folders
INSERT INTO
Folder (DisplayName, ParentId)
VALUES ('Home', @InboxId)
, ('School', @InboxId)
, ('Work', @InboxId)
;
-- I guess we should a couple of addresses
INSERT INTO
Address (FriendlyName, UserName, DomainName)
VALUES ('Brent Larsen', 'brentoboy', 'gmail.com')
, (NULL, 'jimbob', 'someplace.com')
'Database > MS-SQL' 카테고리의 다른 글
CREATE PROCEDURE !! DECLARE !! EXEC !! OUTPUT !! (0) | 2009.03.28 |
---|---|
TRUNCATE TABLE(Transact-SQL) (0) | 2009.03.27 |
SQL @@Error 그리고 TRANSACTION BEGIN TRAN ROLBACK COMMIT IF ELSE문 사용 예 (0) | 2009.03.26 |
땡큐 트리거 TRIGGER 트리거 SQL TRIGGER (0) | 2009.02.24 |
엑셀을 MSSQL에 바로 올리기 excel 을 바로 DB에 올리기 엑셀 데이터 베이스로 가져오기 SSMS (0) | 2009.02.10 |
db 데이터 베이스 백업 backup 복원 백업 복원 (0) | 2009.01.13 |
SELECT @@IDENTITY 최근에 들어간 PK값을 돌려주는 것 (0) | 2009.01.09 |
DB 정리 쿼리교도소탈출 집계 함수 (0) | 2008.11.12 |