SQL server建表问题

xiaoshiniudao666 2015-11-21 02:22:49
use master
go
if DB_ID('NIKE') is not null
drop database NIKE
GO
create database NIKE
go
use NIKE
go
if OBJECT_ID('Type') is not null
drop table Type
-------这个地方一定要先建库,再建表?
go
create table Type
(
TypeID int not null primary key identity(1,1),
TypeName nvarchar(50) not null,
parentID int references Type(TypeID)
)
go
if OBJECT_ID('Goods') is not null
drop table Goods
go
create table Goods
(
GoodsID int primary key identity(1,1) not null,
BarCode nvarchar(6) not null unique,
TypeID int not null references Type(TypeID),
GoodsName nvarchar(50) not null,
StorePrice decimal(8,2),
SalePrice decimal(8,2),
Discount decimal(4,2),
StockNum decimal ,
StokDate date default(getdate())
)
go
if OBJECT_ID('Customer') is not null
drop table Customer
go
create table Customer
(
CustomerID int not null primary key identity(1,1),
LoginID nvarchar(50) not null,
Pwd nvarchar(50) not null,
CustomerName nvarchar(50) not null,
IdentityID nvarchar(18),
Address nvarchar(50),
Phone nvarchar(50)
)
go
if OBJECT_ID('Salesman') is not null
drop table Salesman
go
create table Salesman
(
SalesmanID int not null primary key identity(1,1),
SalesmanName nvarchar(10) not null,
Mobile nvarchar(20) not null unique,
Pwd nvarchar(20) not null,
Gender int not null references Customer(CustomerID),
Wage decimal(8,2),
CommissionRate decimal(8,2),
Role nvarchar(20)
)
go
if OBJECT_ID('Sales') is not null
drop table Sales
go
create table Sales
(
SalesID int not null primary key identity(1,1),
ReceiptsCode nvarchar(14) unique,
SalesDate date not null default(getdate()),
Amount decimal(8,2),
SalesmanID int references Salesman(SalesmanID),
CasherID int references Salesman(SalesmanID)
)
...全文
47 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
xiaoshiniudao666 2015-11-22
我想说的是,一定要先判断表是否存在,才能建表吗?
回复
卖水果的net 2015-11-22
不是必须判断表是否存在,才能建表,你最前是先删除了库,再新建的,所以也不会有表,但是按惯例,最好是事先判断一下比较好。
回复
唐诗三百首 2015-11-21
红色字体代码的逻辑是判断Type对象是否存在,如果存在则删除该Type表.
回复
相关推荐
发帖
疑难问题
创建于2007-09-28

2.1w+

社区成员

MS-SQL Server 疑难问题
申请成为版主
帖子事件
创建了帖子
2015-11-21 02:22
社区公告
暂无公告