认值约束: 给未添加字段值的字段 赋值为默认值
建表时: 直接在字段名后 写 default '值'
建表后:alter table 表名 modify 列的定义 default '值';
删除默认值约束:
alter table 表名 modify 列的定义
非空约束:设置非空约束的字段 不能添加null值
建表时: 直接在字段名后 写 not null
建表后: alter table 表名 modify 列的定义 not null;
删除非空约束:
alter table 表名 modify 列的定义;
reate table other(
id int primary key,
sname varchar(20) not null,
sex varchar(20) default '男'
);
-- 建表后添加默认值约束
create table other(
id int primary key,
sname varchar(20),
sex varchar(20)
);
-- 添加默认值约束
alter table other modify sex varchar(20) default '男';
-- 添加非空约束
alter table other modify sname varchar(20) not null;
drop table other;
select * from information_schema.`TABLE_CONSTRAINTS` where table_name='other';
insert into other values (1,"李白","男");
insert into other values (2,"李清照","女");
insert into other values (3,"张三",null);
insert into other(id,sname) values (6,"赵六");
insert into other(id,sname) values (6,null);
select * from other;
-- 去除默认值约束
alter table other modify sex varchar(20);
-- 去除非空约束
alter table other modify sname varchar(20);