学习资料:
https://www.bilibili.com/video/BV1NJ411J79W?p=16
学习数据准备-执行下方sql语句或者下载sql文件使用数据库可视化文件导入: 密码: iv5w:
链接: https://pan.baidu.com/s/1oNJqc9BNY90oy-QishCBPg 密码: iv5w
create database if not exists `school`;
-- 创建一个school数据库
use `school`;-- 创建学生表
drop table if exists `student`;
create table `student`(
`studentno` int(4) not null comment \'学号\',
`loginpwd` varchar(20) default null,
`studentname` varchar(20) default null comment \'学生姓名\',
`sex` tinyint(1) default null comment \'性别,0或1\',
`gradeid` int(11) default null comment \'年级编号\',
`phone` varchar(50) not null comment \'联系电话,允许为空\',
`address` varchar(255) not null comment \'地址,允许为空\',
`borndate` datetime default null comment \'出生时间\',
`email` varchar (50) not null comment \'邮箱账号允许为空\',
`identitycard` varchar(18) default null comment \'身份证号\',
primary key (`studentno`),
unique key `identitycard`(`identitycard`),
key `email` (`email`)
)engine=myisam default charset=utf8;
-- 创建年级表
drop table if exists `grade`;
create table `grade`(
`gradeid` int(11) not null auto_increment comment \'年级编号\',
`gradename` varchar(50) not null comment \'年级名称\',
primary key (`gradeid`)
) engine=innodb auto_increment = 6 default charset = utf8;
-- 创建科目表
drop table if exists `subject`;
create table `subject`(
`subjectno`int(11) not null auto_increment comment \'课程编号\',
`subjectname` varchar(50) default null comment \'课程名称\',
`classhour` int(4) default null comment \'学时\',
`gradeid` int(4) default null comment \'年级编号\',
primary key (`subjectno`)
)engine = innodb auto_increment = 19 default charset = utf8;
-- 创建成绩表
drop table if exists `result`;
create table `result`(
`studentno` int(4) not null comment \'学号\',
`subjectno` int(4) not null comment \'课程编号\',
`examdate` datetime not null comment \'考试日期\',
`studentresult` int (4) not null comment \'考试成绩\',
key `subjectno` (`subjectno`)
)engine = innodb default charset = utf8;
-- 插入学生数据 其余自行添加 这里只添加了2行
insert into `student` (`studentno`,`loginpwd`,`studentname`,`sex`,`gradeid`,`phone`,`address`,`borndate`,`email`,`identitycard`)
values
(1000,\'123456\',\'张伟\',0,2,\'13800001234\',\'北京朝阳\',\'1980-1-1\',\'text123@qq.com\',\'123456198001011234\'),
(1001,\'123456\',\'赵强\',1,3,\'13800002222\',\'广东深圳\',\'1990-1-1\',\'text111@qq.com\',\'123456199001011233\');
-- 插入成绩数据 这里仅插入了一组,其余自行添加
insert into `result`(`studentno`,`subjectno`,`examdate`,`studentresult`)
values
(1000,1,\'2013-11-11 16:00:00\',85),
(1000,2,\'2013-11-12 16:00:00\',70),
(1000,3,\'2013-11-11 09:00:00\',68),
(1000,4,\'2013-11-13 16:00:00\',98),
(1000,5,\'2013-11-14 16:00:00\',58);
-- 插入年级数据
insert into `grade` (`gradeid`,`gradename`) values(1,\'大一\'),(2,\'大二\'),(3,\'大三\'),(4,\'大四\'),(5,\'预科班\');
-- 插入科目数据
insert into `subject`(`subjectno`,`subjectname`,`classhour`,`gradeid`)values
(1,\'高等数学-1\',110,1),
(2,\'高等数学-2\',110,2),
(3,\'高等数学-3\',100,3),
(4,\'高等数学-4\',130,4),
(5,\'C语言-1\',110,1),
(6,\'C语言-2\',110,2),
(7,\'C语言-3\',100,3),
(8,\'C语言-4\',130,4),
(9,\'Java程序设计-1\',110,1),
(10,\'Java程序设计-2\',110,2),
(11,\'Java程序设计-3\',100,3),
(12,\'Java程序设计-4\',130,4),
(13,\'数据库结构-1\',110,1),
(14,\'数据库结构-2\',110,2),
(15,\'数据库结构-3\',100,3),
(16,\'数据库结构-4\',130,4),
(17,\'C#基础\',130,1);
常用查询语句:
-- DQL
-- 1. 查询全部学生
select * from student;
-- 2. 查询全部学生的姓名,电话
SELECT `studentname`, `phone` FROM student;
-- 3. 别名的使用
SELECT s.studentname FROM student AS s;
-- 4. concat 函数
SELECT CONCAT(\'姓名:\',`studentname`) FROM student;
-- 5. 去重查询
SELECT DISTINCT `studentNo` FROM result;
-- 6. 查询系统版本
SELECT VERSION();
-- 7. 计算表达式
SELECT 100%3 AS `表达式结果`;
-- 8. 查询自增长的步长
SELECT @@auto_increment_increment;
-- 9. 学员考试成绩 加分后结果
SELECT `studentNo`,`studentResult` + 1 AS \'➕分后\' FROM result;
-- 10. 查询成绩在 95~100之间的数据
SELECT * FROM result WHERE `studentResult` BETWEEN 95 AND 100;
SELECT * FROM result WHERE `studentResult`>=95 AND `studentResult`<=100;
SELECT * FROM result WHERE `studentResult` IN (95,96,97,98,99,100);
-- 11. 模糊查询,姓张的同学
SELECT * FROM student WHERE studentname LIKE \'张%\';
-- 12. 查看地址为空的
SELECT * FROM student WHERE address IS NULL;
-- 13. 分页查询 , 从第3条数据开始,查3条数据出来
SELECT * FROM result LIMIT 2,3;
-- 14. 排序查询 DESC 倒序
SELECT * FROM result ORDER BY `studentResult` DESC;
-- 15. 分组
SELECT * FROM result GROUP BY `studentNo`;
-- 16. 连表查询-内连接 : 如果表中至少有一个匹配,就返回行. on 连接表 , where 条件
SELECT s.studentNo, studentName, `subjectNO`,StudentResult FROM student AS s
join result as r where s.studentno = r.studentno;
-- 17. 右链接 : 会从右表中返回所有的值,即使左表中没有匹配 student为右表
SELECT s.studentNo, studentName, `subjectNO`,StudentResult FROM student AS s
RIGHT JOIN result as r ON s.studentno = r.studentno;
-- 18. 左链接 :会从左表中返回所有的值,即使右表中没有匹配。 student 为左表
SELECT s.studentNo, studentName, `subjectNO`,StudentResult FROM student AS s
LEFT JOIN result as r ON s.studentno = r.studentno;
-- 19. 自连接 拆成两张表
SELECT a.categoryName AS \'一级分类\', b.categoryName AS \'二级分类\' FROM `category` AS a, `category` AS b
WHERE a.categoryName = b.categoryName;
-- 20. 聚合函数 :max min sum avg
SELECT MAX(`studentResult`) FROM result;
-- 21. 子查询
SELECT * FROM result WHERE studentNo IN (
SELECT `subjectNo` FROM `subject`
);
-- 常用函数;获取当前时间
SELECT NOW();