Mysql数据库
Mysql数据库
文章目录
- Mysql数据库
-
- @[TOC](文章目录)
- 一、DDL操作数据库
-
- 1.查询
- 2.创建
- 3.删除
- 4.使用数据库
- 二、DDL操作表
-
- 1.创建表
- 2.查询表
- 3.修改表
- 4.删除表
- 5.案例
- 二、DML操作数据
-
- 1.添加数据
- 2.删除数据
- 3.修改数据
- 三、DQL
-
- 1.基础查询
-
- 1.查询多个字段
- 2.去除重复记录
- 3.起别名
- 2.条件查询
- 3.模糊查询
- 4.排序查询
- 5.聚合函数
- 6.分组查询
- 7.分页查询
- 四、约束
-
- 1.约束概述
- 2.约束案例
- 3.外键约束
- 五、多表查询
-
- 1.内连接
- 2.外连接
- 3.子查询
- @[TOC](文章目录)
- 1.查询
- 2.创建
- 3.删除
- 4.使用数据库
- 1.创建表
- 2.查询表
- 3.修改表
- 4.删除表
- 5.案例
- 1.添加数据
- 2.删除数据
- 3.修改数据
- 1.基础查询
-
- 1.查询多个字段
- 2.去除重复记录
- 3.起别名
- 2.条件查询
- 3.模糊查询
- 4.排序查询
- 5.聚合函数
- 6.分组查询
- 7.分页查询
- 1.约束概述
- 2.约束案例
- 3.外键约束
- 1.内连接
- 2.外连接
- 3.子查询
一、DDL操作数据库
1.查询
show databases;
2.创建
a.创建数据库
create databases 数据库名称;
b.创建数据库(判断,如果不存在则创建)
create databases if not exists 数据库名称;
3.删除
a.删除数据库
drop databases 数据库名称;
b.删除数据库(判断,如果存在则删除)
drop databases if not exists 数据库名称;
4.使用数据库
a.查看当前使用数据库
select databases();
b.使用数据库
use 数据库名称;
二、DDL操作表
1.创建表
create table 表名( 字段名1 数据类型1, 字段名2 数据类型2, 字段名3 数据类型3, ................. ................. .................);
2.查询表
a.查询当前数据库下的所有表名称
show tables;
b.查询表结构
desc 表名称;
3.修改表
a.修改表名
alter table 表名 rename to 新表名;
b.添加一列
alter table 表名 add 列名 数据类型;
c.修改数据类型
alter table 表名 modify 列名 新数据类型;
d.修改列名和数据类型
alter table 表名 change 列名 新列名 新数据类型;
e.删除列
alter table 表名 drop 列名;
4.删除表
drop table 表名;
5.案例
二、DML操作数据
1.添加数据
案例:
2.删除数据
案例:
3.修改数据
案例:
三、DQL
1.基础查询
1.查询多个字段
2.去除重复记录
3.起别名
2.条件查询
--条件查询--查询年龄>40select * from stu where age>40;--查询年龄大于40小于60select * from stu where age>40 && age<60;select * from stu where age>40 and age<60;select * from stu where age between 20 and 60;--查询出生日期从1990-2000年select * from stu where hire_date between 1890-00-01 and 1990-12-01;--查询年龄为64select * from stu where age=64;--查询年龄不为64select * from stu where age!=64;--查询年龄为44,45,64select * from stu where age=64 || age=45 || age=44;select * from stu where age=64 or age=45 or age=44;select * from stu where age in (44,45,64);--查询英语成绩为nullselect * from stu where english is null;
3.模糊查询
--模糊查询--查询姓张的select * from stu where name like '张%';--查询名字第二个字是奎的select * from stu where name like '_奎%';--查询名字中包含飞的select * from stu where name like '%飞%';
4.排序查询
--排序查询--按照年龄升序排序select * from stu order by age asc;--按照数学成绩降序排序select * from stu order by math desc;--按照数学成绩降序排序,如果相等按照英语成绩升序排序select * from stu order by math desc,english asc;
5.聚合函数
--聚合函数--查询一共有多少人,counto统计列名不能为null,可以是主键或*select count(id) from stu;select count(*) from stu;--查询数学成绩最高分select max(math) from stu;--查询数学成绩最低分select min(math) from stu;--查询数学成绩总分select sum(math) from stu;--查询数学成绩平均分select avg(math) from stu;--查询英语成绩最低分(因为英语成绩中有个为null)null不参与聚合函数运算select min(english) from stu;
6.分组查询
--分组查询--查询男同学和女同学的数学平均分select sex,avg(math) from stu group by sex;--查询男同学和女同学的数学平均分,以及各自的人数select sex,avg(math),count(*) from stu group by sex;--查询男同学和女同学的数学平均分,以及各自的人数,要求分数大于60的进行分组select sex,avg(math),count(*) from stu where math>60 group by sex;--查询男同学和女同学的数学平均分,以及各自的人数,要求分数大于70的进行分组,分组后人数大于3select sex,avg(math),count(*) from stu where math>70 group by sex having count(*) >3;
7.分页查询
--分页查询--从0开始查询,查询三条数据select * from stu limit 0,3; --每页查询三条数据,查询第一页数据select * from stu limit 0,3; --每页查询三条数据,查询第一页数据select * from stu limit 3,3; 每页查询三条数据,查询第一页数据select * from stu limit 6,3;注意:起始索引=(当前页码-1)*每页条数
四、约束
1.约束概述
2.约束案例
create table emp(id int primary key auto_increment, --非空唯一,自增ename varchar(50) not null unique, --非空唯一joindate date not null, --非空salary double(7,2) not null, --非空bonus double(7,2) default 0 --);
3.外键约束
五、多表查询
1.内连接
案例:
--多表查询select * from dept,emp;--消除无效数据--隐式内连接select * from emp,dept where emp.dep_id =dept.did;--查询emp表的name,gender,dept的dname select emp.name,emp.gender,dept.dname from emp,dept where emp.dep_id =dept.did; --给表起别名 select e.name,e.gender,d.dname from emp e,dept d where e.dep_id =d.did; --显示内连接 select * from emp inner join dept on emp.dep_id =dept.did; --join可以省略 select * from emp inner join dept on emp.dep_id =dept.did;
2.外连接
案例:
--左外连接 select * from emp left join dept on emp.dep_id =dept.did; --右外连接 select * from emp right join dept on emp.dep_id =dept.did;
3.子查询
案例:
--子查询 --单行单列 --查询高于猪八戒的工资 select salary from emp where name='猪八戒'; select * from emp where salary>(select salary from emp where name='猪八戒'); --多行单列--查询财务部和市场部的所有人信息select did from dept where dname='财务部';select * from emp where dep_id in(select did from dept where dname='财务部' or dname='市场部');--多行多列--查询入职日期是2011-11-11之后的员工信息和部门信息select * from emp where join_date>'2011-11-11';select * from (select * from emp where join_date>'2011-11-11') t,dept where t.dep_id =dept.did;