Mysql 列属性约束_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_4\G

例: 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_4\G 来查看约束名;

例: 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();                         --自动增长会从已有的最大值开始增长;

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值