• MSSQLSERVER数据库 约束


          为了减少数据冗余和使数据库内容变的严谨,MSSQL数据库里引入了关系和约束。我们平时做一些小程序,需要使用到MSSQL数据库的时候大多没有严格去规划一下数据库的设计,但是真正开发的时候需要你严格的进行需求分析后再进行数据库设计,好的数据库设计对于维护和未来对程序的扩展有着非常大的好处。

          所以我觉的学习一下约束和关系还是挺重的。我是现学现卖,将学习的过程记录一下:

    在学习之前先背几个单词,在需要在SQL语句写的时候用到:

    • Alter:改变
    • Primary:主要
    • Constraint:约束
    • Unique:唯一的、独特的
    • Foreign::外国的,外交的
    • Reference:参考,引用,提及
    • Cascade层叠,串联的

    数据库约束是为了保证数据的完整性而实现的一套机制,约束有:

    • 非空约束
    • 主键约束(PK) Primary key constraint
    • 唯一约束(UQ) Unique constraint
    • 默认约束(DF) Default constraint
    • 检查约束(CK) Check constraint
    • 外键约束(FK) Foreign key constraint

    我们首先来创建一张表teacher

     

    create table teacher
    (
    	tId int not null,
    	tname nvarchar(50),
    	tage int,
    	tsalary int
    )
    

     

    如何给表增加字段

    alter table teacher

    add ttest nvarchar(50)

    如图,看到添加了ttest字段

    先看一下主键约束

    alter table teacher

    add constraint PK_Teacher primary key(tId)

     

    对比一下这两张图,可以看到图2的键里多了个PK_Teacher,并且列的tId变成了一个钥匙,说明创建成功

        

                (图2)                                                    (图1)

     

    维一约束:

    alter table teacher

    add constraint  UQ_Teacher_tname unique(tname)

    那么唯一约束有什么不同。我们可以往数据库插入两条数据

    insert into teacher(tId,tname,tage,tsalary) values('1','Jack',18,'3000');

    insert into teacher(tId,tname,tage,tsalary) values('2','Jack',21,'4000');

     

    发现在执行第二条数据的时候报错:

    消息2627,级别14,状态1,第

    违反了UNIQUE KEY 约束'UQ_Teacher_tname'。不能在对象'dbo.teacher' 中插入重复键。

     

    通过这样,从而保证了数据的正确性

    默认约束

    alter table teacher

    add constraint DF_Teacher_age default(18) for tage

    插入一条数据:insert into teacher(tId,tname,tsalary) values('2','Bill',99999);

    然后进行查表:select * from teacher

    发现在没有插入数据tage的情况下,最后的名为billage数据默认是18

     

    tId tname tage tsalary

    1 Jack 18 3000

    2 Bill 18 99999

     

     

    检查约束

    --为了方便测试,进行检查约束前先对表添加一个sex字段
    alter table teacher
    add tsex nvarchar(2)
    
    --接着将表里的tsex字段数据全部改成男
    
    update teacher set tsex='男'
    
    --进行检查约束
    
    alter table teacher
    
    add constraint CK_Teacher_Tsex check (tsex='男' or tsex='女');
    
    --测试检查约束
    
    insert into teacher(tId,tname,tage,tsalary,tsex) values('3','CJ','32','10000','猫');
    

      

     

    发现错误,因为猫不是正常规定的人的性别该有的男和女。

    消息547,级别16,状态0,第

    INSERT 语句与CHECK 约束"CK_Teacher_Tsex"冲突。该冲突发生于数据库"MySchool",表"dbo.teacher", column 'tsex'

    语句已终止。

     

     

    那里在工具里怎么设置呢右键字段tsex选择CHECK约束

    在管理器的表设计里,右键Ttest选关系,如图3所示

    也可以对年龄的范围进行约束:

    alter table teacher

    add constraint CK_Teacher_Tage check(tage>=18 and tage<100);

    外键约束

    --为了进行外键约束的测试,先建一个表
    
    create table student
    
    (
    
    sid int not null,
    
    sname nvarchar(50),
    
    sage int,
    
    ssex nvarchar(2)
    
    )
    
    --往student表里追加一个字段用作外键约束
    
    alter table student
    
    add tid int not null
    
    --往student表里插入一条数据
    
    insert into student(sid,sname,sage,ssex,tid) values('1','春晓','23','男',1);
    
    --将teacher表和student表进行外键约束
    
    --由于是给student表的tid字段增加的外键约束,所以他所约束的另一张表的约束字段必需是主键
    
    alter table student
    
    add constraint FK_student_tId foreign key(tid) references teacher(tid)
    
    --对student表的tid字段创建好外键后如果进行插入数据是teacher表里的tid字段里没有的值,那么会报错。
    
    --例如下面的是错误的
    
    insert into insert into student(sid,sname,sage,ssex,tid) values('1','春晓','23','男',100);
    
    --如果要删除student表外键约束的主表teacher里的数据,会报错
    
    delete teacher where tId=1;
    
     
    
    --如何想要删除teacher表里的数据?要那么先要删除student表里tid=1的数据,才可以删除
    
    --我们也可以在创建外键约束的时候,进行删除的级联设置
    
    --如下SQL语句
    
    alter table student
    
    add constraint FK_student_tId foreign key(tid) references teacher(tid)
    
    on delete cascade
    
    --当然也可以进行级联修改:
    
    alter table student
    
    add constraint FK_student_tId foreign key(tid) references teacher(tid)
    
    on update cascade
    

      

    那么如何在管理器里进行外键的设置 如图 右键tid选关系。在表的规范里进行设置主表和外表的关系,InsertUpdate规范里进行设置更新和删除的级联设置。

    综合练习

    --下面来做一个踪合的约束设置
    --创建一个Employee表
    --对tId 设置为主键
    --对tName 控制是唯一
    --对tSex 控制只能是男女
    --对tAge 控制只在-60之间默认值是
    
    create table Employee
    (
    	tId int not null,
    	tName nvarchar(50),
    	tSex nvarchar(2),
    	tAge int
    )
    
    alter table Employee
    add constraint PK_Employee_tId primary key(tId),
    constraint UQ_Employee_tName unique(tName),
    constraint CK_Employee_tSex check(tSex='男' or tSex='女'),
    constraint CK_Employee_tAge check(tAge>=18 and tAge<=100),
    constraint DF_Employee_tAge default(18) for tAge
    
    --现在我们将原来的Employee表删除,我们在创建表的时候进行约束设置
    --对tId 设置为主键
    --对tName 控制是唯一
    --对tSex 控制只能是男女
    --对tAge 控制只在-60之间默认值是
    drop table Employee
    create table Employee
    (
    	tId int not null primary key,
    	tName nvarchar(50) unique,
    	tSex nvarchar(2) check(tSex='男' or tSex='女'),
    	tAge int check(tAge>=19 and tAge<=60) default(18)
    )
    
    --如何想在创建表的时候对约束起名字,可以写成这种形式
    tSex nvarchar(2) constraint CK_Employee_tSex check(tSex='男' or tSex='女'),
    

      

    最后我们那约束进行删除 该怎么办如下SQL语句:

    alter table Employee

    drop constraint CK_Employee_tSex

     

    约束的了解就到这里!

     

  • 相关阅读:
    操作Redis--hash/key-value
    Coverage数据构成
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/cxeye/p/2623129.html
Copyright © 2020-2023  润新知