• MySQL约束条件


    约束条件

    '''
        default默认值
            补充知识点:插入数据的时候可以指定字段
                create table t1(
                    id int,
                    name char(16)
                );
                insert into t1(name,id) values('jason',1);
            create table t2(
                id int,
                name char(16) not null,
                gender enum('male','female','others') default 'male'
            );
            insert into t2(id,name) values(1,'egon');
            insert into t2 values(2,'jason','female');
        unique唯一
            单列唯一
                create table t3(
                    id int unique,
                    name char(16)
                );
                insert into t3 values(1,'jason'),(1,'egon');
                insert into t3 values(1,'jason'),(2,'egon');
            联合唯一
                ip和port,单个都可以重复,但是加在一起必须是唯一的
                create table t4(
                    id int,
                    ip char(16),
                    port int,
                    unique(ip,port)
                );
                insert into t4 values(1,'127.0.0.1',8080);
                insert into t4 values(2,'127.0.0.1',8081);
                insert into t4 values(3,'127.0.0.2',8080);
                insert into t4 values(4,'127.0.0.1',8080); 报错
        primary key主键
            单单从约束效果上来看,prinary key 等价于not null+unique,非空且唯一!!!
                create table t5(id int primary key);
                insert into t5 values(null);报错
                insert into t5 values(1),(1);报错
            它除了有约束效果之外,它还是innodb存储引擎组织数据的依据
                innodb存储引擎在创建表的时候必须要有primary key
                因为它类似于书的目录,能够帮助提升查询效率并且也是建表的依旧
                1:一张表中有且只有一个主键,如果你没有设置主键,那么会从上往下搜索直到遇到一个非空且唯一的字段,将它自动升级为主键
                    create table t6(
                        id int,
                        name cahr(16),
                        age int not null unique,
                        addr char(32) not null unique
                    );
                    
                2:如果表中没有主键也没有其它任何的非空且唯一字段,那么innodb会采用自己内部提供的一个隐藏字段作为主键,隐藏意味着你无法使用它,就无法提升查询效率
                3:一张表中通常都应该有一个主键字段,并且通常将id字段作为主键
                    单个字段主键
                        create table t7(
                            id int primary key,
                            name char(16)
                        );
                    联合主键(多个字段联合起来作为表的主键,本质还是一个主键),用的不多
                        create table t8(
                            ip char(16),
                            port int,
                            primary key(ip,port)
                        );
            总结:也就意味着,以后我们在创建表的时候id字段一定要加primary key
            扩展:auto_increment自增(注意通常都是加在key(主)键上,不能给普通字段加)
                当编号特别多的时候,人为的去维护太繁琐
                create table t9(
                    id int primary key auto_increment,
                    name char(16)
                );
                insert into t9(name) values('jason'),('egon'),('mike');
            结论:以后再创建表的id(数据的唯一标识id/uid/sid)字段的时候,id int primary key auto_increment
            补充:delete from 在删除表中数据的时候,主键的自增不会停止
                  truncate 表名 清空表数据并且重置主键
      约束条件之zerofill(用0填充)
      约束条件之unsigned(无符号)   约束条件之not null(不能插入null)
    '''
    """
    create table t1(
      id int primary key auto_increment,
      name varchar(20) not null unique,
      age int not null unique,
      gender enum('male', 'female', 'others') default 'male' not null,
      hobby set('read', 'study', 'sport') default 'study' not null
    );

    insert into t1(name, age) values('admin', 19);
    insert into t1(name, age) values('jack', 20);
    insert into t1(name, age) values('admin', 21); # 报错
    insert into t1(name, age) values('mike', 21);

    select * from t1; # 此时会发现三条数据id分别为1,2,4;也就是插入的数据为重复的数据时,主键自增也不会停止

    insert into t1 values(3, 'jason', 30, 'male', 'read,study'); # 指定id为3能成功插入数据
    """
    while True: print('studying...')
  • 相关阅读:
    十二、curator recipes之双重屏障DoubleBarrier
    十一、curator recipes之联锁InterProcessMultiLock
    十、curator recipes之信号量InterProcessSemaphoreV2
    九、curator recipes之不可重入锁InterProcessSemaphoreMutex
    八、curator recipes之选举主节点LeaderSelector
    五、curator recipes之选举主节点Leader Latch
    ADO.net 数据库连接new SqlConnection、Open、Close、Dispose
    Java学习笔记【八、数据结构】
    Java学习笔记【七、时间、日期、数字】
    Java学习笔记【六、正则表达式】
  • 原文地址:https://www.cnblogs.com/xuewei95/p/15113244.html
Copyright © 2020-2023  润新知