虚位以待(AD)
虚位以待(AD)
首页 > 数据库 > MsSql数据库 > SQLserver建表建约束

SQLserver建表建约束
类别:MsSql数据库   作者:码皇   来源:<a href="http://blog.csdn.net/xiong9999" target="_blank" rel="nofollow&q   点击:

SQLserver建表建约束:exec xp_cmdshell md E: project ;--先判断数据库是否存在如果存在就删除,if exists(select * from sysdatabases where name= bbsDB )。

SQLserver建表建约束

exec xp_cmdshell 'md E:project'

--先判断数据库是否存在如果存在就删除

if exists(select * from sysdatabases where name='bbsDB')

drop database bbsDB

--创建数据库文件

create database bbsDB

--主数据库文件

on primary

(

name='bbsDB_data',--为主要数据库文件命名

filename='E:projectbbsDB_data.mdf',--主数据库文件的路径

size=10mb--初始大小

)

log on(

--日志文件

name='bbsDB_log',

filename='E:projectbbsDB_log.ldf',

size=3mb,

maxsize=20mb--最大增长量为

)

go

use bbsDB

drop table BBSUsers

create table BBSUsers(

UID int identity(1,1) primary key not null,--标识列自增长

UNumber varchar(32) not null,--用户名,昵称

UPassword varchar(16) not null,--密码不能少于6位数默认为(888888)

UEmail varchar(32) null, --电子邮件必须包含@默认值为(P@P.com)

UBirthday datetime null, --生日

USex bit not null,--性别1代表男

UClass int null, --用户的等级

UStatement varchar (255) null --备注信息

)

go

select UPassword from BBSUsers

alter table BBSUsers-- 为密码添加检查约束长度大于=6的长度

add constraint CK_upassword check(len(UPassword)>=6)

alter table BBSUsers--为密码添加默认约束(888888)

add constraint DK_upassword default ('888888') for UPassword

alter table BBSUsers--为E-mail添加默认约束P@P.com

add constraint DK_uemail default('P@P.com')for UEmail

alter table BBSUsers--Email检查约束@

add constraint CK_uemail check (UEmail like '%@%')

alter table BBSUsers--为性别添加默认约束约束

add constraint DK_usex default('1') for USex

alter table BBSUsers--等级默认约束

add constraint DK_ucalss default('1') for UClass

create table bbsTopic(

TID int identity(1,1) primary key not null,--贴子编号,自动增长

TSID int not null,--版块编号;外键,引用bbsSecton表的主键UID

TUID int not null,--发贴人ID;外键,引用bbsUsers表的主键UID

TReplyCount int null,--回复数量

TFace int null,--发贴表情

TTopic varchar(20) not null,--标题

TContents varchar(30) not null,--正文必须大于6个字符

TTime datetime null,--发贴时间

TClickCount int null,--点击数

Tstate int not null,--状态,例如是否被锁,是否为精华贴

TlastReply datetime null--最后回复时间,必须晚于发贴时间

)

alter table bbsTopic--引用bbsSecton表的主键UID

add constraint FK_tsid foreign key(TSID) references bbsSction(UID)

alter table bbsTopic --引用bbsSecton表的主键UID

add constraint FK_tuid foreign key(TUID) references bbsUsers(UID)

alter table bbsTopic --为默认约束

add constraint DK_treplycount default ('0') for TReplyCount

alter table bbsTopic--为TContents设置检查约束

add constraint CK_tcontents check (len(TContents)>=6)

alter table bbsTopic--为发贴时间设置约束

add constraint DK_time default ('当天') for TTime

alter table bbsTopic--为点击设置默认约束

add constraint DK_tclickcount default('0')for TClickCount

alter table bbsTopic --为状态设置默认约束

add constraint DK_tstate default('1') for Tstate

create table bbsReply(

RID int identity(1,1) primary key not null,--自动编号,贴子编号

RTID int not null,--主贴ID;外键,引用bbsTopic表的主键TID

RSID int not null,--版块ID;外键,引用bbsSection表的主键SID

RUID int not null,--回贴人ID,外键,引用bbsUsers表的主键UID

Rface int null,--回贴表情

Rcontents varchar(30) not null,--正文,必须大于6个字符

Rtime datetime null,--回铁时间

RclickCount int null --点击数

)

alter table bbsReply--为RTID设置外键约束

add constraint FK_rtid foreign key(RTID) references bbsTopic(TID)

alter table bbsReply--为RSID设置外键约束

add constraint FK_rsid foreign key(RSID) references bbsSection(SID)

alter table bbsReply--为RUID设置外键约束

add constraint FK_ruid foreign key(RUID) references bbsUsers(UID)

create table bbsSection(

SID int identity(1,1) primary key not null,--版块编号,自动增长

Sname varchar(32) not null,--版块名称

SmasterID int not null,--版主的用户ID,外键;引用用户表bbsUsers的UID

Sprofile varchar(20) null,--版面简介

SclickCount int null,--点击率

StopicCount int null--发贴数

)

alter table bbsSection--外键约束引用bbsusers的UID

add constraint FK_smasterid foreign key(SmasterID) references bbsUsers(UID)

--作业

建库,如果存在则先删除。请问T-SQL语句如何写?其原因是什么?

答:if exists (select * from sysdatabases where name='数据名')

drop database 数据库名

先删除是为了不和以前的数据库重名

建表,如果存在则先删除。请问T-SQL语句如何写?其原因是什么?

答:if exists(select * from sysobjects where name='表名')

drop table 表名

先删除表是为不覆盖以前的表

char(6)与varchar(6)的区别?

答:cahr 是不可变长、varchar是可变长度

约束的类型有哪些?

SQL server 三层安全模型是指?

答:登陆权限、访问权限、创建表的权限

如何修改sa帐号的密码?

答: exec xp_password 'sa','pass'

相关热词搜索: