sqlserver数据库设计完整性与约束
2024-08-25 13:44:15
use StudentManageDB
go
--创建主键约束 if exists(select * from sysobjects where name='pk_StudentId')
alter table Students drop constraint pk_StudentId
alter table Students add constraint pk_StudentId primary key(StudentId) --创建唯一约束
if exists(select * from sysobjects where name='uq_StudentIdNo')
alter table Students drop constraint uq_StudentIdNo
alter table Students add constraint uq_StudentIdNo unique(StudentIdNo) --创建检查约束
if exists(select * from sysobjects where name='ck_Age')
alter table Students drop constraint ck_Age
alter table Students add constraint ck_Age check(Age between 18 and 25) if exists(select * from sysobjects where name='ck_PhoneNumber')
alter table Students drop constraint ck_PhoneNumber
alter table Students add constraint ck_PhoneNumber check(len(PhoneNumber)=11) update Students set PhoneNumber='' where StudentId=10000
select * from Students --创建默认值
if exists(select * from sysobjects where name='df_StudentAddress')
alter table Students drop constraint df_StudentAddress
alter table Students add constraint df_StudentAddress default('地址不详') for StudentAddress insert into Students (StudentName,Gender,Birthday,Age,StudentIdNo ,PhoneNumber,
StudentAddress,ClassId)
values('李小璐','女','1989-01-12',24,120229198901121315, '',default,1) insert into Students (StudentName,Gender,Birthday,Age,StudentIdNo ,PhoneNumber,
ClassId)
values('李小璐','女','1989-01-12',24,120229198901121316, '',1) --外键约束
insert into StudentClass (ClassId,ClassName) values(1,'软件1班')
if exists(select * from sysobjects where name='fk_ClassId')
alter table Students drop constraint fk_ClassId
alter table Students add constraint fk_ClassId foreign key (ClassId) references StudentClass(ClassId) select * from studentClass
最新文章
- Unity3D NGUI刮刮卡效果
- Cocos学习-----Cocos2Dx安装
- Linux下SVN安装与基本操作
- osharp3 整合 dbcontextscope 文章2 将dbcontext的创建收回到ioc管理
- Jersey框架一:Jersey RESTful WebService框架简介
- 快速解决Canvas.toDataURL 图片跨域的问题
- IOS 7 Study - UIActivityViewController(Presenting Sharing Options)
- avalon学习笔记一 列表及条件过滤
- Java中的流程控制(二)
- Hibernate基础学习(一)—初识Hibernate
- Simpson积分(BZOJ2178)
- 《Java技术》的第二次作业
- 把一个机器上1天内新增的文件用rsync传送到另外一台机器
- MySQL delete语句的问题
- Win7+ 电脑 休眠 自动唤醒
- mysql常用运算符
- 【待完成】[MapReduce_9] MapReduce 的 Shuffle 机制
- Linux文件的时间
- Android: TextView 及其子类通过代码和 XML 设置字体大小的存在差异的分析
- MySQL只有.frm文件恢复表结构
热门文章
- python——psutil的使用(获取进程信息)
- 用IBM MQ中间件开发碰到的MQRC_NOT_AUTHORIZED(2035)问题
- numpy 笔记
- ~ # himm -sh: himm: not found的解决方法
- css 如何实现图片等比例缩放
- :nth-of-type(n) 与 :nth-child(n) 区别
- netBeans 修改新建php文件头部注释模板
- DevOps需要的工具
- php解析url并得到url中的参数及获取url参数
- Delphi常用API,API函数