MYSQL笔记
参照《MySQL数据库原理、设计与应用》清华大学出版社
第三章 数据库基本操作
3.1数据类型
3.1.1数字类型
1.整数类型
2.浮点数类型
3.定点数类型
DECIMAL
4.BIT类型
3.1.2时间和日期类型
3.1.3字符串类型
3.2表的约束
3.2.1默认约束(default)
①创建时 create table xxx( 字段名 字段类型 default 默认约束值);
②已创建增加或修改default alter table xxx modify 字段名 字段类型 default 默认约束值;
③去除default alter table xxx modify 字段名 字段类型;
法二
alter table 表名 alter column 字段名 drop default; (若本身存在,则删除)
alter table 表名 alter column 字段名 set default 默认值;(若不存在则可直接设)
3.2.2非空约束(not null)
①创建时 create table xxx( 字段名 字段类型 not null);
②创建后增加 alter table xxx modify 字段名 字段类型 not null;
③去除not null alter table xxx modify 字段名 字段类型;
3.2.3唯一约束(unique)
①创建时
create table xxx( 字段名 字段类型 unique);
②已创建后增加unique
alter table xxx modify 字段名 字段类型 unique;
alter table xxx add unique(字段名);
③删除unique alter table xxx drop index 字段名;
3.2.4主键约束(primary key)(既不能重复,也不能为空)(一个表内只能有一个)
①创建时
create talbe xxx( 字段名 字段类型 primary key);
②追加primary key 法一:alter table xxx add primary key(字段名); 法二:alter table xxx modify 字段名 字段类型 primary key;
③删除primary key(只能去掉key)(不能重复,但还不能为空)
alter table xxx drop primary key;
3.3自动增长(配合primary key使用)
①创建时 create table xxx( 字段名 字段类型 primary key auto_increment);
②追加 alter table xxx modify 字段名 字段类型 auto_increment;
③删除自动增长 alter table xxx modify 字段名 字段类型 unsigned;