zoukankan      html  css  js  c++  java
  • 使用SQL语句创建和删除约束

    原文:http://blog.csdn.net/hamber_bao/article/details/6504905

    约束的目的就是确保表中的数据的完整性。

    常用的约束类型如下:

    主键约束:(Primary Key constraint)      要求主键列唯一,并且不允许为空

    唯一约束:(Unique Constraint)              要求该列唯一,允许为空,但只能出现一个空值

    检查约束:(Check Constraint)                某列取值范围限制、格式限制等。如有关年龄的限制

    默认约束:(Default Constraint)               某列的默认值,如我们的男性学员比较多,性别默认为男

    外键约束:(Foreign Key Constraint)       用于在两表之间建立关系,需要指定引用主表的哪一列

    一、添加约束

    在创建表时,我们可以在字段后添加各种约束,但一般不这样混用,推荐将添加约束和建表的语句分开编写。

    添加约束的语法如下:

    Alter Table 表名   
    Add Constraint  约束名 约束类型 具体的约束类型  

    上述语法标识修改某个表,添加某个约束,其中约束名的命名规则推荐采用"约束类型_约束字段"这样的形式。

    --添加主键约束   
    Alter Table stuInfo   
    Add Constraint  PK_stuNO primary Key(stuNo)   
    ---添加唯一约束   
    Alter Table stuInfo   
    Add Constraint UQ_stuID unique(stuID)   
    ---添加默认约束   
    Alter Table stuInfo   
    Add Constraint DF_stuAddress default('地址不详') for stuAddress   
    ---添加检查约束   
    Alter Table stuInfo   
    Add Constraint CK_stuAge check(stuAge between 15 and 40)   
    ---添加外键约束   
    Alter Table stuMarks   
    Add Constraint FK_stuNo foreign key(stuNo) references stuInfo(stuNo)  

    二、删除约束

    如果错误的添加了约束,则可以删除约束

    删除约束的语法如下:

    Alter Table 表名   
    Drop Constraint  约束名  

    附加:在创建表的时候同时添加约束的写法:

    use stuDB   
    go   
    if exists(select * from Sysobjects where name = 'stuInfo')   
    drop table stuInfo   
    go   
    create table stuInfo   
    (   
         stuName varchar(20) not null primary key(stuName)    
    ,stuID int not null unique(stuID)   
    ,stuAddress varchar(20) not null default('地址不详')   
    ,stuAge int not null check(stuAge between 15 and 40)   
    )  

     (非常感谢楼主!)

  • 相关阅读:
    Linux 添加环境变量
    postgresql 获取修改列的值
    5月30日周一上午
    周日5月29日
    2016年5月26日
    如何使用Gson(添加到项目里去)
    linux内核分析课程总结()待完善
    5月5日离散课笔记
    4月28日的离散课(还少了一部分)
    2016年4月29日
  • 原文地址:https://www.cnblogs.com/ghw0501/p/4934628.html
Copyright © 2011-2022 走看看