创建SQLQuery医院挂号数据库、数据表

USE master
go
if exists(select * from sysdatabases where name='hospital')
drop database hospital
GO
create database hospital
on primary
(name='hospital_data',
filename='G:\project\hospital_data.mdf', 
size=10mb,
maxsize=100mb,
filegrowth=10%)
log on
(name='hospital_log',
filename='G:\project\hospital_log.ldf',
size=5mb,
filegrowth=1MB) 
Go
use hospital
go
if exists(select * from sysobjects where name='doctor')
drop table doctor
go
--医师表
create table doctor 
(
doctorid int not null,              --医师ID
name varchar(32) not null,          --医师姓名
[description] varchar(32) not null, --医师简介
zhicheng varchar(32) not null,      --医师职称
memo varchar(200) null              --备注
)
go
USE hospital
go
if exists(select * from sysobjects where name='user')
drop table [user]
go
--用户表
create table [user]
(
userid int not null,                   --用户ID
name varchar(32) not null,             --用户姓名
loginName  varchar(16) not null,       --登录名称
loginpwd varchar(32) not null,         --登录密码
department varchar(32) not null,       --所在院系
schoolNumber varchar(32) not null,     --学号
Sex char(2) not null,                  --性别
[address] varchar(32) null,            --所在公寓
phone varchar(32) null,                --联系电话
email varchar(32) null,                --电子邮箱
doctorid int not null,                 --主治医师
time smalldatetime not null,           --预约时间
memo varchar(200) null                 --备注
)
go
if exists(select * from sysobjects where name='administrator')
drop table administrator
go
--管理员表
create table administrator
(
administratorid int not null,        --管理员ID
loginName varchar(16) not null,      --登录账号
loginpwd varchar(32) not null        --登录密码
)
go
if exists(select * from sysobjects where name='[time]')
drop table [time]
go
 --时刻表
create table [time]
(
timeid int not null,           --时刻ID
time smalldatetime not null    --预约时间
)
go
if exists(select * from sysobjects where name='department')
drop table department
go
--科室表
create table department 
(
departmentid int not null,            --科室ID
name varchar(32) not null,            -- 科室名称
[description] varchar(32) not null,   --科室简介
doctorid int not null,                --医师ID
memo varchar(200) null                --备注
)
go
/*********************************************************/
/***为医师表创建约束***/
--创建主键约束
use hospital
go
alter table doctor
add constraint pk_doctor primary key(doctorid)
go
/*********************************************************/
/***为用户表创建约束***/
--创建主键约束
use hospital
go
alter table [user]
add constraint pk_user primary key(userid)
go
--创建唯一约束
alter table [user]
add constraint UQ_id unique (userid)
go
alter table [user]
add constraint UQ_loginName1 unique (loginName)
go
--创建用户表字段Sex 设置check约束(检查约束)
alter table [user]
add constraint ck_user
check(Sex between '男' and '女') 
go
--创建用户表外键约束
alter table [user] with check 
add constraint fk_doctorl foreign key(doctorid)
references doctor (doctorid)
go
/*********************************************************/
/***为时刻表创建约束***/
--创建主键约束
use hospital
go
alter table  [time]
add constraint pk_time primary key(time)
go
--创建时刻表外键约束
alter table [user] with check 
add constraint fk_time foreign key(time)
references time (time)
go
/*********************************************************/
/***为管理员表创建约束***/
--创建主键约束
use hospital
go
alter table administrator
add constraint pk_administrator primary key(administratorid)
go
--创建唯一约束
alter table administrator
add constraint UQ_loginName unique (loginName)
go
/*********************************************************/
/***为科室表创建约束***/
--创建主键约束
use hospital
go
alter table department
add constraint pk_department primary key(departmentid)
go
--创建科室表外键约束
alter table department with check 
add constraint fk_doctor foreign key(doctorid)
references doctor (doctorid)
go

你可能感兴趣的:(创建SQLQuery医院挂号数据库、数据表)