日期:2014-05-16  浏览次数:20470 次

sql中如果存在数据库或表则删除数据库或表

--房屋发布系统

--注意:使用该sql语句生成数据库和表时,必须先在E盘下创建名为houseRental 的文件夹;

USE master
GO
if exists(select * from dbo.sysdatabases where name='houseRental')
	drop database houseRental
GO
CREATE DATABASE houseRental
ON 
( 
   NAME = 'houseRental_data',
   FILENAME = 'E:\houseRental\houseRental.mdf',
   SIZE = 10,
   MAXSIZE = 50,
   FILEGROWTH = 5 
)
LOG ON( 
	NAME = 'houseRental_log',
	FILENAME = 'E:\houseRental\houseRental.ldf',
	SIZE = 5MB,
	MAXSIZE = 25MB,
	FILEGROWTH = 5MB 
)
GO

USE houseRental
GO
--用户信息表;
if exists(select * from dbo.sysobjects where name='userInfo')
	drop table userInfo
GO
create table userInfo(
	userId 					int not null,				--自增长ID
	userName 				varchar(15) not null,		--用户名,唯一性
	password 				varchar(15) not null,		--用户密码
	email 					varchar(25) not null,		--邮箱地址
	userRole 				int,						--默认为普通会员(0:默认为普通会员,1:为管理员)
	registDate 				varchar(25)					--默认当前时间
)

--房屋信息表;
if exists(select * from dbo.sysobjects where name='houseInfo')
	drop table houseInfo
GO
create table houseInfo(
	houseId 				int not null,				--自增长ID
	houseNumber				varchar(15) not null,		--房屋编号
	userName				varchar(15) not null,		--房屋发布会员
	employNumber			varchar(15) not null,		--房屋对应负责销售员工编号(唯一主键)
	informationType 		int not null,				--判断是发布出租/出售房屋类型(0代表发布出租信息,1代表发布出售信息)
	houseOwnerName 			varchar(15) not null, 		--业主姓名
	houseOwnerSex 			int not null,				--业主性别(0:代表男,1:代表女)
	houseOwnerPhoneNumber 	varchar(15),   				--联系电话
	houseArea 				varchar(15) not null,		--房屋所在区域
	houseSize 				int not null,				--房屋面积
	houseFloor 				int,						--房屋出租/出售 楼层
	houseTotalFloor 		int, 						--房屋总楼层数
	HouseUnit				varchar(15), 				--房屋户型(一室一厅,二室一厅,)[数据存储值为:一室二厅]
	BudgetPrice 			float, 						--预算价格
	SpecificAddress 		varchar(200) not null,		--房屋具体地址
	houseFloorType 			int not null,				--楼型,,[0:代表高层,1:代表多层,2:平房,3:其他]
	otherRequirements 		varchar(200)				--其他信息
)

--员工信息表;
if exists(select * from dbo.sysobjects where name='employInfo')
	drop table employInfo
GO
create table employInfo(
	employId 				int not null,				--自增长ID
	employNumber 			varchar(15) not null,		--员工编号
	employPassWord			varchar(15) not null,		--员工密码,默认为6个8;
	employRealName			varchar(15) not null,		--员工真实姓名
	employPhoneNumber		varchar(15) not null		--员工联系电话			
)

--员工业绩表;
if exists(select * from dbo.sysobjects where name='employResult')
	drop table employResult
GO
create table employResult(
	employResultId			int not null,				--自增长ID
	employResultNumber		varchar(15) not null,		--业绩编号
	employNumber			varchar(15) not null,		--员工编号
	houseNumber				varchar(15) not null,		--房屋编号
	houseAmount				float,						--房屋售价/出租价
	percentPaid				float,						--销售提成比例(按总金额的20%拿提成)
	salesCompleted			varchar(15)					--销售完成时间
)

--房产咨询表;
if exists(select * from dbo.sysobjects where name='houseNews')
	drop table houseNews
GO
create table houseNews(
	houseNewId			int not null,					--自增长ID
	houseNewTheme		varchar(200) not null,			--咨询主题
	houseNewContent		text,							--咨询内容
	houseNewDate		varchar(15)						--发布日期
)

--留言评论表
if exists(select * from dbo.sysobjects where name='Message')
	drop table Message
GO
create table Message(
	messageId			int not null,					--自增长ID
	houseNumber			varchar(15) not null,			--房屋出租/出售编号(针对房屋编号留言)
	messageContent		varchar(200) not null,			--留言内容
	contact				varchar(50),					--联系方式
	messageDate			varchar(15) not null			--留言时间
)

--添加约束
alter table userInfo add constraint pk_userId primary key (userId);
alter table userInfo add constraint DF_userRole default(0) for userRole;
alter table userInfo add constraint DF_registDate default(CONVERT(varchar(100), GETDATE(), 23)) for registDate;
alter table houseInfo add constraint pk_houseId primary key (houseId);
alter table employInfo add constraint pk_employId primary key (employId);
alter table employInfo add constraint DF_employPassWord default('888888') for employPassWord;
alter table employResult add constraint pk_employResultId primary key (employResultId);
alter table houseNews add constraint pk_hous