数据库
-
学习数据库主要学习的就是如何对数据进行增删改查操作.
增加(插入数据) 删除数据 修改数据 查询数据SQL语言
-
Structured Query Language:结构化查询语言,用户程序员和数据库软件进行交流
-
执行SQL语言之前需要先连接数据库软件
- windows: 开始->MySQL/MariaDB->MySQL Client 点击运行 然后输入自己的密码 回车
- linux: 在桌面右键->打开终端 输入以下指令
mysql -uroot -p 回车 输入密码 再回车
-
退出命令: exit;
Access denied for user ‘root’@‘localhost’ (using password: YES) 密码错误
SQL分类
-
DDL: 数据定义语言, 负责数据库和表相关的操作
-
DML: 数据操作语言, 负责数据增删改
-
DQL: 数据查询语言, 负责查询数据
-
DCL: 数据控制语言, 负责控制用户权限相关
-
TCL: 事务控制语言, 事务相关
DDL
数据库相关SQL
-
数据库软件中要想保存数据需要先建库再键表
- 查询所有数据库
- 格式:
show databases;
- 创建数据库
- 默认字符集格式: create database 数据库名;
create database db1;
- 指定字符集格式: create database 数据库名 character set gbk/utf8;
create database db2 character set gbk;
create database db3 character set utf8;
- 查询数据库详情
- 格式: show create database 数据库名;
show create database db1;
- 删除数据库
- 格式: drop database 数据库名;
drop database db1;
- 使用数据库
- 查询所有
show databases;
- 创建 create database 数据库名
character set utf8/gbk;
- 查询详情 s
how create database 数据库名
; - 删除
drop database 数据库名
; - 使用数据库
use 数据库名
;数据库相关练习
- 分别创建mydb1和mydb2 第一个utf8 第二个gbk
create database mydb1 character set utf8;
create database mydb2 character set gbk;
- 查询所有数据库检查是否创建成功
show databases;
- 分别查询mydb1和mydb2检查字符集是否正确
show create database mydb1;
show create database mydb2;
- 先使用mydb1 再使用mydb2
use mydb1;
use mydb2;
- 删除两个数据库
drop database mydb1;
drop database mydb2;
表相关的SQL语句
- 操作表相关的SQL 必须先使用某个数据库
create database mydb1;
show create database mydb1;
use mydb1;
- 如果默认不是utf8,以后创建数据库需要制定字符集为utf8
create database mydb1 character utf8;
- 创建表
- 格式: create table 表名(字段1名 类型,字段2名 类型);
create table person(name varchar(5),age int);
- 制定字符集格式:create table 表名(字段1名 类型,字段2名 类型) charset=utf8/gbk;
create table student(name varchar(10),score int) charset=gbk;
create table hero(name varchar(10),money int) default charset=utf8;
- 查询所有表
- 格式:
show tables;
- 查询表详情
- 格式: show create table 表名;
show create table person;
show create table hero;
- 查询表字段
- 格式: desc 表名;
desc person;
- 删除表
- 格式: drop table 表名;
drop table hero;
- 修改表名
- 格式: rename table 原名 to 新名;
rename table person to t_person;
- 添加表字段
- 最后添加格式:alter table 表名 add 字段名 类型;
alter table student add gender varchar(5);
- 最前面添加格式:alter table 表名 add 字段名 类型 first;
alter table student add id int first;
- 在某个字段的后面添加: alter table 表名 add 字段名 类型 after xxx;
alter table student add money int after name;
- 删除表字段
- 格式: alter table 表名 drop 字段名;
alter table student drop money;
- 修改表字段
- 创建表:
create table t1(name varchar(10),age int)charset=utf8/gbk;
- 查询所有
show tables
; - 查询详情
show create table t1
; - 查询表字段
desc t1
; - 删除表
drop table t1
; - 修改表名
rename table t1 to t2
; - 添加表字段
alter table t1 add age int first/after xxx
; - 删除表字段
alter table t1 drop age
; - 修改表字段
alter table t1 change
原名 新名 新类型;表相关练习
- 创建mydb2 字符集utf8 并使用该数据库
create database mydb2 character set utf8;
use mydb2;
- 在mydb2中创建员工表emp 字段有name 字符集utf8
create table emp(name varchar(10)) charset=utf8;
- 最后面添加age字段
alter table emp add age int;
- 最前面添加id字段
alter table emp add id int first;
- name后面添加gender字段
alter table emp add gender varchar(5) after name;
- 修改gender字段为工资salary
alter table emp change gender salary int;
- 删除age字段
alter table emp drop age;
- 修改表名为
t_emp rename table emp to t_emp;
- 删除表 删除数据库
drop table t_emp;
drop database mydb2;
DML数据操作语言
- 执行操作数据的SQL必须保证已经使用了某个数据库,并且存在数据所对应的表
create database mydb3 character set utf8;
use mydb3;
create table person(name varchar(10),age int)charset=utf8;
- 插入数据(增)
- 全表插入格式(要求值的数量和顺序和表字段保持一致):
insert into 表名 values(值1,值2,值3);
insert into person values("Tom",18);
- 指定字段插入格式(要求值得数量和顺序和指定的一致)
insert into 表名(字段名1,字段名2) values(值1,值2);
insert into person(name) values('Jerry');
- 批量插入:
insert into person values('Lucy',20),('Lily',21);
insert into person(name) values('Lilei'),('Hanmeimei');
- 插入中文
insert into person values('刘德华',50);
如果执行上面SQL语句出现错误提示 提示中包含16进制内容,执行以下SQL
set names gbk;
insert into person values('刘德华',50);
- 查询数据
- 格式: select 字段信息 from 表名 where 条件;
select name from person;
select name,age from person where age>20;
select * from person where age=50;
- 修改数据
- 格式: update 表名 set 字段名=xxx,字段名=xxx where 条件;
- 举例:
update person set age=8 where name='Tom';
update person set age=10 where age is null;
- 删除数据
- 格式: delete from 表名 where 条件;
delete from person where age=10;
delete from person where name='刘德华';
delete from person where age<=20;
delete from person;
操作数据相关SQL回顾
- 插入 insert into 表名(字段1,字段2) values(值1,值2),(值1,值2);
- 查询 select 字段信息 from 表名 where 条件;
- 修改 update 表名 set 字段名=xxx,字段名=xxx where 条件;
- 删除 delete from 表名 where 条件;
数据相关练习题
- 创建hero表 字段: id,name,job(职业),money
create table hero(id int,name varchar(10),job varchar(5),money int)charset=utf8;
- 插入以下数据:
insert into hero values(1, '诸葛亮', '法师', 18888),(2, '周瑜', '法师', 13888),(3, '刘备', '战士', 8888),(4, '孙尚香', '射手', 6888),(5 ,'黄忠', '射手', 8888);
- 修改13888为28888
update hero set money=28888 where money=13888;
- 修改所有射手价格为3000
update hero set money=3000 where job='射手';
- 删除价格为18888的英雄
delete from hero where money=18888;
- 修改孙尚香为猪八戒
update hero set name='猪八戒' where name='孙尚香';
- 删除价格低于5000的英雄
delete from hero where money<5000;
- 给表添加gender字段在name字段后面
alter table hero add gender varchar(5) after name;
- 修改所有英雄性别为男
update hero set gender='男';
- 删除表里面所有数据
delete from hero;
- 删除表
drop table hero;
数据类型
- 整数类型: int(m)和bigint(m) 等效java中的long , m代表显示长度
create table t1(name varchar(10),age int(10) zerofill);
insert into t1 values('aaa',18);
select * from t1;
- 浮点数: double(m,d) m代表总长度 d代表小数长度 53.234 double(5,3) ,超高精度浮点数 decimal(m,d)精度远高于double 只有涉及超高精度运算时使用.
- 字符串:
- char(m): 固定长度, m=10 存abc 占10 , 优点:执行效率略高 ,最大长度255
- varchar(m): 可变长度,m=10 存abc 占3 , 优点:更节省空间,最大长度65535,但是建议存255以下
- text(m):可变长度, 最大长度65535, 建议保存长度大于255的
- 日期
- date: 只能保存年月日
- time: 只能保存时分秒
- datetime: 默认值为null ,最大值9999-12-31
- timestamp(时间戳:距1970年1月1号的毫秒数): 默认值为当前系统时间,最大值2038-1-19
- 举例:
create table t_date(t1 date,t2 time,t3 datetime,t4 timestamp);
insert into t_date values(null,null,'2020-6-12 16:19:20',null);
insert into t_date values('2018-10-12','15:30:20',null,null);
select * from t_date;
*.sql文件
- 此文件是从数据库中导出的数据,里面有很多条的SQL语句.
- 把emp.sql文件解压出来后 赋值到某个盘的根目录(如果linux系统放在桌面)
- 通过以下SQL语句导入该文件
windows系统: source d:/emp.sql;
linux系统: source /home/soft01/桌面/emp.sql; - 提示一堆Query OK 说明导入完成
- 测试:
** show tables;
select * from emp; 如果显示乱码 执行 set names gbk; 再查**
去重distinct
- 查询员工表中有哪些工作
select distinct job from emp;
- 查询员工表中的部门编号deptno有哪些
select distinct deptno from emp;
is null和is not null
- 查询没有上级领导的员工信息
select * from emp where mgr is null;
- 查询有上级领导的员工姓名 工资和工作
### 比较运算符 > < >= <= = !=和<>
- 查询工资小于等于3000的员工姓名和工资
select ename,sal from emp where sal<=3000;
- 查询不是程序员的员工姓名和工作(两种写法)
select ename,job from emp where job!='程序员';
select ename,job from emp where job<>'程序员';
and和or
- and类似java中的 && ,当需要同时满足多个条件的时候使用
- or类型java中的||, 当多个条件满足一个就行的时候使用
- 查询1号部门工资大于2000的员工信息
select * from emp where deptno=1 and sal>2000;
- 查询工作是人事或者工资大于3000的员工姓名 工作和工资
select ename,job,sal from emp where job='人事' or sql>3000;
in
- 当查询某个字段的值为多个值得时候使用
- 查询工资为,3000/1500/5000的员工信息
select * from emp where sal=3000 or sal=1500 or sal=5000;
select * from emp where sal in(3000,1500,5000);
between x and y 包含xy
- 当查询某个字段的值在某两个值之间的时候使用
- create database db1 character set utf8/gbk;
- show databases;
- show create database db1;
- drop database db1;
- use db1;
- 表相关
- create table t1(name varchar(10),age int) charset=utf8/gbk;
- show tables;
- show create table t1;
- desc t1;
- drop table t1;
- rename table t1 to t2;
- alter table t1 add age int first/after xxx;
- alter table t1 drop age;
- alter table t1 change 原名 新名 新类型;
- 数据相关
- insert into t1(name,age) values(xxx,xxx),(xxx,xxx);
- select 字段信息 from t1 where 条件
- update t1 set xxx=xxx where 条件
- delete from t1 where 条件
- 去重 distinct
- is null 和 is not null
- 比较运算符 > < >= <= = !=和<>
- and 和 or
- in
- between x and y
综合练习
-
查询没有上级领导的员工编号empno,姓名,工资
select empno,ename,sal from emp where mgr is null;
-
查询有奖金的员工姓名和奖金
select ename,comm from emp where comm>0;
-
查询名字中包含精的员工姓名和工资
select ename,sal from emp where ename like '%精%';
- 查询名字中第二个字是八的员工信息
select * from emp where ename like '_八%';
- 查询1号部门工资大于2000的员工信息
select * from emp where deptno=1 and sal>2000;
-
查询2号部门或者工资低于1500的员工信息
select * from emp where deptno=2 or sal<1500;
-
查询工资为3000,1500,5000的员工信息按照工资升序排序
select * from emp where sal in(3000,1500,5000) order by sal;
- 查询3号部门的工资总和
select sum(sal) from emp where deptno=3;
- 查询每个部门工资大于1000的员工人数,按照人数升序排序
select deptno,count(*) c from emp where sal>1000 group by deptno order by c;
-
查询每种工作中有领导的员工人数按照人数降序排序
select job,count(*) c from emp where mgr is not null group by job order by c desc;
-
查询所有员工信息,按照部门编号升序排序,如果部门编号一致则工资降序
select * from emp order by deptno,sal desc;
-
查询有领导的员工,每个部门的编号和最高工资
select deptno,max(sal) from emp where mgr is not null group by deptno;
-
查询有领导的员工,按照工资升序排序,第3页的2条数据
select * from emp where mgr is not null order by sal limit 4,2;
-
查询每个部门的工资总和,只查询有上级领导的员工并且要求工资总和大于5400,最后按照工资总和降序排序,只查询结果中的第一条数据
where mgr is not null group by deptno having s>5400 order by s desc limit 0,1;
关联关系
创建表时,表与表之间存在的业务关系
外键: 用来建立关系的字段称为外键
有哪些关系:
-
一对一:有AB两张表,A表的一条数据对应B表的一条,同时B表的1条也对应A表的一条,称为一对一关系。
如何创建一对一的两张表:在从表(userinfo)中添加外键指向主表(user)的主键。
-
create table user(id int primary key auto_increment,username varchar(10),password varchar(10))charset=utf8;
create table userinfo(uid int,nick varchar(10),email varchar(20))charset=utf8;
insert into user values(null,'libai','123456');
insert into userinfo values(1,'刺客','xxx@163.com');
-
一对多:有AB两张表,A表的一条数据对应B表的多条,同时B表的1条对应A表的一条,称为一对多关系。
如何创建一对多的两张表: 外键添加在多的表中
create table team(id int primary key auto_increment,name varchar(10))charset=utf8;
create table player(id int primary key auto_increment,name varchar(10),tid int)charset=utf8;
insert into team values(null,'北京队');
insert into player values(null,'林书豪',1),(null,'王老五',1);
-
多对多:有AB两张表,A表的一条数据对应B表的多条,同时B表的1条对应A表的多条,称为多对多关系。
如何创建多对多的两张表: 创建单独的关系表
create table student(id int primary key auto_increment,name varchar(10))charset=utf8;
create table teacher(id int primary key auto_increment,name varchar(10))charset=utf8;
create table t_s(tid int,sid int);
insert into student values(1,'小明'),(2,'小红'),(3,'小绿');
insert into teacher values(1,'苍老师'),(2,'传奇哥');
insert into t_s values(1,1),(1,2),(1,3),(2,1);
关联查询
关联查询: 查询存在关联关系的多张表的查询方式
三种关联查询的方式:1. 等值连接 2. 内连接 3. 外连接
- 等值连接
格式: select * from A,B where 关联关系 and 其它条件
-
查询工资高于2000的员工的姓名和对应的部门名
select e.ename,d.dname from emp e,dept d where e.deptno=d.deptno and e.sal>2000;
-
内连接
格式: select * from A join B on 关联关系 where 其它条件
- 查询工资高于2000的员工的姓名和对应的部门名
-
select e.ename,d.* from
emp e join dept d on e.deptno=d.deptno
where e.sal>2000;
- 等值连接和内连接查询到的数据是一样的,都是两张表的交集数据
- 查询每个球队名和对应的球员名
select t.name,p.name
from team t join player p
on p.tid=t.id;
- 查询1号部门工资低于3000的员工姓名,工资,部门地址
select e.ename,e.sal,d.loc
from emp e join dept d on e.deptno=d.deptno
where e.deptno=1 and e.sal<3000;
-
外连接:
等值连接和内连接查询的是两张表的交集数据,而外连接查询的是一张表的全部数据和另外一张表的交集数据
格式: select * from A left/right join B on 关联关系 where 条件; insert into emp (empno,ename,sal) values(100,'灭霸',10000);
- 查询所有员工姓名和对应的部门名
select e.ename,d.dname
from emp e left join dept d
on e.deptno=d.deptno;
-
查询所有部门名称,地址和对应的员工姓名,工资
select d.dname,d.loc,e.ename,e.sal from emp e right join dept d on e.deptno=d.deptno;
关联查询总结:
如果查询的是多张表的数据,则使用关联查询, 查询的是两张表的交集数据使用等值连接或内连接(推荐),如果查询的是一张表的全部和另外一张表的交集数据则使用外连接.
各个关键字的顺序
select * from A join B on 关联关系 where普通字段条件 group by 字段名 having聚合函数条件 order by 字段名 limit 跳过条数,请求条数;
程序员初成长路线:(很全面的学习视频,网址…点击查看)https://blog.csdn.net/qq_43541242/article/details/107165068