ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

约束概述(重要)

2022-01-23 15:31:45  阅读:173  来源: 互联网

标签:重要 name 约束 email vip 概述 unique id


7、约束(重要)

  7.1、什么是约束?

    约束对应的英语单词:constraint

    在创建表的时候,我们可以给表中的字段加上一些约束,保证表中数据完整有效。

  7.2、约束包括哪些?

    非空约束:not null

    唯一性约束:unique

    主键约束:primary key (简称PK)

    外键约束:foreign key (奖惩FK)

    检查约束:check(mysql不支持,oracle支持)

    重点学习四个约束:

      not null

      unique

      primary key

      foreign key

      约束添加在列的后面,叫列级约束

      约束没添加在列的后面,叫表级约束

  7.3、非空约束:not null

    非空约束not null约束的字段不能为NULL

    drop table if exists t_vip;

    create table t_vip(

      id int,

      name varchar(255) not null

    );

    insert into t_vip(id,name) values(1,'zhangsan');

    insert into t_vip(id,name) values(2,'lisi');

 

    insert into t_vip(id) values(3);//报错,name有非空约束

    

     附:xxxx.sql文件被称为sql脚本文件

      sql脚本文件编写大量的sql语句

      执行sql脚本文件,该文件中所有的sql语句会全部执行

      批量的执行sql语句,可以使用sql脚本文件

      在mysql但你高中怎么执行sql脚本?
      source  D:\............

      实际工作中项目经理给了个xxx.sql文件,执行这个脚本文件,电脑上数据库数据就有了,

      数据量大直接用记事本打开sql脚本文件可能会死机,所以用source

  

   7.4、唯一性约束:unique

      唯一性约束unique约束的字段不能重复,但是可以为NULL

      drop table if exists t_vip

      create table t_vip(

        id int,

        name varchar(255) unique,//约束添加在列的后面,叫列级约束

        email varchar(255)

      );

      insert into t_vip(id,name,email) values(1,'zhangsan','zhangsan@123.com');

      insert into t_vip(id,name,email) values(2,'lisi','lisi@123.com');

      insert into t_vip(id,name,email) values(3,'wangwu','wangwu@123.com');

      select * from t_vip;

 

      insert into t_vip(id,name,email) values(1,'zhangsan','zhangsan@123.com');//name有唯一性约束,报错

      name字段虽然被unique约束,但都可以为NULL

    新需求:name和email字段联合起来具有唯一性!!

      

      drop table if exists t_vip

      create table t_vip(

        id int,

        name varchar(255) unique,

        email varchar(255)

      );

      这张表这样创建是不符合我以上“新需求”的

      这样创建表示:name具有唯一性,email具有唯一性。各自唯一。

      insert into t_vip(id,name,email) values(1,'zhangsan','zhangsan@123.com');

      insert into t_vip(id,name,email) values(2,'zhangsan','zhangsan@sina.com');

 

    联合唯一方法:

      

      drop table if exists t_vip

      create table t_vip(

        id int,

        name varchar(255) ,

        email varchar(255)

        unique(name,email)     //约束没有添加在列的后面,叫表级约束

      );

 

        需要多个字段联合起来约束的时候用表级约束

      注意:not null 只有列级约束,没有表级约束,unique有表级约束

 

   7.5、unique和not null联合

      

     drop table if exists t_vip

      create table t_vip(

        id int,

        name varchar(255) not null unique

      ):

    

 

       在mysql中,如果一个字段同时被not null和unique约束的话,该字段自动变为主键字段。

    注意:oracle不一样

标签:重要,name,约束,email,vip,概述,unique,id
来源: https://www.cnblogs.com/doremi429/p/15836488.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有