
-- =============================================
-- ylb:電子商務(wù)模板
-- author:YUANBO
-- development time:2011-11-9
-- thank you:LiuGaiZhen
-- =============================================
USE master
GO
-- Drop the database if it already exists
IF EXISTS (
SELECT name
FROM sys.databases
WHERE name = N'EShop'
)
DROP DATABASE EShop
GO
CREATE DATABASE EShop
GO
use EShop
go
-- =============================================
-- 1,供應(yīng)商
-- =============================================
create table Suppliers
(
SupplierID int identity(1,1) primary key, --供應(yīng)商ID [PK]
CompanyName nvarchar(40) not null, --公司名稱
ContactName nvarchar(30), --聯(lián)系人姓名
ContactTitle nvarchar(30), --聯(lián)系人頭銜
[Address] nvarchar(60), --地址
City nvarchar(15), --城市
Region nvarchar(15), --地區(qū)
PostalCode nvarchar(15), --郵政編碼
Country nvarchar(24), --國(guó)家
Phone nvarchar(24), --電話
Fax nvarchar(24), --傳真
HomePage ntext --主頁(yè)
)
go
-- =============================================
-- 2,類別
-- =============================================
create table Categories
(
CategoryID int identity(1,1) primary key, --類別ID [PK]
CategoryName nvarchar(15) not null, --類別名稱
[Description] ntext, --說(shuō)明
Picture image --圖片
)
go
-- =============================================
--3,產(chǎn)品
-- =============================================
create table Products
(
ProductID int identity primary key, --產(chǎn)品ID『PK』
ProductName nvarchar(40) not null, --產(chǎn)品名稱
SupplierID int foreign key references Suppliers(SupplierID), --供應(yīng)商ID
CategoryID int foreign key references Categories(CategoryID), --類別ID
QuantityPerUnit nvarchar(20), --單位數(shù)量
UnitPrice money, --單價(jià)
UnitsInStock smallint default(0) check(UnitsInStock>=0), --庫(kù)存量
UnitsOnOrder smallint default(0) check(UnitsOnOrder>=0), --訂購(gòu)量
ReorderLevel smallint default(0) check(ReorderLevel>=0), --再訂購(gòu)量
Discontinued bit --中止
)
go
-- =============================================
-- 4,訂單明細(xì)
-- =============================================
create table OrderDetails
(
OrderID int identity(1,1), --訂單ID
ProductID int, --產(chǎn)品ID
UnitPrice money not null, --單價(jià)
Quantity smallint not null, --數(shù)量
Discount real not null, --折扣
primary key(OrderID,ProductID) --聯(lián)合主鍵
)
go
-- =============================================
-- 5,雇員
-- P:1,ReportsTo; 2,baseID
-- =============================================
create table Employees
(
EmployeeID int identity(1,1) primary key, --雇員ID【PK】
lastName nvarchar(20) not null, --姓氏
FirstName nvarchar(10) not null, --名字
Title nvarchar(30), --頭銜
TitleOfCourtesy nvarchar(25), --尊稱
BirthDate datetime, --出生日期
HireDate datetime, --雇傭日期
[Address] nvarchar(50), --地址
City nvarchar(15), --城市
Region nvarchar(15), --地區(qū)
PostalCode nvarchar(10), --郵政編碼
Country nvarchar(15), --國(guó)家
HomePhone nvarchar(24), --家庭電話
Extension nvarchar(4), --分機(jī)
Photo image, --照片
Notes ntext, --備注
--ReportsTo int FK
PhotoPath nvarchar(255) --圖片地址
--baseID --上級(jí)編號(hào)
)
go
-- =============================================
-- 6,客戶
-- =============================================
create table Customers
(
CustomerID nchar(5) primary key, --客戶ID【PK】
CompanyName nvarchar(40) not null, --公司名稱
ContactName nvarchar(30), --聯(lián)系人姓名
ContactTitle nvarchar(30), --聯(lián)系人頭銜
[Address] nvarchar(60), --地址
City nvarchar(15), --城市
Region nvarchar(15), --地區(qū)
PostalCode nvarchar(15),--郵政編號(hào)
Country nvarchar(24), --國(guó)家
Phone nvarchar(24), --電話
Fax nvarchar(24) --傳真
)
go
-- =============================================
-- 7,客戶演示圖形
-- =============================================
create table CustomerDemoGraphics
(
CustomerTypeID nchar(10) primary key, --客戶演示圖形ID 【PK】
CustomerDesc ntext --客戶描述
)
go
-- =============================================
-- 7,客戶演示圖形
-- =============================================
create table CustomerCustomerDemo
(
CustomerID nchar(5) foreign key references Customers(CustomerID), --客戶ID【PK,F(xiàn)K】
CustomerTypeID nchar(10) foreign key references CustomerDemoGraphics(CustomerTypeID), --客戶演示圖形ID【PK,F(xiàn)K】
primary key(CustomerID,CustomerTypeID)
)
go
-- =============================================
-- 7,訂單
-- =============================================
create table Orders
(
OrderID int identity primary key, --訂單ID【PK】
CustomerID nchar(5) foreign key references Customers(CustomerID), --客戶ID【FP】
EmployeeID int foreign key references Employees(EmployeeID), --雇員ID【FP】
OrderDate datetime, --訂購(gòu)日期
RequiredDate datetime, --到貨日期
ShippedDate datetime, --發(fā)貨日期
--ShipVia int FK --運(yùn)貨商
Fright money, --運(yùn)貨費(fèi)
ShipName nvarchar(15), --貨主名稱
ShipAddress nvarchar(60), --貨主地址
ShipCity nvarchar(15), --貨主城市
ShipRegion nvarchar(15), --貨主地區(qū)
ShipPostalCode nvarchar(10),--貨主郵政編碼
ShipContry nvarchar(15) --貨主國(guó)家
)
-- =============================================
-- 8,運(yùn)貨商
-- =============================================
create table Shippers
(
ShipperID int identity primary key, --運(yùn)貨商ID【PK】
CompanyName nvarchar(40) not null, --公司名稱
Phone nvarchar(24) --電話
)
print 'ylb, tech 創(chuàng)建電子商務(wù)數(shù)據(jù)庫(kù)完成'
