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


    列属性(约束)
    1: 是否允许为空(not null)  --not null不允许为空
    create table t_1(
        a tinyint(3) zerofill not null,
        b tinyint(3)
    );
    例: insert into t_1 values(18,29);
    例: insert into t_1 values(null,12);  --报错
    例: desc t_1;
    例: alter table t_1 modify a tinyint(3) not null zerofill;  --报错zerofill不能数据类型分开
    例: insert into t_1(a) values(19);
    例: insert into t_1(b) values(28);

    *******************************************************************************************************************

    2: 默认值属性(default)
        create table t_2(
            a tinyint(2) zerofill not null default 18,
            b tinyint(2)
        );
        例: insert into t_2(b) values(19);
    *******************************************************************************************************************

    3: 列注释(comment)
        create table t_3(
            id tinyint(2) zerofill not null default 18 comment '编号',
            name varchar(30) comment '姓名'
        );
        use information_schema;
        show tables;
        desc information_schema.columns;
        select table_schema, table_name, column_name, column_comment from information_schema.columns;
        select table_schema, table_name, column_name, column_comment from information_schema.columns where table_schema = 'test' and table_name = 't_3';
    *******************************************************************************************************************

    4: 唯一约束  --不允许有重复的值
        drop table if exists t_4;
        create table t_4(
            id int,
            age tinyint,
            constraint un_id unique(id)    --创建一个唯一约束给id字段, constraint un_id给这个唯一约束取名为un_id;
        );
        desc t_4;
        show create table t_4G

        例: insert into t_4 values(1, 2);
        例: insert into t_4 values(1, 2);        --报错, id值要是唯一的
        例: alter table t_4 drop index un_id;    --删除唯一约束 un_id(约束名);
        例: alter table t_4 add unique(id);      --增加唯一约束,
        ps: 约束名可以不用写, 如果不写的话则会默认的创建一个唯一约束名字,可以通过show create table t_4G 来查看约束名;
            例: alter table t_4 drop index id;

        例: insert into t_4 values(null, null);
        例: insert into t_4 values(null, null);  --唯一约束允许null值的重复
    *******************************************************************************************************************

    5: 主键约束(primary key)
        create table t_6(
            t_no int(1) primary key,
            t_name varchar(30),
            t_sex varchar(3)
        );
        例: desc t_6;
        例: insert into t_6 values(null, '胖胖', '男');   --报错 主键值不能为null, 在创建主键时也会默认把字段设置not null
        例: insert into t_6 values(1, '胖胖', '男');
        例: insert into t_6 values(1, '小明', '男');      --报错 主键值不能重复

        ps: 一个表中只能有一个主键;
        drop table if exists t_7;
        create table t_7(
            a int,
            b int
        );
        alter table t_7 modify a int primary key;    --添加主键
        alter table t_7 modify b int primary key;    --报错 一个表中只能有一个主键;
        alter table t_7 drop primary key;            --删除主键

        ps: 可以设置组合主键
        drop table if exists t_8;
        create table t_8(
            a int,
            b tinyint,
            primary key(a, b)
        );        
        例: insert into t_8 values(1,2);
        例: insert into t_8 values(1,3);
        例: insert into t_8 values(1,3);
    ********************************************************************************************************************

    6: 自动增长(auto_increment)
        create table t_9(
            id int primary key auto_increment,
            name varchar(30)
        );
        例: insert into t_9 values(null, '胖胖');      --如果添加了auto_increment,主键值可以用null来表示插入,但真正插入的并不是null值
        例: insert into t_9 values(null, '小明');
        例: insert into t_9 values(null, '小红');
        例: insert into t_9(name) values('哈哈');
            insert into t_9 values('小哈');            --报错
        ps: 自动增长需要整形和索引
            例: create table t_9(                      --报错
                    id int auto_increment
                );

        自动增长的初始值从1开始, 可以自定义这个初始值
        create table t_10(                    
            id int primary key auto_increment
        );
        例: alter table t_10 auto_increment 10;              --把自动增长的初始值 设置为10
        例: insert into t_10 values();
        例: insert into t_10 values(50);
        例: insert into t_10 values();                         --自动增长会从已有的最大值开始增长;
    ********************************************************************************************************************


  • 相关阅读:
    使用idea2017搭建SSM框架(转发:https://www.cnblogs.com/hackyo/p/6646051.html#!comments)
    Maven下载、安装和配置(转发:http://blog.csdn.net/jiuqiyuliang/article/details/45390313)
    IDEA main方法自动补全(转发:http://blog.csdn.net/zjx86320/article/details/52684601)
    IntelliJ IDEA 设置代码提示或自动补全的快捷键 (附IntelliJ IDEA常用快捷键)(转发:https://www.cnblogs.com/jx17/p/6244491.html)
    使用IntelliJ IDEA 15和Maven创建Java Web项目
    easyui学习笔记
    Bootstrap学习笔记
    IoC和AOP
    面向对象的五大原则
    面向对象的思想
  • 原文地址:https://www.cnblogs.com/wadmwz/p/7572672.html
Copyright © 2011-2022 走看看