30 约束(Constraint)

摘要:
30约束什么是约束?非空约束非空--------------------------droptableifexist_用户;createtablet_user;//ERROR1364:字段“用户名”没有默认值插入到t_用户值中;插入t_用户值;唯一性约束*唯一性约束修改的字段是唯一的,不能重复,但可以为空*案例:droptableifexistst_user;createtablet_user;//1062-重复输入“zhangsan”工厂“用户名”插入_用户值,;插入用户值,,;*案例:将uniquedroptableifexist添加到两个或多个列_user;createtablet_user;插入t_用户值;插入t_用户值;插入t_用户值;选择*fromt_user;//1062重复“11-zs”标记“用户代码”插入t_用户值;droptableifexist_user;createtablet_user;//1062重复“11'”的“用户代码”插入t_用户值,;*注意:非空约束只有列级约束,没有表级约束。
30 约束(Constraint)
 
    什么是约束?常见的约束有哪些呢?
        在创建表的时候,可以给表的字段添加相应的约束,添加约束的目的是为了保证表中数据的合法性、有效性、完整性。
        表格
        id            username(唯一性约束)            password(非空约束)
        
        常见的约束有哪些呢?
            非空约束(not null):约束的字段不能为null
            唯一约束(unique):约束数据的字段不能重复
            主键约束(primary key):约束的字段既不能为null,也不能重复(简称PK)
            外键约束(foreign key):...(简称FK)
            检查约束(check):注意Orcale数据库有check约束,但是mysql没有,目前mysql不支持该约束。
            
        非空约束 not null
        
        ----------------------------------------
        drop table if exists t_user;
        create table t_user(
            id int,
            username varchar(255) not null,
            password varchar(255)
        );
        
        // ERROR 1364 (HY000): Field 'username' doesn't have a default value
        insert into t_user(id,password) values(1,'123');
        
        insert into t_user(id,username,password) values(1,'lisi','123');
        
    唯一性约束(unique)
        
        * 唯一约束修饰的字段具有唯一性,不能重复,但可以为null;
        * 案例:
            drop table if exists t_user;
            create table t_user(
                id int,
                username varchar(255) unique // 列级约束
            );
            
            // 1062 - Duplicate entry 'zhangsan' for key 'username'
            insert into t_user (id,username) values(1,'zhangsan'),(2,'zhangsan');
            
            insert into t_user(id) values(1),(2),(3);
            
        * 案例,给两个列或者多个列添加unique
            drop table if exists t_user;
            create table t_user(
                id int,
                usercode varchar(255),
                username varchar(255),
                unique(usercode,username) // 多个字段联合添加1个约束 unique(表级约束)
            );
            
            insert into t_user values(1,'111','zs');
            insert into t_user values(2,'111','ls');
            insert into t_user values(3,'222','zs');
            select * from t_user;
            
            //1062 - Duplicate entry '111-zs' for key 'usercode'
            insert into t_user values(4,'111','zs');
            
            
            drop table if exists t_user;
            create table t_user(
                id int,
                usercode varchar(255) unique,
                username varchar(255) unique
            );
            
            // 1062 - Duplicate entry '111' for key 'usercode'
            insert into t_user values(1,'111','zs'),(2,'111','ls');
            
        * 注意:not null约束只有列级约束,没有表级约束。
        
    主键约束:
        
        * 怎么给一张表添加主键约束?
            drop table if exists t_user;
            create table t_user(
                id int primary key,// 列级约束
                username varchar(255),
                email varchar(255)
            );
            
            insert into t_user values(1,'zs','zs@qq.com'),(2,'ls','ls@qq.com'),(3,'ww','ww@qq.com');
            
            select * from t_user;
            +----+----------+-----------+
            | id | username | email     |
            +----+----------+-----------+
            |  1 | zs       | zs@qq.com |
            |  2 | ls       | ls@qq.com |
            |  3 | ww       | ww@qq.com |
            +----+----------+-----------+
            
            // 1062 - Duplicate entry '1' for key 'PRIMARY'
            insert into t_user(id,username,email) values(1,'ll','ll@qq.com');
            
            // 1364 - Field 'id' doesn't have a default value
            insert into t_user(username,email) values('ll','ll@qq.com');
            
            根据以上的测试得出:id是主键,因为添加了主键约束,主键字段中的数据不能为null,也不能重复。
            主键的特点:不能为null,也不能重复。
            
        * 主键相关的术语?
            主键约束:primary key
            主键字段:id字段添加了primary key之后,id叫做主键字段
            主键值:id字段中的每一个值都是主键值。
            
        * 主键有什么作用?
            
            - 表的设计三范式中有要求:第一范式就要求任何一张表都应该有主键。
            - 主键的作用:主键值是这行记录在这张表当中的唯一标识。(就像一个人的身份证号码一样。)
            
        * 主键的分类:
            根据主键字段的字段数量来划分:
                单一主键(推荐的,常用你的)
                复合主键(多个字段联合起来添加一个主键约束)(复核主键不建议使用,因为复合主键违背三范式。)
                    
            根据主键性质来划分:
                自然主键:主键值最好是一个和业务没有任何关系的自然数。(这种方式是推荐的。)
                业务主键:主键值和系统的业务挂钩,例如:拿着银行卡的卡号做主键,拿着身份证号码作为主键。(不推荐用)
                最好不要拿着和业务挂钩的字段作为主键。因为以后的业务一旦发生改变的时候,主键值可能也需要随着发生
                变化,但有的时候没有办法变化,因为变化可能会导致主键重复。
                
        * 一张表的主键约束只能有1个。(必须记住)
        
        * 使用表级约束方式定义主键:
            drop table if exists t_user;
            create table t_user(
                id int,
                username varchar(255),
                primary key(id)
            );
        
            insert into t_user(id,username) values(1,'zs');
            insert into t_user(id,username) values(2,'ls');
            insert into t_user(id,username) values(3,'ww');
            insert into t_user(id,username) values(4,'ll');
            
            select * from t_user;
            +----+----------+
            | id | username |
            +----+----------+
            |  1 | zs       |
            |  2 | ls       |
            |  3 | ww       |
            |  4 | ll       |
            +----+----------+
            
            // 1062 - Duplicate entry '4' for key 'PRIMARY'
            insert into t_user(id,username) values(4,'faker');
            
            以下内容是演示一下复合主键,不需要掌握:
                drop table if exists t_user;
                create table t_user(
                    id int,
                    username varchar(255),
                    password varchar(255),
                    primary key(id,username)
                );
                
        
        * mysql提供的主键值自增:(非常重要)
            drop table if exists t_user;
            create table t_user(
                id int primary key auto_increment, // id字段自动维护一个自增的数字,从1开始,以1递增。
                username varchar(255)
            );
            
            insert into t_user(username) values('a');
            insert into t_user(username) values('b');
            insert into t_user(username) values('c');
            insert into t_user(username) values('d');
        
            select * from t_user;
            +----+----------+
            | id | username |
            +----+----------+
            |  1 | a        |
            |  2 | b        |
            |  3 | c        |
            |  4 | d        |
            +----+----------+
        
            提示:orcale当中也提供了一个自增机制,叫做:序列(sequence)对象。

免责声明:文章转载自《30 约束(Constraint)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇idea Rebug调试工具Shell编程基础教程1--Shell简介下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

mysql表关联问题(第二卷:外键1对多之2)

接着上一卷内容我们继续: 上卷我用的查询语句我们可以看到全部数据,很明显这样的方式查找的数据并不详细: SELECT * FROM usr LEFT JOIN fzu ON usr.fzu = fzu.id WHERE fzu.fzu = 'A';我在之前语句的后面拼接了个where语句 看下结果: 多么完美的查询 现在悲催的是你的BOSS又加需求了,加...

linux环境安装mysql,以及mysql基本的终端操作命令

linux环境下安装mysql服务器、客户端mysql简单的终端操作指令(使用数据库、简单的增删改查和备份恢复)1 SQL: Structured Query Language 结构化查询语言。 运用sql语句,可以对关系型数据库进行操作。 目前常见的关系型数据库有: MySQL oracle...

MYSQL的常用命令和增删改查语句和数据类型

连接命令:mysql -h[主机地址] -u[用户名] -p[用户密码]   创建数据库:create database [库名]   显示所有数据库: show databases;   打开数据库:use [库名]   当前选择的库状态:SELECT DATABASE();   创建数据表:CREATE TABLE [表名]([字段名] [字段类型]...

MySQL数据库增删改查SQL语句(2018整理集合大全)

查看数据库 show databases;  使用数据库 use 数据库名; 创建数据库 CREATE DATABASE 数据库名; 删除数据库 DROP DATABASE 数据库名; 创建表 create table 表名(     列名1 类型(长度) [约束],     列名2 类型(长度) [约束],     …… ); 长度区别 int类型带长度...

我的MYSQL学习心得(一) 简单语法

我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运算符 我的MYSQL学习心得(六) 函数 我的MYSQL学习心得(七) 查询 我的MYSQL学习心得(八) 插入 更新 删除 我的MYSQL学习心得(九) 索引 我的MYSQL学习心得(十) 自定义...

MySQL数据库增删改字段(属性)

MySQL数据库的各种操作今天在这里总结一下: 一、增加 1.在已有的表中添加新的字段: 首先是增加表的字段,比如一张表原本没有字段“ Time ”,现在我们要增加这样一个字段,可以用下面的SQL语句实现: alter table +table的名字+ add + 字段名字 + 数据类型+分号,以下SQL在表testTable中插入了2个字段,font和a...