MySQL是一个 关系型数据库管理系统 , 由瑞典MySQL AB 公司开发,属于 Oracle 旗下产品。. MySQL 是最流行的 关系型数据库管理系统 之一,在 WEB 应用方面,MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件之一。. MySQL是一种关系型数据库管理系统,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。. MySQL所使用的 SQL 语言是用于访问 数据库 的最常用标准化语言。 MySQL 软件采用了双授权政策,分为社区版和商业版,由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选择 MySQL 作为网站数据库。
 

--创建数据库

create database demo

--使用数据据库
use demo

--use命令可以让我们来使用数据库。

--use命令格式: use <数据库名>;

--创建表

create table classinfo(

--int 类型 设置主键和自增
classid int primary key auto_increment,
className varchar(20)
)

create table student(
stuid varchar(20) primary key,
stuname varchar(20),
stusex char(2),
stuage int,
stuaddress varchar(50),
stuclassid int ,

--外键
FOREIGN KEY(stuclassid) REFERENCES classinfo(classid)
)
--添加数据
insert into classinfo VALUES(null,'J211201');
insert into classinfo VALUES(null,'J211202');
insert into classinfo VALUES(null,'J211203');
insert into classinfo VALUES(null,'J211204');
insert into classinfo VALUES(null,'J211205');

insert into student VALUES('12122','张三','男','20','河南商丘','1');
insert into student VALUES('12123','甄贞','女','22','河南周口','1');
insert into student VALUES('12124','张三三','女','24','香港','2');
insert into student VALUES('12125','王五','男','26','澳门','2');
insert into student VALUES('12126','王冰冰','女','28','上海','3');
insert into student VALUES('12127','雷霆嘎巴','男','30','北京','3');
insert into student VALUES('12128','小红','女','18','山东菏泽曹县','4');
insert into student VALUES('12129','武则天','女','31','四川成都','4');
insert into student VALUES('12130','奥里给','女','31','湖北武汉','4');

DROP TABLE student
--查询班级信息
SELECT*FROM classinfo
--修改学生地址
UPDATE student set stuaddress='山西省吕梁市文水县' where stuid='12129'
--删除某个学生信息
DELETE FROM student where stuid='12129'
--查询某个学生年龄大于20的学生信息
SELECT*FROM student where stuage>20
--查询学生年龄在20到30之间的学生信息
SELECT*FROM student where stuage BETWEEN 20 and 30
--查询性别实女生并且年龄大于30的学生信息
SELECT*FROM student where stuage>30 and stusex='女'
--查询性别为女的学生信息
SELECT*FROM student where stusex='女'
--查询地址是湖北武汉的学生信息
SELECT*FROM student where stuaddress='湖北武汉'
--查询班级编号为3的学生信息
SELECT*FROM student where stuclassid='3'
--查询学生姓王的学生信息  模糊查询
SELECT*FROM student where stuname LIKE'王%'
--查询学生姓王的学生信息并且年龄大于24岁的学生信息
SELECT*FROM student where stuname LIKE'王%' and stuage>24
--查询学生姓名中有王的学生信息
SELECT*FROM student where stuname LIKE'%王%'
SELECT*FROM student where stuname LIKE'王_'
--内连接查询   inner join on 查询学生信息,显示班级名称
SELECT s.*,c.className FROM student s inner join  classinfo c on s.stuclassid=c.classid
--查询每个班级的学生人数
SELECT count(*),c.className FROM student s inner join  classinfo c on s.stuclassid=c.classid GROUP BY s.stuclassid
--查询每个班级的学生人数,学生性别是男
SELECT count(*)as 男生人数,c.className FROM student s inner join  classinfo c on s.stuclassid=c.classid and s.stusex='男' GROUP BY s.stuclassid
--查询每个班级平均年龄大于24的信息  分组再筛选
select avg(stuage),stuclassid from student GROUP BY stuclassid HAVING avg(stuage)>24
--对年龄做一个升序的排序  降序desc
SELECT*FROM student ORDER BY stuage
SELECT*FROM student ORDER BY stuage desc
--分页-每页显示5行数据  pageSize=5,PageCode=3
SELECT*FROM student LIMIT 0,5
--外连接  左外(以左边的表为基准),右外(以右边的表为基准)
SELECT s.*,c.className FROM student s left outer join classinfo c on s.stuclassid=c.classid
SELECT s.*,c.className FROM student s RIGHT outer join classinfo c on s.stuclassid=c.classid
--查询前10条数据
SELECT*FROM student LIMIT 0,10
--查询班级编号为J211201的学生信息(子查询)
SELECT*FROM student where stuclassid in(SELECT classid FROM classinfo where className='J211201')
--查询年龄大于平均年龄的所有学生信息
SELECT*FROM student where stuage >(SELECT avg(stuage) FROM student )

--*******************************************************************************************************************************
create table dept(
deptno int PRIMARY KEY,
dname varchar(20),
loc varchar(50)
)

create table emp(
empno int ,
ename varchar(10),
job varchar(50),
mgr int,
hiredate datetime,
sal double,
comm double,
deptno int ,
FOREIGN KEY(deptno) REFERENCES dept(deptno)
)
create table boss(
bossid int ,
bossname varchar(20),
bosstime datetime
)
drop table boss
delete from boss where bossid='123'
insert into boss VALUES('123','周总','2021-12-22 17:24:32');
insert into boss VALUES('1234','王总','2021-12-21 17:24:32');
insert into boss VALUES('1235','张总','2021-12-21 17:24:32');
insert into boss VALUES('1236','赵总','2021-12-21 17:24:32');

insert into dept VALUES('609200','北京项目部','河南郑州');
insert into dept VALUES('609201','上海项目部','河南郑州');
insert into dept VALUES('609202','深圳项目部','河南郑州');
select*from dept


insert into emp VALUES('211206','sa','测试工程师','123','2021-12-22 16:43:32','1000','1000','609200');
insert into emp VALUES('211201','sab','测试工程师','123','2021-12-22 16:43:32','3000','1000','609200');
insert into emp VALUES('211202','a','初级开发工程师','1234','2021-12-22 16:43:32','200','1000','609200');
insert into emp VALUES('211203','bb','中级开发工程师','1235','2021-12-22 16:43:32','1000','1000','609201');
insert into emp VALUES('211204','dd','高级开发工程师','1236','2021-12-22 16:43:32','500','500','609201');
insert into emp VALUES('211205','qq','前端开发','1236','2021-12-22 16:43:32','500',null,'609201');
insert into emp VALUES('211206','qww','前端开发','1236','2021-12-22 16:43:32','1500',null,'609201');
--     提示:
-- emp员工表(empno员工号/ename员工姓名/job工作/mgr上级编号/hiredate受雇日期/sal薪金/comm佣金/deptno所属部门编号) 
-- dept部门表(deptno部门编号/dname部门名称/loc地点) 
-- 工资 = 薪金 + 佣金
-- 在Emp 表中完成如下练习:
-- 1、在emp表中查询出所有记录的姓名、部门编号、薪水,并且列名要显示为中文。
select ename as 姓名,sal as 薪水,deptno as 部门编号 from emp
-- 2、在emp表中查询出薪水大于1500的记录,并且按照薪水的降序排列。
select*from emp where (sal+ifnull(comm,0))>1000 ORDER BY (sal+ifnull(comm,0)) desc
-- 3、在emp表中查询出comm字段为空值的记录。
select*from emp where comm is null
-- 4、查询出emp表中含有几个部门的记录。(用DISTINCT去除重复记录)
select DISTINCT(deptno) from emp 
-- 5、在emp表中查询出部门编号为609200或6092001的记录(要求使用IN关键字)
select*from emp where deptno in(609200 , 609201)
-- 6、在emp表中查询出姓名的第二个字母为A的记录。
select*from emp where ename like '_A%'
-- 7、查询出emp表中总共有多少条记录。
SELECT count(*)from emp
-- 8、查询emp表中出每个部门的部门代码、薪水之和、平均薪水。
select sum(sal+ifnull(comm,0)) as 薪水之和,avg(sal+ifnull(comm,0)) as 平均工资 from emp GROUP BY deptno


-- 练习二:
-- 1.列出至少有一个员工的所有部门。 
SELECT deptno from emp GROUP BY deptno HAVING count(*)>=1
-- 2.列出薪金比“dd”多的所有员工。 
SELECT ename from emp where (sal+comm)>(SELECT (sal+comm) from emp where ename='dd')
-- 3.列出所有员工的姓名及其直接上级的姓名。 
SELECT e.ename,b.bossname from emp e left outer join boss b on e.mgr=b.bossid
-- 4.列出受雇日期早于其直接上级的所有员工。 
SELECT e.ename from emp e left outer join boss b on e.mgr=b.bossid where e.hiredate>b.bosstime
-- 5.列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门。
SELECT e.*,d.dname from emp e RIGHT outer join dept d on e.deptno=d.deptno
-- 6.列出所有“CLERK”(高级开发工程师)的姓名及其部门名称。 
SELECT e.ename,d.dname from emp e left outer join dept d on e.deptno=d.deptno
-- 7.列出最低薪金大于1500的各种工作。 
select DISTINCT(job) from emp where (sal+comm)>1500 
-- 8.列出在部门“SALES”(销售部)(北京项目部)工作的员工的姓名,假定不知道销售部的部门编号。
select ename from emp where deptno in(select deptno from dept where dname='北京项目部')
-- 9.列出薪金高于公司平均薪金的所有员工。 
SELECT ename from emp where (sal+ifnull(comm,0))>(SELECT avg(sal+ifnull(comm,0)) from emp )
-- 10.列出与“SCOTT”(sab)从事相同工作的所有员工。
select ename from emp where job=(select job from emp where ename='sab')
-- 12.列出薪金高于在部门30(上海项目部)工作的所有员工的薪金的员工姓名和薪金。
select ename,(sal+ifnull(comm,0))as 薪金 from emp where (sal+ifnull(comm,0))>(select sum(sal+ifnull(comm,0)) from emp where deptno=(select deptno from dept where dname='上海项目部'))
-- 13.列出在每个部门工作的员工数量、平均工资 
select count(*) as 员工数量,avg(sal+ifnull(comm,0)) as 平均工资,deptno as 部门编号 from emp GROUP BY deptno
-- 14.列出所有员工的姓名、部门名称和工资。 
SELECT e.ename as 部门名称,d.dname from emp e left outer join dept d on e.deptno=d.deptno
-- 15.列出所有部门的详细信息和部门人数。 
SELECT count(e.deptno)as 部门人数,d.* from emp e RIGHT outer join dept d on e.deptno=d.deptno GROUP BY deptno
-- 16.列出各种工作的最低工资。 
SELECT min(sal+ifnull(comm,0)) from emp
-- 17.列出薪金最低的MANAGER的基本信息。 
SELECT * from emp where (sal+ifnull(comm,0))=(SELECT min(sal+ifnull(comm,0)) from emp)
-- 18.列出所有员工的年工资,按年薪从低到高排序。
SELECT (sal+ifnull(comm,0))*12 as 年工资,ename from emp ORDER BY (sal+ifnull(comm,0))*12

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐