zoukankan      html  css  js  c++  java
  • MySQL中的六个约束

    主键约束 primary key:一个表中只能有一个主键,主键值不能为空,不能重复

                                       设置主键约束:1 在创建表时就设置好主键约束

                                                                   create table person(id int primary key);

                                                             或  create table person(id int ,name char(10),primary key(id));

                                                                2 通过修改表中列的属性来设置主键约束

                                                                  alter table person add primary key(id);

                                                            或  alter table person modify id int primary key;

                                       设置联合主键:1:在创建时就设置为联合主键约束

                                                                       create table person(id int,name char(10),primary key(id,name));

                                                                2:修改列的属性设置联合主键约束 

                                                                       alter table person add primary key(id,name);

                                       不同记录的联合主键的列值不能同时相同,可以分别相同,不能同时为空

                                       删除主键约束:alter table person drop primary key;

    递增约束 auto_increment:设置为自动递增的列,自己没给他添加值,系统会自动给他赋值,一般把主键设为递增约束

                                                create table student(id int primary key auto_increment);

    唯一约束 unique:设置为唯一约束的列值不能重复

                               

                                     设置唯一约束:  1 在创建表时就设置好唯一约束

                                                                   create table person(id int unique);

                                                             或  create table person(id int ,name char(10),unique(id));

                                                                2 通过修改表中列的属性来设置唯一约束

                                                                  alter table person add unique(id);

                                                            或  alter table person modify id int unique;

                                       设置联合唯一约束:1:在创建时就设置为联合唯一约束

                                                                       create table person(id int,name char(10),primary key(id,name));

                                                                       2:修改列的属性设置联合唯一约束 

                                                                       alter table person add unique(id,name);

                                       不同记录的联合唯一约束的列值不能同时相同,可以分别相同,不能同时为空

                                       删除主键约束:alter table person drop unique;

                              

    默认约束 default:设置某个列在没有记录时使用默认值

    create table person (id int,name char(10) default 'zhangsan');

    非空约束 not null:非空约束的列值不能为空

     create table person(id int,name char(10) not null);

    外键约束 foreige key   references:副表被约束的列值从主表约束列值中选择,主表约束的列值被副表列值使用时,这个主表的列值不能被删除。主表的列必须是主键;

     create table person(id int primary key);

     create table student(id int,foreign key(id) references person(id));

  • 相关阅读:
    Caused by: Unable to load bean: type:com.opensymphony.xwork2.ObjectFactory class:org.apache.struts2.
    走到头儿的外企之路——北漂18年(45)
    [ERROR] XML Parser Error on line 11: 注释中不允许出现字符串 "--"
    Unknown lifecycle phase "mybatis-generator-generate"
    org.eclipse.wst.xsl.jaxp.debug.invoker.TransformationException
    windows expect-5.21r1b1-setup.exe 下载链接
    linux下C语言实现静态IP地址,网关的设置
    OpenSSL “心脏滴血”漏洞
    linux 让root用户可以telnet
    perl 创建文本框
  • 原文地址:https://www.cnblogs.com/zhangyang4674/p/11600362.html
Copyright © 2011-2022 走看看