网上书店数据库设计 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

运行结果:


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