1.DML data manage language数据库管理语言
外键:外键是什么?就是对其进行表与表之间的联系,就是使用的键进行关联!
方法一:我们在数据库里面就对其进行表与表之间的连接【这种是不建议的,我不太喜欢就是将数据里面弄得很复杂,数据库就是对其存储数据就行,这种物理外键不建议,我们可以执行使用软件程序方面对其进行操作就行,本质上面就是一个筛选的过程!
可以参考一下这个文章,解释了为什么不能用外键】
create table if not exists grade (id int not null auto_increment comment '学生id',name varchar(10) not null default '匿名' comment '学生名字',primary key (id)
);
alter table student addconstraint fk_student_grade foreign key (id) references grade (id);
-- alter table 表 add constraint 外键名 foreign key (字段名) references 主表名(主键字段名);
方法二:软件层面(后面我才开始介绍!应用层面的时候会进行介绍!)
下面就开始介绍数据库的操作!
增:(为了加上记忆,就是你要添加(insert into),你先需要先说明你哪个表(student),表里面的哪个字段(字段1,字段2....),然后在对字段进行添加(值1,值2...))
-- insert into 表(字段1,字段2...) values (值1,值2...);
-- 主键是自增的不需要对其进行定义
-- 字段需要和后面的值一一对应
insert into student (name, password, sex, birthday, address, email)
values ('张三', '123456', '男', '1999-01-01', '北京', 'zhangsan@qq.com');
insert into student (name, password, sex, birthday, address, email)
values ('李四', '15951', '男', '1999-01-01', '四川', 'lisi@qq.com');
删:(首先我们需要对其进行要进行删除,要找到对应的表(delete from student),当然你不能不加条件对其删除,那最终导致的结果就会全部进行删除(where id = 3))
-- delete from 表名 where 条件;
delete from student where id = 3;
改(你要对其进行修改,首先你需要设置条件然后对其进行寻找(where id= 2),然后选择表对其进行修改 (update student),选择字段进行修改 set 字段 = ‘修改 ’ )
-- update 表名 set 字段1 = 值1,字段2 = 值2... where 条件;
update student set name = '李化' where id = 2;
update student set password = '123', sex = '女' where id = 3;
update student set name = '李化', password = '123' where id = 3 and sex = '女';
update student set name = '李化化', password = '123' where id = 3 or sex = '男'; -- 这里表示就是男或者id为3,都需要对其进行修改
!!!这里面稍微需要注意一点就是where里面是进行条件的,换句话说就是一些操作符
大于小于等于不等于(不说了噻!)
between..and..(范围)
and,or
2.DQL data quire language数据库查询语言
(查询毋庸置疑是最重要的,因为大部分我们的数据库是进行查询的)
2.1.最简单就是查询表里面的所有内容,对于数据库表达式列的处理!
-- 语法:select 字段 from 表名;
select * from student;
-- 可以起别名,便于好看
select sex as '性别' from student as s;
-- 使用函数
select concat('性别:', sex) as '性别' from student;
-- 进行去重
select distinct sex from student;
2.2where条件查询
2.2.1与或非
select name as '名字' from student where address = '北京'and sex = '男';
2.2.2模糊查询
还有什么 is null 或者is not null
运算符 | 语法 | 描述 |
like | a like b | 就相当于就是 ‘小红’ like ‘小红。。。。’ |
in | a in (b1,b2.。) | 就相当于就是‘a’ i在这里面 |
-- where条件进行查询
select name as '名字' from student where address = '北京'and sex = '男';
-- 模糊查询
-- like
-- %表示任意字符,_表示一个字符
-- 查看张姓的人
select name as '名字' from student where name like '张%';-- 查看张姓的第二个人
select name as '名字' from student where name like '张_';-- 查看中间一个字为‘佳’
select name as '名字' from student where name like '%佳%';-- in
-- 查询地址子在北京和上海的人
select name as '名字' from student where address in ('北京', '上海');
2.3.联表查询(join连接)
联表查询主要分为三个:
inner join:只要满足要求就进行匹配
select name,s.address,st.des
from student as s
inner join status as st
where s.address = st.address ;
right join:(其实就是必须包含右边的东西,然后再进行筛选)
select name,s.address,st.des
from student as s
right join status as st
on s.address = st.address ;
left join:(其实就是必须包含左边的东西,然后再进行筛选)
select name,s.address,st.des
from student as s
left join status as st
on s.address = st.address ;
小结一下:(我们做一个综合的案例)
首先我们创建四个表,学生,老师,课程,成绩,并对其进行数据填写!
-- ==================学生=========================
create table if not exists student
(s_no varchar(21) not null primary key comment '学号',s_name varchar(12) not null comment '学生姓名',s_age tinyint unsigned not null comment '学生年龄',s_sex enum ('1','0') default '1' comment '学生性别',s_birthday datetime comment '学生生日',class varchar(10) not null comment '学生班级'
);insert into student(s_no, s_name, s_age, s_sex, s_birthday, class)
values (1, '李洋', 18, '1', 19980422, '1'),(2, '温俊林', 19, '0', 19970422, '1'),(3, '周恒华', 20, '1', 19950422, '1'),(4, '张松涛', 22, '1', 19940422, '2'),(5, '李晖', 20, '0', 19960622, '2'),(6, '包政', 23, '1', 19930422, '2');-- ==================课程=========================
create table if not exists course
(cno varchar(21) not null primary key comment '课程号',cname varchar(10) not null comment '课程名称',tno varchar(10) not null comment '教师编号'
);insert into course(cno, cname, tno)
values ('1', 'linux', '001'),('2', 'linux', '002'),('3', 'python', '003');-- ==================分数=========================
create table if not exists score
(sno varchar(21) not null primary key comment '学号',cno varchar(21) not null comment '课程号',mark float(4, 1) not null comment '成绩'
);insert into score(sno, cno, mark)
values ('1', '1', '99.5'),('2', '1', '80.5'),('3', '1', '85.5'),('4', '1', '84.5'),('5', '1', '89.5'),('6', '1', '89.5');-- ==================老师=========================
create table if not exists teacher
(t_no varchar(21) not null primary key comment '教师编号',t_name varchar(10) not null comment '教师姓名',t_age tinyint unsigned not null comment '教师年龄',t_sex enum ('0','1') not null default '1' comment '教师性别',prof varchar(10) comment '教师职称',depart varchar(15) not null comment '教师部门'
);insert into teacher(t_no, t_name, t_age, t_sex, prof, depart)
values ('001', '曾导', 18, 1, '校长', 'linux'),('002', '徐导', 19, 1, '教学总监', 'linux'),('003', '李导', 20, 1, '讲师', 'python');
select的练习题:(注意我使用的不是那个一层一层进行筛选)
-- 查询student表中的所有记录的s_name、s_sex和class列
select student.s_name,student.s_sex,student.class from student;
-- 查询教师所有的单位即不重复的depart列
select distinct teacher.depart from teacher;
-- 查询score表中成绩在80到90之间的所有记录
select * from score where mark between 80 and 90;
-- 查询score表中成绩为85.5,89.5或80.5的记录
select * from score where mark in (85.5,89.5,80.5);
-- 查询student表中1班或性别为“女”的同学记录
select * from student where class='1' or s_sex='女';
-- 以cno升序、mark降序查询Score表的所有记录
select * from score order by cno asc,mark desc;
-- 查询”曾导“教师任课的学生成绩
select teacher.t_name,course.cname,student.s_name,score.mark
from teacher,course,student,score
where teacher.t_name='曾导' andteacher.t_no=course.tno andcourse.cno=score.cno andscore.sno=student.s_no;
-- 查询linux课程所有男生的成绩并且查出对应课程的教师名,职称,及所在部门。
-- 96ms运行时间
select teacher.t_name,teacher.prof,teacher.depart,score.mark
from teacher,course,student,score
where course.cname='linux' andstudent.s_sex='1' andteacher.t_no=course.tno andcourse.cno=score.cno andscore.sno=student.s_no;/*我要进行一层一层的查询1. 先查询linux课程的所有成绩2. 再查询linux课程的所有男生3. 最后查询linux课程的所有男生对应的成绩4. 最后查询linux课程的所有男生对应的成绩对应的教师名,职称,及所在部门*/-- 40ms运行时间select teacher.t_name,teacher.prof,teacher.depart,mark,s_namefrom teacherinner join course
on teacher.t_no = course.tno and depart = 'linux'
inner join scoreon score.cno = course.cnoinner join studenton student.s_no = score.sno and student.s_sex = '1';
【值得注意一点就是,我们在使用的时候一层一层要不直接进行判断要节约一半的时间!!!】
2.4自连接(换句话说就是自己对自己的这个表进行拆分成两个一模一样的表,然后通过拆成的两个进行比较!)
我们还是举一个例子:
创建shop表格,添加数据进行对比!
create table if not exists shop (id int not null auto_increment comment '商品id',name varchar(10) not null default '匿名' comment '商品名字',price float(10,2) not null default 0.00 comment '商品价格',primary key (id)
);
insert into shop (name,price) values ('华为手机',6999.00);
insert into shop (name,price) values ('小米手机',5000.00);
insert into shop (name,price) values ('荣耀手机',5999.00);
insert into shop (name,price) values ('华为平板',4999.00);
insert into shop (name,price) values ('小米平板',3999.00);
insert into shop (name,price) values ('荣耀平板',8999.00);
然后我想对其价格估算,我想知道比华为平板高的商品有哪些?
方法1:就是进行分步查询
select price from shop where name = '华为平板'; -- 先进行查询
select * from shop where price > 4999;
方法2.自连接
select a.*
from shop as a,shop as b
where b.name = '华为平板'and a.price > b.price;
3.排序和分页
排序:升序ASC,降序DESC
select a.*
from shop as a,shop as b
where b.name = '华为平板'and a.price > b.price
order by a.price desc;
分页: 1.环节数据库压力 2.还有就是美观!
select a.*
from shop as a,shop as b
where b.name = '华为平板'and a.price > b.price
order by a.price desc
limit 0, 2; -- 起始值,页面大小
-- limit规律就是limit (当前页-1)*页面大小,页面大小
4.MySQ函数(毕竟是一门语言,就有自己的api,自己的函数)
4.1常用函数(常见但是也不是很常用)
就是什么绝对值,向上取整,向下取整,产生随机数等等!
需要就进行查询一下!
4.2聚合函数(就是比如有时候需要对其表格里面的数据进行处理)
聚合函数的查询
函数名称 | 描述 |
count() | 计数 |
sum() | 求和 |
avg() | 平均值 |
max() | 最大值 |
min() | 最小值 |
-- =============count()===============
select count(shop.name)
from shop; -- 会忽略null值select count(*)
from shop; -- 不会忽略nullselect count(1)
from shop; -- 不会忽略null-- ====================sum()==============select sum(shop.price)
from shop;-- ====================avg()==============
select avg(shop.price)
from shop;-- ====================max()==============
select max(shop.price)
from shop;-- ====================min()==============
select min(shop.price)
from shop;
下面我们通过分组来进行查询
select shop.name,sum(shop.price) as sum_price,avg(shop.price) as avg_price,max(shop.price) as max_price,min(shop.price) as min_price
from shop
group by shop.name;
【小的知识点补充一下就是md5:messenge digest 信息摘要算法,是不可逆的一种加密算法,我们可以对于数据库里面的数据进行加密】
【但是就是我们网上不是有那种在线破解码,那些都是把一些常见的密码已经存好了,存放在一个字典里面,你去查询就可以看到了!】
insert into shop (name, price)
values (md5('华为汽车'), 699);
总结
select完整的语句:
-- select的查询语法完整语句
select 去重的列
要查询的字段
*** join 关联表名 on 关联条件
from 表名
where 指定结果需满足的条件,或者子查询
group by 指定结果按照哪几个字段进行分组
having 指定分组后,分组结果需满足的条件
order by 指定结果按照哪一个字段进行排序
limit 指定结果返回的条数