一. 基本命令
1. 启动服务:
说明: 以管理员身份运行 cmd
格式: net start 服务名称
示例: net start MySQL
2. 停止服务:
说明: 以管理员身份运行 cmd
格式: net stop 服务名称
示例: net stop MySQL
3. 连接数据
格式: MySQL -u 用户名 -p
示例: MySQL -u root -p
输入密码(安装时设置的)
4. 退出登录(断开连接)
quit 或 exit
5. 查看版本(连接后可以执行)
示例: select version();
6. 显示当前时间(连接后执行)
示例: select now();
7. 远程连接
格式: MySQL -h ip 地址 -u 用户名 -p
输入对方的 MySQL 密码
#############################################
二. 数据库操作
1. 创建数据库
格式: create database 数据库名 charset=utf8;
示例: create database sunck charset=utf8;
2. 删除数据库
格式: drop database 数据库名;
3. 切换数据库
格式: use 数据库名;
4. 查看当前选择的数据库
格式: select database();
#############################################
三. 表操作
1. 查看当前数据库中所有表
show tables;
2. 创建表
格式: create table 表名(列及类型);
说明: auto_increment 表示自增长
primary key 主键
not null 不为空
示例: create table student(id int auto_increment primary key,name varchar(20) not null, age int not null, gender bit default 1, address varchar(20), isDelete bit default 0);
3. 删除表
格式: drop table 表名;
示例: drop table student;
4. 查看表结构
格式: desc 表名;
示例: desc student;
5. 查看建表语句
格式: show create table 表名;
6. 重命名表名
格式: rename table 原表名 to 新表名;
7. 修改表(结构)
格式: alter table 表名 add|change|drop 列名 类型;
add : alter table 表名 add 字段名 类型;
change: alter table 表名 change 要修改的旧字段名 新字段名 类型 ;
drop: alter table 表名 drop 字段名;
#############################################
四. 数据操作
1. 增
a. 全列插入:
格式: insert into 表名 values(...);
说明: 主键列是自动增长, 但是在全列插入时需要占位, 通常使用 0, 插入成功以后以实际数据为准
示例: insert into student values(0,"tom",19,1,"北京",0);
b. 缺省插入:
格式: insert into 表名(列 1, 列 2,....) values(值 1, 值 2....)
示例: insert into student(name, age ,address ) values("lilei", 19, "上海")
c. 同时插入多条数据:
格式: insert into 表名 values(.....),(......),.....
示例: insert into student values(0,"hanmeimei",18,0,"北京",0),(0,"cp",26,1,"北京",0),(0,"wangba",21,1,"北京",0);
2. 删
格式: delete from 表名 where 条件;
示例: delete from student where id=4;
注意: 没有条件是全部删除, 慎用
3. 改(数据)
格式: update 表名 set 列 1 = 值 1, 列 2 = 值 2,............where 条件
示例: update student set age=16 where id=7;
注意: 如果没有条件是全部列都修改, 慎用
4. 查
格式: select * from 表名
五. 查
1. 基本语法
格式: select * from 表名
说明:1.from 关键字后面是表名, 表示数据库来源于这张表
2.select 后面写表中的列名, 如果是 * 表示在结果集中显示表中的所有列
3. 在 select 后面的列名部分, 可以使用 as 为列名起别名, 这个别名显示在结果集中
4. 如果要查询多个列, 之间使用逗号分隔
示例: select * from student;
- select name,age from student;
- select name as a,age from student; (as 起外号的作用)
2. 消除重复行
在 select 后面, 列名前面使用 distinct 可以消除重复的行
示例: slect distinct name from student;
3. 条件查询
a. 语法
select * from 表名 where 条件
b. 比较运算符
等于 =
大于>
小于 <大于等于>=
小于等于 <=
不等于 != 或<>
需求: 查询 id 值大于 8 的所有数据????????
select * from student where id>8;
c. 逻辑运算符
and 并且
or 或者
not 非
需求: 查询 id 值大于 7 的女同学
select * from student where id>7 and gender=0;
d. 模糊查询
like
% 表示任意多个任意字符
_表示一个任意字符
需求: 查询姓王的同学
- select * from student where name like "王 %"
- select * from student where name like "王_"
e. 范围查询
in 表示在一个非连续的范围内
between...and... 表示在一个连续的范围内
需求: 查询编号为 8,10,12 的学生
select * from student where id in (8,10,12);
查询编号为 6 到 8 的学生
select * from student where id between 6 and 8;
f. 空判断
注意: null 与 "" 不同的
判断空: is null
判断非空: is not null
需求: 查询没有地址的同学
select * from student where address is null;
g. 优先级
小括号, not, 比较运算符, 逻辑运算符
and 比 or 优先级高, 如果同时出现并希望先算 or, 需要结合 () 来使用
4. 聚合
为了快速得到统计数据, 提供了五个聚合函数
a.count(*) 表示计算总行数, 括号中可以写 * 和列名
b.max(列) 表示求此列的最大值
c.min(列) 表示求此列的最小值
d.sum(列) 表示求此列的和
e.avg(列) 表示求此列的平均值
需求: 查询学生总数
select count(*) from student;
需求: 查询女生的编号最大值
select max(id) from student where gender=0;
需求: 查询女生的编号最小值
select min(id) from student where gender=0;
需求: 查询女生的年龄和
select sum(age) from student where gender=0;
需求: 查询所有学生的年龄平均值
select avg(age) from student;
5. 分组
按照字段分组, 表示此字段相同的数据会被放到一个集合中.
分组后只能查询出相同的数据列, 对于有差异的数据列, 无法显示在结果集中.
可以对分组后的数据进行统计, 做聚合运算
语法: select 列 1, 列 2, 聚合... from 表名 group by 列 1, 列 2, 列 3...,having 列 1, 列 2,...., 聚合....
需求: 查询男女生总数
select gender,count(*) from student group by gender;
分组后的数据筛选:
where 与 having 的区别:
where 对 from 后面指定的表进行筛选, 属于对原始数据的筛选
having 是对 group by 的结果进行筛选
6. 排序
语法: select * from 表名 order by 列 1 asc|desc, 列 2 asc|desc,....;
说明:
a. 将数据按照列 1 进行排序, 如果某些列 1 的值相同, 则按照列 2 进行排序, 后面同理
b. 不写 asc|desc, 默认按照从小到大数据排序
c.asc 升序
d.desc 降序
需求: 按年龄排序
- select * from student order by age;
- select * from student order by age desc,id desc;
7. 分页
语法: select * from 表名 limit start,count;
说明: start 索引从 0 开始
示例: select * from student limit 0,3;
- select * from student limit 3,3;
- select * from student where gender=1 limit 0,3;
六. 关联
建表语句:
- create table class(id int auto_increment primary key,name varchar(20) not null, stuNum int not null);
- create table students(id int auto_increment primary key,name varchar(20) not null,gender bit default 1,classid int not null,foreign key(classid) references class(id));
插入一些数据:
- insert into class values(0, "python01",55),(0, "python02",50),(0, "python03",60),(0, "python04",80);
- insert into students values(0,"tom",1,1);
关联查询:
select students.name,class.name from class inner join students on class.id=students.classid;
分类:
1. 表 A inner join 表 B
表 A 与表 B 匹配的行会出现在结果集中
2. 表 A left join 表 B
表 A 与表 B 匹配的行会出现在结果集中, 外加表 A 中独有的数据, 未对应的数据使用 null 填充
3. 表 A right join 表 B
表 A 与表 B 匹配的行会出现在结果集中, 外加表 B 中独有的数据, 未对应的数据使用 null 填充
来源: http://www.bubuko.com/infodetail-3269316.html