zoukankan      html  css  js  c++  java
  • mysql约束

    约束

    什么是约束 constraint

    ​ 约束就是一种限制,例如宪法,让你不能做某些事情

    ​ 数据库的约束,是对数据的安全性,完整性的保证;

    mysql中的约束

    1、unique

    唯一性约束,表示这个字段不能出现重复的值, 用于唯一标识一条记录

    例如身份证号码,学号等

    2、not null

    非空约束,表示这个字段的值不能为空

    例如,账户名,密码等,

    3、null

    一些数据类型默认就是可以为空的

    4、default

    默认值,用于给某一个字段设置默认值

    普通约束测试:
    #完整的建表语句
    create table table_name(字段名称 字段类型[(宽度) 约束]) charset utf8;
    
    # 学生表 具备  姓名 性别 学号
    create table student(
        # 非空
    	name char(20) not null,
        # 默认值
        gender enum("g","b") default "b",
        # 唯一
        id int  unique
    )
    #测试:
    insert into student values(null,null,null);   # 错误原因是 name 不能为空 
    insert into student values("jack",null,null);  # 可以插入  null也是一个特殊的值  并且id的唯一约束 也可以为null
    insert into student(name,id) values("jack",null); #可以插入 当没有给gender指定参数时,将使用默认值 
    alter table student modify id int unique not null; # 为已经存在的字段添加约束
    
    

    5、primary key

    主键约束,从约束角度来看就等同于,非空+唯一

    主键与普通的约束的区别

    create table person(
        id char(19) primary key,
    	name char(20)
    );
    
    insert into person values("1","rose");# ok 
    insert into person values("1","jack");# ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'主键冲突;
    insert into person values("2","jack"); # ok
    insert into person values(null,"tom"); # Column 'id' cannot be null #主键不能为空  
    
    
    #从约束角度来看就等同于,非空+唯一  
    create table person2(
        id char(19),
    	name char(20)
    );
    
    # 在innodb存储引擎中 ,主键用于组织数据 (树形结构)  
    # 也就说主键对于innodb引擎来说是必须要的,没有不行!
    # 如果没有手动指定主键  
    # mysql 会自动查找一个具备非空且唯一的字段作为主键 
    # 如果也没有也没有这样的字段,mysql会创建一个隐藏字段 作为主键
    # 首先明确主键是一种索引  unique也是,索引的作用是加速查询  
    # 如果我们在查询语句中没有使用索引字段 ,mysql将无法为我们的查询加速 意味着如果没有主键,将无法加速查询
    
    总的来说:主键 具备约束的作用,还能加快我们的查询速度,所以今后在创建表的时候都应该创建索引字段
    应该将什么样的字段设置为主键????
    如果本来的业务中就存在非空且唯一的字段 那就把它设为主键   如果没有就自己添加一个字段专门作为主键  
    通常我们会将主键设置为类int 类型,是为了方便保证其唯一;
    
    案例:
    mysql> create table PC(
        id int primary key, 
        pp char(20),
        model char(10),
        price float
    );
    
    mysql> insert into PC values(1,"IBM","1214SB",40000);
    mysql> insert into PC values(2,"DELL","1200DSB",4000);
    mysql> select *from PC;
    mysql> select *from PC where id = 1;
    mysql> select *from PC where pp = "DELL";
    
    为主键设置自动增长

    当我们创建了主键字段时,插入数据必须保证主键是唯一的不能重复,这就需要我们自己管理主键值,这是很麻烦的,所以mysql有一个自动增长的属性,可以添加在整形字段上,每次插入数据时,都可以自动的插入值,并且每次加1不会冲突;

    create table teacher(
        id int primary key auto_increment,
        name char(10)
    );
    insert into teacher values(null,"jack"); # 对于自动增长的字段可以给null  也会自动生成值
    insert into teacher(name) values("jack");# 也可以跳过这个字段 
    
    
    
    create table teacher3(
        id char unique auto_increment ,
        name char(10)
    );
    

    自动增长可以用在 具备索引,并且是数字类型的字段上,但是通常与主键一起使用!

  • 相关阅读:
    张维迎:你必须知道的10个经济学原理
    艾德莱斯绸:“千年时尚”托起新产业
    Sending forms through JavaScript[form提交 form data]
    Sending form data
    Your first HTML form
    form submission
    <input type="file">
    web storm查看文件结构
    jQuery-File-Upload
    IHttpHandler
  • 原文地址:https://www.cnblogs.com/chuwanliu/p/11177225.html
Copyright © 2011-2022 走看看