MySQL特点
- 关系型数据库
- 跨平台
- 支持多种编程语言(python、java、php)
- 基于磁盘存储,数据是以文件形式存放在数据库目录/var/lib/mysql下
启动连接
- 服务端启动
sudo /etc/init.d/mysql start|stop|restart|status
sudo service mysql start|stop|restart|status
- 客户端连接
mysql -hIP地址 -u用户名 -p密码
本地连接可省略 -h 选项
基本SQL命令
库管理
1、查看已有库;
show databases;
2、创建库并指定字符集;
create database 库名 charset utf8;
3、查看当前所在库;
select database();
4、切换库;
use 库名;
5、查看库中已有表;
show tables;
6、删除库;
drop database 库名
表管理
1、创建表并指定字符集;
create table 表名(字段名 类型 xxx)charset=utf8;
2、查看创建表的语句 (字符集、存储引擎);
show create table 表名
3、查看表结构;
desc 表名
4、删除表;
drop table 表名1,表名2
表字段管理(alter table 表名)
1、增 :
alter table 表名 add 字段名 类型 first|after xxx
2、删 :alter table 表名 drop 字段名
3、改 :alter table 表名 modify 字段名 新类型
4、表重命名:alter table 表名 rename 新表名
表记录管理
1、增 :
insert into 表名(字段名) values(字段1值,字段2值),()....
2、删 :delete from 表名 where 条件
3、改 :update 表名 set 字段名=值 where 条件
4、查 :select 字段名 或者 * from 表名 where 条件
数据类型
四大数据类型
- 数值类型
int
4字节
smallint
2字节
tinyint
1字节
bigint
8字节
float
decimal(7,3)
格式:0000.000 `
- 字符类型
char()
varchar()
text
blob
varchar 预留1-2字节 用于存储 当前字段实际存储的数据长度
- 枚举类型
enum()
set()
- 日期时间类型
date
,datetime
,timestamp
,time
,year
日期时间函数
NOW()
CURDATE()
CURTIME()
YEAR(字段名)
日期时间运算
select * from 表名 where 字段名 运算符(NOW()-interval 间隔);
间隔单位: 1 day | 3 month | 2 year
例如:查询1年以前的用户充值信息
select * from user_pay where created_time < (NOW() - interval 1 year)
MYSQL运算符
- 数值比较
>
>=
<
<=
=
!=
例如1 : 查询成绩不及格的学生
select * from students where score < 60;
例如2 : 删除成绩不及格的学生
delete from students where score < 60;
例如3: 把id为3的学生的姓名改为 周芷若
update students set name='周芷若' where id=3;
- 逻辑比较
and
or
例如 : 查询成绩不及格的男生
select * from students where score < 60 and gender = 'M';
例如 : 查询成绩在60-70之间的学生
select * from students where score > 60 and score < 70;
- 范围内比较
between 值1 and 值2
、in()
、not in()
例如 : 查询不及格的学生姓名及成绩
select name,score from students where score between 0 and 59
例如 : 查询AID19和AID18班的学生姓名及成绩
select name,score from students where class in('AID19','AID18');
- 模糊比较(like)
where 字段名 like 表达式( % _ )
例如 : 查询北京的姓赵的学生信息
select * from students where address='bj' and name like '赵%';
- NULL判断
is NULL
、is not NULL
例如 : 查询姓名字段值为NULL的学生信息
select * from students where name is NULL;
查询
- order by
给查询的结果进行排序(永远放在SQL命令的倒数第二的位置写)
order by 字段名 ASC/DESC
例如 : 查询成绩从高到低排列
select * from students order by score DESC
- limit
限制显示查询记录的条数(永远放在SQL命令的最后写)
limit n
:显示前n条
limit m,n
:从第(m+1)条记录开始,显示n条
limit (6-1)*10 , 10
分页:每页显示10条,显示第6页的内容
举例:
-
查找所有蜀国人的信息
select * from sanguo where country='蜀国';
-
将赵云的攻击力设置为360,防御力设置为68
update sanguo set attack=360,defence=68 where name='赵云';
-
将吴国英雄中攻击值为110的英雄的攻击值改为100,防御力改为60
update sanguo set attack=100, defence=60 where country='吴国';
-
找出攻击值高于200的蜀国英雄的名字、攻击力
select name,attack from sanguo where country='蜀国' and attack > 200;
-
将蜀国英雄按攻击值从高到低排序
select * from sanguo where country='蜀国' order by attack DESC;
-
魏蜀两国英雄中名字为三个字的按防御值升序排列
select * from sanguo where country in('魏国','蜀国') and name like '___' order by defence;
-
在蜀国英雄中,查找攻击值前3名且名字不为 NULL 的英雄的姓名、攻击值和国家
select name, attack,country from sanguo
where country='蜀国' and name is not NULL
order by attack DESC
limit 3;