目录
MYSQL增删改查
约束条件
auto_increment: 自增 1
primary key :主键索引,加快查询速度,列的值不能重复
not null:标识字段不能为空
default:为该字段设置默认值
数字
tinyint:
为一个字节 ,范围有符号:(-128,127),范围无符号(unsigned):(0,255)
smallint:
为二个字节,范围有符号:(-32 768,32 767),范围无符号(unsigned):(0,65 535)
mediumint:
为三个字节,范围有符号:(-8 388 608,8 388 607),范围无符号(unsigned):(0,16 777 215)
int: (************************) 推荐使用
为四个字节,范围有符号:(-2 147 483 648,2 147 483 647),范围无符号(unsigned):(0,4 294 967 295)
bigint:
为八个字节,范围有符号:(-9 233 372 036 854 775 808,9 223 372 036 854 775 807),范围无符号(unsigned):(0,18 446 744 073 709 551 615)
字符串
char:
0-255字节 定长字符串
varchar:
0-65535字节 边长字符串
时间日期类型
date:
3个字节,1000-01-01/9999-12-31 格式:YYYY-MM-DD 用途:年月日
time:
3个字节,'-838:59:59'/'838:59:59' 格式:HH:MM:SS 用途:时分秒
datetime:
3个字节,1000-01-01 00:00:00/9999-12-31 23:59:59 格式:YYYY-MM-DD\HH:MM:SS 用途:年月日时分秒
操作表:
create table 表名(
字段名 列表名 [可选的参数],# 用逗号隔开
字段名 列表名 [可选的参数],# 用逗号隔开
字段名 列表名 [可选的参数],# 用逗号隔开
)charset=utf8;
create table t1(
id int auto_increment primary key,
name varchar(32) not null default'xx',
age int not null default 0
gender enum('male','female') #枚举
)charset=utf8
修改表名
alter table 旧名字 remane 新名字;
增加字段
alter table add 字段名 列类型 [可选参数]
alter table add 字段名 列类型 [可选参数] first;插入到第一个位置删字段
alter table 类名 drop 字段名;
修改字段
alter table 表名 modify 字段名 数据类型 [完整性约束条件]
alter table 表明 change 旧字段名 新字段名 新数据类型 [完整性约束条件]
删表
drop table 表名查表
show tables
操作表数据行
增:
insert into 表名 (列1,列2) values(值1,'值2')
insert into t1 (id, name) values (1, 'zekai');
删:
delete from 表名 where 条件;
mysql> delete from t5 where id=1;
delete from 表名 ;删除表中所有数据
truncate 表名; 没有where 条件
区别:
- delete之后,插入数据从上一次主键自动增加1开始,truncate则从1开始
- delete删除,是一行一行删除,truncate则全选删除,他的删除速度高于delete.
改:
upday 表名 set 列名=新值1,列名2=新值2 where 条件
mysql> update t66 set name='xxxx' where id=30;
查:
select 列1, 列2 from 表名; (*代表查询所有的列)
between..and... 取值范围是闭区间
mysql> select * from t66 where id between 31 and 33;
distinct避免重复
mysql> select distinct name from t66;
四则运算
mysql> select name, age*10 as age from t3;
mysql> select * from t66 where id in (23,34,11);
模糊查询:
mysql> select * from t66 where name like 'x%'; # 以X开头