基本操作
mysql -uroot -proot
# 登录数据库
show databases;
# 显示数据库
use xxx;
# 选择数据库
create database xxx;
# 创建数据库
create table xxx;
# 创建数据表
show tables;
# 显示数据表
select * from xxx;
# 查询数据表
drop dstabases xxx;
# 删除数据库
drop table xxx;
# 删除数据表
exit
# 退出数据库
创建数据表的小
democreate table if not exists 'xxx'( 'id' int unsigend auto_increment, 'name' varchar(22) not null, 'age' int() not null, 'sex' varchar(1) not null, 'data' date, primary key ('id'), )charset=utf8;
数据类型
- 数值类型
| 类型 | 说明 |
|---|---|
| tinyint | 小整数 |
| smallint | 大整数 |
| mediumint | 大整数 |
| int/integer | 大整数 |
| bigint | 极大整数 |
| float | 单精度浮点数 |
| double | 双精度浮点 |
| decimal(m,d) | 小数 |
- 日期和时间
| 类型 | 说明 |
|---|---|
| date | 日期值 |
| time | 时间值 |
| year | 年份值 |
| datetime | 混合时间值 |
| timestamp | 混合日期和时间值,时间戳 |
添加,删除,修改
创建数据表
create table if not exists 'xxx'( 'id' int unsigend auto_increment, 'name' varchar(22) not null, 'age' int() not null, 'sex' varchar(1) not null, 'data' date, primary key ('id'), )charset=utf8;添加数据
insert into xxx(id,name,age,sex,data) values (1,'ikun','21','坤',now());删除数据表
drop table xxx;修改数据表结构
alter table xxx modify name varchar(13) not null;
查询
select * from xxx;
# 查询
select name,age,sex from xxx;
# 直接查询
select * from xxx where age>20;
# 条件查询
select * from xxx where name like %ikun%;
# 模糊查询
select * from xxx where name=ikun and/or/not age>20;
# 逻辑运算符
select * from xxx where age in/not in(20,21,22,23);
# in/not in
select * from xxx where order by age desc/asc;
# 排序查询 asc(升序) desc(降序)
运算符
| 运算符 | 功能 |
|---|---|
| = | 等于 |
| != | 不等于 |
| > | 大于 |
| < | 小于 |
| >= | 大于等于 |
| <= | 小于等于 |
| between | 包含之间 |
| not between | 不包含之间 |
| in | 包含 |
| not in | 不包含 |
| <=> | 相等(严格判断) |
| like | 模糊查询 |
| regexp/rlike | 正则查询 |
| is null | 空 |
| is not null | 不为空 |
函数
| 函数 | 功能 |
|---|---|
| abs() | 绝对值 |
| avg() | 平均值 |
| max() | 最大值 |
| min() | 最小值 |
| sum() | 求和 |
| count() | 统计 |