一、Windows连接数据库
二、MySQL数据库练习
-- 创建数据库 create database mydb2 charset utf8; -- 使用数据库 use mydb2; -- 创建课程表 CREATE TABLE course ( cs_id int(11) NOT NULL COMMENT '课程编号', cs_name varchar(50) NOT NULL COMMENT '课程名称', cs_credit tinyint(255) unsigned DEFAULT NULL COMMENT '课程学分', cs_type char(12) DEFAULT NULL COMMENT '课程类别', cs_depart char(6) DEFAULT NULL COMMENT '院系名称', PRIMARY KEY (cs_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- 插入数据 insert into course (cs_id,cs_name,cs_credit,cs_type,cs_depart) values (5200313,'数据库原理及应用',4,'核心专业','信工'), (5203314,'计算机导论',4,'通识教育','信工'), (5219314,'数据结构',5,'专业核心','信工'), (5223013,'大学物理',4,'专业基础','信工'), (5227614,'毕业实习',4,'集中实践','信工'), (5230912,'云计算',2,'共同选修','信工'), (5236212,'机器学习',2,'共同选修','信工'), (5237514,'c语言',4,'专业基础','信工'), (5245112,'区块链',2,'任意选修','信工'), (7200422,'知识产权法',2,'任意选修','文法'), (20201833,'概率论',3,'专业基础','基础'), (20202336,'高等数学',6,'专业基础','基础'), (29299131,'劳动教育',1,'集中实践','学务'); -- 创建 学生表 CREATE TABLE student ( stu_id bigint(11) unsigned NOT NULL COMMENT '学号', stu_name char(12) NOT NULL COMMENT '姓名', stu_sex enum('男','女') DEFAULT NULL COMMENT '性别', stu_age tinyint(255) unsigned DEFAULT NULL COMMENT '年龄', stu_major char(9) DEFAULT NULL COMMENT '专业', stu_college char(12) DEFAULT NULL COMMENT '学院', PRIMARY KEY (stu_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- 插入数据 insert into student (stu_id,stu_name,stu_sex,stu_age,stu_major,stu_college) values (201804550101,'郭奎','男',22,'计科','信工学院'), (201804550102,'吕宇航','男',18,'计科','信工学院'), (201804550103,'张豪辉','女',19,'计科','信工学院'), (201804550107,'丁志杰','男',17,'金融学','金贸学院'), (201804550109,'范伟','男',19,'金融学','金贸学院'), (201804550116,'张依婷','女',17,'大数据','信工学院'), (201804550120,'张维','男',19,'计科','信工学院'), (201804550121,'朱柳阳','女',20,'计科','信工学院'), (201804550144,'谭兵炎','男',20,'大数据','信工学院'), (201804550153,'杨志强','男',17,'大数据','信工学院'); -- 1.查询全部课程的信息。 SELECT * from course; -- 2.查询信工学院开设的课程名、课程号及学分。 select cs_name as "课程名称" , cs_id as "课程号" , cs_credit as "学分" from course where cs_depart = "信工"; -- 3.查询学分超过3学分的课程代码、课程名和开课单位。 select cs_id as "课程代码" , cs_name as "课程名称" , cs_depart as "开课单位" from course where cs_credit > 3; -- 4.查询计科专业和大数据专业的学生信息。 select * from student where stu_major = "计科" || stu_major = "大数据"; -- 5.查询不是信工学院的学生姓名和学号。 select * from student where stu_college != "信工学院"; -- 6.查询年龄是17,18,19的学生姓名和专业。 select stu_name as "姓名" , stu_major as "专业" from student where stu_age in(17,18,19); -- 7.查询学分在2到4之间课程的信息。 select * from course where cs_credit > 2 and cs_credit < 4; -- 8.查询课程名称中带“数据”的课程名、课程号及开课单位。 select cs_name as "课程名", cs_id as "课程号", cs_depart as "开课单位" from course where cs_name like "%数据%"; -- 9.查询信工学院的的专业有哪些。 select distinct stu_major as "专业" from student where stu_college ="信工学院"; -- 10.查询年龄为空的学生信息。 select * from student where stu_age is null; -- 11.查询不是信工学院开设的集中实践课的开课单位和课程名称。 select cs_depart as "开课单位", cs_name as "课程名称" from course where cs_depart != "信工" && cs_type = "集中实践"; -- 12.查询信工学院开设的课程的类型有哪些。 select distinct cs_type as "类型" from course where cs_depart = "信工"; -- 13.查询学生所在的专业个数。 select count(distinct stu_major) as "数量" From student; -- 14.查询信工学院开设的课程的平均学分。 select avg(cs_credit) from course where cs_depart = "信工"; -- 15.查询学生的信息,查询结果按姓名升序排序。 select * from student order by stu_name asc; -- 16.查询 每个专业的学生的最大年龄、最小年龄和平均年龄,查询结果按平均年龄降序排列。 select stu_major as "专业" ,max(stu_age) as "最大年龄" , min(stu_age) as "最小年龄" , avg(stu_age) as "平均年龄" from student group by stu_major ORDER BY avg(stu_age) desc; -- 17.查询每个开课单位开设的课程门数的数量,直询结果按课程门数升序排列。 select cs_depart as "开课单位" , count(cs_name) as "开课数量" from course group by cs_depart order by count(cs_name); -- 18.查询单位开课门数少于2门的开课单位和课程名称。 select cs_name as "课程名称" , cs_depart as "开课单位" from course where cs_depart in( select cs_depart as "开课单位" from course GROUP BY cs_depart having count(cs_name) <2 );
创建student表,向student表中插入数据
原创文章,作者:6024010,如若转载,请注明出处:https://blog.ytso.com/274901.html