1 use StudentManageDB 2 go 3 --创建主键约束 4 5 if exists(select * from sysobjects where name=‘pk_StudentID‘) 6 alter table Students drop constraint pk_StudentID 7 alter table Students add constraint pk_StudentID primary key(StudentID) 8 9 --创建唯一约束10 if exists(select * from sysobjects where name=‘uq_StudentIDNo‘)11 alter table Students drop constraint uq_StudentIDNo12 alter table Students add constraint uq_StudentIDNo unique(StudentIDNo)13 14 --创建检查约束15 if exists(select * from sysobjects where name=‘ck_Age‘)16 alter table Students drop constraint ck_Age17 alter table Students add constraint ck_Age check(Age between 18 and 25)18 19 if exists(select * from sysobjects where name=‘ck_PhoneNumber‘)20 alter table Students drop constraint ck_PhoneNumber21 alter table Students add constraint ck_PhoneNumber check(len(PhoneNumber)=11)22 23 24 update Students set PhoneNumber=‘13099012876‘ where StudentID=1000025 select * from Students26 27 --创建默认值28 if exists(select * from sysobjects where name=‘df_StudentAddress‘)29 alter table Students drop constraint df_StudentAddress30 alter table Students add constraint df_StudentAddress default(‘地址不详‘) for StudentAddress31 32 insert into Students (Studentname,Gender,Birthday,Age,StudentIDNo,PhoneNumber,33 StudentAddress,ClassID)34 values(‘李小璐‘,‘女‘,‘1989-01-12‘,24,120229198901121315,‘13099012876‘,default,1)35 36 insert into Students (Studentname,37 ClassID)38 values(‘李小璐‘,120229198901121316,1)39 40 --外键约束41 insert into StudentClass (ClassID,Classname) values(1,‘软件1班‘)42 if exists(select * from sysobjects where name=‘fk_ClassID‘)43 alter table Students drop constraint fk_ClassID44 alter table Students add constraint fk_ClassID foreign key (ClassID) references StudentClass(ClassID)45 46 select * from studentClass总结
以上是内存溢出为你收集整理的sqlserver数据库设计完整性与约束全部内容,希望文章能够帮你解决sqlserver数据库设计完整性与约束所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)