1. 程式人生 > >sql中如果存在資料庫或表則刪除資料庫或表

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_houseNewId primary key (houseNewId);
alter table houseNews add constraint DF_houseNewDate default(CONVERT(varchar(100), GETDATE(), 23)) for houseNewDate;
alter table Message add constraint pk_messageId primary key (messageId);
alter table Message add constraint DF_messageDate default(CONVERT(varchar(100), GETDATE(), 23)) for messageDate;