網上書店數據庫設計 SQL Server 2008 R2

使用關係型數據庫SQL Server 2008 R2,通過腳本的方式新建網上書店的數據庫BookShop

BookShop共包含9張表,分別爲BookType(圖書類別表),Book(圖書信息表),CustomerType(會員級別表),CUstomer(會員表),SysUser(系統用戶表),LeaveWord(留言反饋表),ShoppingCart(購物車記錄表),ShoppingCartList(購物車商品清單表),TOrder(訂單信息表)。


具體實現如下:

USE master--使用系統
GO
CREATE DATABASE BookShop --創建數據庫

ON PRIMARY	--主文件
(
	NAME='BookShop_data', --文件名
	FILENAME='D:\SQLProject\BookShop_data.mdf',--路徑
	SIZE=5MB,--初始大小
	MAXSIZE=200MB,--最大容量
	FILEGROWTH=10%--增長速度
)

LOG ON--日誌文件
(
	NAME='BookShop_log',
	FILENAME='D:\SQLProject\BookShop_log.ldf',
	SIZE=20MB,
	FILEGROWTH=10%
)
GO


--創建表:會員表Customer
use BookShop
go

create table Customer
(
	Id int not null primary key,--會員標識,主鍵
	CustomerTypeID int not null foreign key(Id) references CustomerType(Id),--會員級別ID,外鍵
	Realname varchar(50) not null,--真實姓名
	LoginName varchar(50) not null,--會員賬號
	Password varchar(20) not null,--會員密碼
	Address varchar(100) not null,--通信地址
	LinkTel varchar(20) not null,--聯繫電話
	Postalcode varchar(8) not null,--郵政編碼
	Reg_date Date not null,--註冊日期
	RecentlyLoginDate Date not null,--最近登錄日期
	LoginNum int not null,--登錄次數
	LinkMail varchar(50) null--聯繫郵件
 )
go

--增加對登陸次數的約束
alter table Customer with nocheck
add constraint CK_LNum
check (LoginNum>=0)
go


--創建表:會員級別表CustomerType
use BookShop
go

create table CustomerType
(
	Id int not null primary key,--級別標識
	Name varchar(50) not null,--級別名稱
	Percentage Float(8) not null--打折比率
)
go

--增加對打折比率的約束
alter table CustomerType with nocheck
add constraint CK_Percent
check (Percentage>=0)
go


--創建表:圖書信息表Book
create table Book
(
	Id int not null primary key,--圖書標識,主鍵
	Name varchar(100) not null,--圖書名稱
	BookTypeID int not null foreign key(Id) references BookType(Id),--圖書類別號,外鍵
	Price Money null,--圖書價格
	SpecialPrice Money not null,--物價圖書價格
	StockNum int null,--庫存量
	Pic varchar(100) null, --封面圖書地址
	PublishCompany varchar(100) null,--出版社
	PublishDate Date null,--出版日期
	Author varchar(50) null,--作者
	IsCommend Bit not null,--是否爲推薦圖書
	IsSpecial Bit not null--是否爲特價圖書
)
go

--增加對圖書庫存量的約束
alter table Book with nocheck
add constraint CK_Stock
check (StockNum>=0)
go



--創建表:訂單信息表Order
use BookShop
go
create table Torder
(
	Id int not null primary key,--訂單標識,主鍵
	CustomerID int not null foreign key(Id) references Customer(Id),--會員ID,外鍵
	ShoppingCartID int not null foreign key(Id) references ShoppingCart(Id),--購物車ID,外鍵
	OrderCode varchar(10) not null,--訂單編號
	OrderDate Date not null,--訂單日期
	OrderState varchar(50) not null--訂單狀態
)
go


--創建表:購物車記錄表ShoppingCart
create table ShoppingCart
(
	Id int not null primary key,--購物車標識
	CustomerID int not null foreign key(Id) references Customer(Id),--會員ID
	TotalPrice Money not null,--購物車總金額
	State Bit not null--購物車狀態
)
go

--增加對購物車總金額的約束
alter table ShoppingCart with nocheck
add constraint CK_TotalPrice
check (TotalPrice>=0)
go


--創建表:購物車商品清單表ShoppingCartList
use BookShop
go
create table ShoppingCartList
(
	Id int not null primary key,--清單標識,主鍵
	BookID int not null foreign key(Id) references Book(Id),--圖書ID,外鍵
	ShoppingCartID int not null foreign key(Id) references ShoppingCart(Id),--購物車ID,外鍵
	BookNum int not null,--圖書數量
	BookPrice Money not null--圖書價格
)
go

--增加對圖書價格的約束
alter table ShoppingCartList with nocheck
add constraint CK_Price
check (BookPrice>0)
go

--增加對圖書數量的約束
alter table ShoppingCartList with nocheck
add constraint CK_BNum
check (BookNum>=0)
go

--創建表:圖書類別表BookType
use BookShop
go
create table BookType
(
	Id int not null primary key,--類別標識,主鍵
	ParentID int not null, --父類別ID
	Name varchar(50) not null,--類別名稱
	Description varchar(100) null,--類別描述
	Orders int null--排序號
)
go

--增加對圖書排序號的約束,從0開始
alter table BookType with nocheck
add constraint CK_Order
check (Orders>=0)
go


--創建表:留言反饋表LeaveWord
use BookShop
go
create table LeaveWord
(
	Id int not null primary key,--留言標識,主鍵
	CustomerID int not null foreign key(Id) references Customer(Id),--會員ID,外鍵
	SysUserID int null foreign key(Id) references SysUser(Id),--系統管理員ID,外鍵
	Subject varchar(100) not null,--留言主題
	LeaveContent varchar(500) not null,--留言內容
	LeaveDate Date not null,--留言日期
	ReplyContent varchar(500) null,--回覆內容
	ReplyDate Date null--回覆日期
)
go


--創建表:系統用戶表SysUser
use BookShop
go
create table SysUser
(
	Id int not null primary key,--系統用戶標識,主鍵
	RealName varchar(50) not null,--真實姓名
	LoginName varchar(50) not null,--系統用戶賬號
	Password varchar(20) not null,--系統用戶密碼
	Address varchar(100) not null,--通信地址
	Role varchar(20) not null,--管理員角色
	HireDate Date not null--僱用日期
)
go

運行結果:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章