头歌MySQL数据库实训答案 有目录

头歌MySQL数据库答案

特别感谢黄副班、小青提供代码,有问题联系公众号【学思则安】留言更正

其他作业链接

头歌java实训答案集

数据库1-MySQL数据定义与操作实战

MySQL数据库 - 初识MySQL

数据库部分一条一条的写,可鼠标手动粘贴,除特定命令外未分大小写。
第1关:创建数据库

mysql -uroot -p123123 -h127.0.0.1


create database MyDb;

第2关创建表

mysql -uroot -p123123 -h127.0.0.1


create database TestDb;


use TestDb;


create table t_emp (id int,
name varchar(32),
deptId int,
salary float);

第3关:使用主键约束

mysql -uroot -p123123 -h127.0.0.1


create database MyDb;


use MyDb;


create table t_user1(
userId INT PRIMARY KEY,
name VARCHAR(32),
password VARCHAR(11),
phone VARCHAR(11),
email VARCHAR(32));


create table t_user2(
name VARCHAR(32),
phone VARCHAR(11),
email VARCHAR(32),
PRIMARY KEY(name,phone));

第4关:外键约束

mysql -uroot -p123123 -h127.0.0.1


create database MyDb;


use MyDb;



CREATE TABLE t_class
(
    id INT  PRIMARY KEY,
    name VARCHAR(22) 
);)


CREATE TABLE t_student
(
    id INT  PRIMARY KEY,
    name VARCHAR(22) ,
    classId int,
    CONSTRAINT fk_stu_class1 FOREIGN KEY(classId) REFERENCES t_class(id)
);

第5关:添加常用约束

mysql -uroot -p123123 -h127.0.0.1


CREATE DATABASE MyDb;



USE MyDb;


CREATE TABLE t_user
(
    id INT  PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(32) NOT NULL UNIQUE,
    sex VARCHAR(4) DEFAULT '男'
)DEFAULT CHARSET=utf8;

MySQL数据库 - 数据库和表的基本操作(一)

第1关:查看表结构与修改表名

USE Company;

########## Begin ##########

########## modify the table name ##########
ALTER TABLE tb_emp RENAME jd_emp;


########## show tables in this database ##########
show tables;


########## describe the table ##########
describe jd_emp;


########## End ##########

第2关:修改字段名与字段数据类型

USE Company;

#请在此处添加实现代码
########## Begin ##########

########## change the column name ##########
ALTER TABLE tb_emp change Id prod_id int(11);


########## change the data type of column ##########
ALTER TABLE tb_emp MODIFY Name varchar(30);


########## End ##########

DESCRIBE tb_emp;

第3关:添加与删除字段

USE Company;

#请在此处添加实现代码
########## Begin ##########

########## add the column ##########
ALTER TABLE tb_emp ADD Country varchar(20) AFTER Name; 
########## delete the column ##########
ALTER TABLE tb_emp DROP Salary;


########## End ##########

DESCRIBE tb_emp;

第4关:修改字段的排列位置

USE Company;

#请在此处添加实现代码
########## Begin ##########

########## modify the column to top ##########
ALTER TABLE tb_emp MODIFY Name varchar(25) FIRST;


########## modify the column to the rear of another column ##########
ALTER TABLE tb_emp MODIFY DeptId int(11) AFTER Salary;


########## End ##########

DESCRIBE tb_emp;

第5关:删除表的外键约束

USE Company;

#请在此处添加实现代码
########## Begin ##########

########## delete the foreign key ##########
ALTER TABLE tb_emp DROP FOREIGN KEY emp_dept;


########## End ##########
SHOW CREATE TABLE tb_emp G;

MySQL数据库 - 数据库和表的基本操作(二)

第1关:插入数据

USE Company;

#请在此处添加实现代码
########## Begin ##########

########## bundle insert the value #########
INSERT INTO tb_emp(Id,Name,DeptId,Salary) 
VALUES (1,"Nancy",301,2300.00),
(2,"Tod",303,5600.00),(3,"Carly",301,3200.00);

########## End ##########
SELECT * FROM tb_emp;
########## End ##########

第2关:更新数据

USE Company;

#请在此处添加实现代码
########## Begin ##########

########## update the value ##########
UPDATE tb_emp
SET Name="Tracy",DeptId=302,Salary=4300.00
WHERE id=3;


########## End ##########

SELECT * FROM tb_emp;

########## End ##########

DESCRIBE tb_emp;

第3关:删除数据

USE Company;

#请在此处添加实现代码
########## Begin ##########

########## delete the value ##########
DELETE FROM tb_emp
WHERE Salary>3000;


########## End ##########

SELECT * FROM tb_emp;

########## End ##########

DESCRIBE tb_emp;

MySQL数据库 - 单表查询(一)

第1关:基本查询语句

USE Company;

#请在此处添加实现代码
########## Begin ##########

########## retrieving the Name and Salary ##########
select Name,Salary from tb_emp;

########## retrieving all the table ##########
select * from tb_emp;

########## End ##########

第2关:带 IN 关键字的查询

USE Company;

#请在此处添加实现代码
########## Begin ##########

########## retrieving the Name and Salary with IN statement ##########
SELECT Name,Salary FROM tb_emp WHERE Id NOT IN (1);


########## End ##########

第3关:带 BETWEEN AND 的范围查询

USE Company;

#请在此处添加实现代码
########## Begin ##########

########## retrieving the Name and Salary with BETWEEN AND statement ##########
SELECT Name,Salary FROM tb_emp 
WHERE Salary BETWEEN 3000 AND 5000;


########## End ##########

MySQL数据库 - 单表查询(二)

第1关:带 LIKE 的字符匹配查询

USE Company;

######### Begin #########
SELECT Name,Salary FROM tb_emp WHERE Name LIKE "C%";

######### End #########

第2关:查询空值与去除重复结果

USE Company;

######### Begin #########
SELECT * FROM tb_emp WHERE DeptId IS NULL;

######### End #########

######### Begin #########
SELECT DISTINCT Name FROM tb_emp;

######### End #########

第3关:带 AND 与 OR 的多条件查询

USE Company;

######### Begin #########
SELECT * FROM tb_emp WHERE DeptId=301 AND Salary > 3000;

######### End #########

######### Begin #########
SELECT * FROM tb_emp WHERE DeptId=301 OR DeptId=303;

######### End #########

MySQL数据库 - 单表查询(三)

第1关:对查询结果进行排序

USE School;

#请在此处添加实现代码
########## Begin ##########

########## 查询1班同学的所有信息以成绩降序的方式显示结果 ##########
select * from tb_score where class_id = 1 order by score desc;

########## End ##########

第2关:分组查询

USE School;

#请在此处添加实现代码
########## Begin ##########

########## 对班级名称进行分组查询 ##########
SELECT * FROM tb_class GROUP BY class_id;

########## End ##########

第3关:使用 LIMIT 限制查询结果的数量

USE School;

#请在此处添加实现代码
########## Begin ##########

########## 查询班级中第2名到第5名的学生信息 ##########
SELECT * FROM tb_score order by score desc LIMIT 1,4;

########## End ##########

MySQL数据库 - 连接查询

第1关:内连接查询

USE School;

########## 查询数据表中学生姓名和对应的班级 ##########
#请在此处添加实现代码
########## Begin ##########
select tb_student.name as studentName,tb_class.name as className from tb_student join tb_class on tb_class.id = tb_student.class_id; 



########## End ##########

第2关:外连接查询

USE School;

########## 使用左外连接查询所有学生姓名和对应的班级 ##########

#请在此处添加实现代码
########## Begin ##########
select tb_student.name as studentName,tb_class.name as className
from tb_class right join tb_student on 
tb_class.id=tb_student.class_id;



########## End ##########

########## 使用右外连接查询所有学生姓名和对应的班级 ##########
select tb_student.name as studentName,tb_class.name as className
from tb_class left join tb_student 
on tb_class.id=tb_student.class_id;
#请在此处添加实现代码
########## Begin ##########




########## End ##########

第3关:复合条件连接查询

USE School;

########## 查询所有班级里分数在90分以上的学生的姓名和学生的成绩以及学生所在的班级 ##########
#请在此处添加实现代码
########## Begin ##########
select s1.name as studentName,score,
s2.name as className from tb_student as s1,
tb_class as s2 where s1.class_id=s2.id and
s1.score>90 order by score desc;
########## End ##########

MySQL数据库 - 子查询

第1关:带比较运算符的子查询

USE Company;

#请在此处添加实现代码
########## Begin ##########
#1.查询大于所有平均年龄的员工姓名与年龄
select name,age from tb_emp where age>(select avg(age) from tb_emp);


########## End ##########

第2关:关键字子查询

USE Company;
#请在此处添加实现代码
########## Begin ##########

#1.使用 ALL 关键字进行查询
SELECT position,salary FROM tb_salary WHERE salary > 
ANY(SELECT max(salary) FROM tb_salary where position="java");
#2.使用 ANY 关键字进行查询
SELECT position,salary FROM tb_salary WHERE salary > 
ANY(SELECT min(salary) from tb_salary where position="java");
#3.使用 IN 关键字进行查询
select position,salary from tb_salary where position in("java");
########## End ##########

MySQL数据库 - 复杂查询(一)

第1关:交换工资

#请在此添加实现代码
########## Begin ##########
UPDATE tb_Salary
SET
sex = CASE sex WHEN "m"  THEN "f"
ELSE "m"
END;

########## End ##########

第2关:换座位

#请在此添加实现代码
########## Begin ##########
SELECT if(Id%2=0,Id-1,if(Id=5,Id,Id+1)) AS id,name
FROM tb_Seat ORDER BY Id;
########## End ##########

第3关:分数排名

#请在此添加实现代码
########## Begin ##########
select Score,(select count(distinct score) from score where score >=s.score) as Rank
from score as s order by Score desc;
select Score,(select count(*) from score as s2 where s2.score >s1.score)+1 as Rank
from score as s1 order by Rank;

########## End ##########

第4关:体育馆的人流量

#请在此添加实现代码
########## Begin ##########
select distinct a.* from gymnasium a,gymnasium b,gymnasium c
where a.visitors_flow>=100 and b.visitors_flow>=100
and c.visitors_flow>=100
and(
    (a.id = b.id-1 and b.id = c.id - 1)or
    (a.id = b.id-1 and a.id = c.id + 1)or
    (a.id = b.id+1 and b.id = c.id + 1)
)
order by a.id;

########## End ##########

第5关:统计总成绩

#请在此添加实现代码
########## Begin ##########
select t1.classname,t1.chinese,t2.maths
from(select c.classname classname,sum(s.chinese)
chinese from tb_class c,tb_score s where c.stuname=
s.name and s.chinese>=60 group by c.classname)t1,
(select c.classname classname,sum(s.maths)maths from tb_class c,tb_score s
where c.stuname=s.name and s.maths>=60 group by c.classname)t2
where t1.classname=t2.classname;

########## End ##########

MySQL数据库 - 复杂查询(二)

第1关:查询学生平均分

#请在此添加实现代码
########## Begin ##########
select b.s_id,b.s_name,ROUND(AVG(a.s_score),2)as avg_score from student b 
inner join score a on b.s_id = a.s_id
GROUP BY b.s_id,b.s_name HAVING avg_score <60
union 
select a.s_id,a.s_name,0 as avg_score from student a 
where a.s_id not in (select distinct s_id from score);

########## End ##########

第2关:查询修课相同学生信息

#请在此添加实现代码
########## Begin ##########
create view temp as(select s_id,group_concat(c_id)as c from score group by s_id);
select * from student where s_id in(select s_id from temp where c=(select c from temp where s_id="01")and s_id<>"01");
########## End ##########

第3关:查询各科成绩并排序

#请在此添加实现代码
########## Begin ##########
select a.*,count(b.s_score)+1 rank from score a left join score b 
on a.c_id = b.c_id and a.s_score <b.s_score
group by a.c_id,a.s_id
order by a.c_id,count(b.s_score);

########## End ##########

第4关:查询张老师课程成绩最高的学生信息

#请在此添加实现代码
########## Begin ##########
select a.*,b.s_score,b.c_id,c.c_name from student a 
INNER JOIN score b ON a.s_id = b.s_id
INNER JOIN course c ON b.c_id = c.c_id
where b.c_id = (select c_id from course c,teacher d where c.t_id=d.t_id and d.t_name="张三")
and b.s_score in (select MAX(s_score)from score where c_id="02");

########## End ##########

第5关:查询两门课程不及格同学信息

#请在此添加实现代码
########## Begin ##########
select a.s_id,a.s_name,ROUND(AVG(b.s_score))
avg_score from student a 
inner join score b on a.s_id = b.s_id
where a.s_id in(
    select s_id from score where s_score<60 GROUP BY s_id having count(*)>=2
)
GROUP BY a.s_id,a.s_name;

########## End ##########

MySQL数据库 - 使用聚合函数查询

第1关:COUNT( )函数

USE School;

#请在此处添加实现代码
########## Begin ##########

########## 查询该表中一共有多少条数据 ##########
select count(*) from tb_class;

########## 查询此表中367班有多少位学生 ##########
select classid,count(*) from tb_class where classid=367;

########## End ##########

第2关:SUM( )函数

USE School;

#请在此处添加实现代码
########## Begin ##########

########## 查询所有学生总分数 ##########
select sum(score) from tb_class;

########## 查询学生语文科目的总分数 ##########
select course,sum(score) from tb_class where course="语文";


########## End ##########

第3关:AVG( )函数

USE School;

#请在此处添加实现代码
########## Begin ##########

########## 查询学生语文科目的平均分数 ##########
select course,avg(score)from tb_class where course="语文";


########## 查询学生英语科目的平均分数 ##########
select course,avg(score) from tb_class where course="英语";


########## End ##########

第4关:MAX( )函数

USE School;

#请在此处添加实现代码
########## Begin ##########

########## 查询语文课程中的最高分数 ##########
select course,max(score) from tb_class where course="语文";


########## 查询英语课程中的最高分数 ##########
select course,max(score) from tb_class where course="英语";


########## End ##########

第5关:MIN( )函数

USE School;

#请在此处添加实现代码
########## Begin ##########

########## 查询语文课程中的最低分数 ##########
select course,min(score) from tb_class where course="语文";


########## 查询英语课程中的最低分数 ##########
select course,min(score) from tb_class where course="英语";


########## End ##########

MySQL数据库 - 其他函数的使用

第1关:字符函数

#请在此添加实现代码
########## Begin ##########
select CONCAT(UPPER(SUBSTR(Name,1,1)),LOWER(SUBSTR(Name,2,LENGTH(Name)))) as Name from employee;
########## End ##########

第2关:数学函数

#请在此添加实现代码
########## Begin ##########
update Score set s_score=TRUNCATE(s_score-(round(sqrt((power(4,4)-power(3,3))/power(2,2)),2)),2);
########## End ##########

第3关:日期时间函数和流程控制类函数

#请在此添加实现代码
########## Begin ##########

##########  查询学生出生年份及年龄 ###
  • 1
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、验目的 1.掌握SQL Server 2005的安装。 2.掌握SQL Server Management Studio的启动和使用。 3.掌握SQL Server 2005服务器的配置和注册。 4.掌握SQL Server 2005查询的基本使用。 5.掌握应用SQL Server Management Studio创建数据库的方法。 6.掌握应用SQL Server Management Studio修改和查看数据库的方法。 7.掌握应用SQL Server Management Studio删除数据库的方法。 8.掌握应用Transact-SQL语句创建数据库的方法。 9.掌握应用Transact-SQL修改和查看数据库的方法。 10.掌握通过Transact-SQL删除数据库的方法。 11.掌握SQL Server 2005数据库和操作系统物理文件的关系。 12.掌握数据库的分离和附加方法。 二、验内容 1.完成SQL Server 2005开发版的安装。 提示:若计算机系统中已经安装有SQL Server 2005系统,则在安装时需要选择安装命名例。安装过程中身份验证模式选择“混合模式”并设置sa账户的密码。 2.利用SQL Server配置管理器启动、停止SQL Server服务(包括默认例和命名例),配置SQL Server服务为自动启动。 3.利用SQL Server配置管理器配置进行SQL Server 2005网络配置,启用默认例和命名例的TCP/IP协议。 4.利用SQL Server外围配置器配置数据库引擎的服务及远程连接,设置为“本地连接和远程连接”,选择“同时使用TCP/IP和named pipes”。 5.利用SQL Server Management Studio注册安装的命名例。 6.利用SQL Server Management Studio注册远程服务器。 提示:注册远程服务器时需要使用混合验证模式,利用sa账户和密码登录远程服务器。 7.启动SQL Server Management Studio,连接到服务器。新建一个查询,在其中输入如下代码: DECLARE @position int, @string char(5) SET @position = 1 SET @string = 'China' WHILE @position <= DATALENGTH(@string) BEGIN SELECT SUBSTRING(@string, @position, 1) 字符, ASCII(SUBSTRING(@string, @position, 1)) ASCII码 SET @position = @position + 1 END
由于MySQL数据库题目不同,答案也会有所不同。以下是一些可能的MySQL数据库题目以及相应的答案: 1. 创建一个名为“students”的数据库,其中包含一个名为“grades”的表格,该表格包含以下列:学生ID(student_id),姓名(name),年龄(age)和成绩(grade)。 答案: CREATE DATABASE students; USE students; CREATE TABLE grades ( student_id INT PRIMARY KEY, name VARCHAR(50), age INT, grade INT ); 2. 向“grades”表格中插入以下数据: 学生ID 姓名 年龄 成绩 1 小明 18 90 2 小红 19 85 3 小刚 20 92 答案: INSERT INTO grades (student_id, name, age, grade) VALUES (1, '小明', 18, 90), (2, '小红', 19, 85), (3, '小刚', 20, 92); 3. 从“grades”表格中选择所有学生的姓名和成绩,并按成绩降序排列。 答案: SELECT name, grade FROM grades ORDER BY grade DESC; 4. 更新学号为2的学生的成绩为88分。 答案: UPDATE grades SET grade = 88 WHERE student_id = 2; 5. 从“grades”表格中删除学号为3的学生记录。 答案: DELETE FROM grades WHERE student_id = 3; 6. 在“grades”表格中添加一个名为“性别”的列,其数据类型为“VARCHAR(10)”。 答案: ALTER TABLE grades ADD COLUMN gender VARCHAR(10); 7. 将“grades”表格中所有成绩低于80分的学生的性别设置为“不及格”。 答案: UPDATE grades SET gender = '不及格' WHERE grade < 80; 8. 创建一个名为“courses”的表格,其中包含以下列:课程ID(course_id),课程名称(name)和学分(credit)。 答案: CREATE TABLE courses ( course_id INT PRIMARY KEY, name VARCHAR(50), credit INT ); 9. 向“courses”表格中插入以下数据: 课程ID 课程名称 学分 1 数据库 3 2 算法设计 4 3 操作系统 3 答案: INSERT INTO courses (course_id, name, credit) VALUES (1, '数据库', 3), (2, '算法设计', 4), (3, '操作系统', 3); 10. 创建一个名为“score”的表格,其中包含以下列:学生ID(student_id),课程ID(course_id)和分数(score)。 答案: CREATE TABLE score ( student_id INT, course_id INT, score INT, PRIMARY KEY (student_id, course_id), FOREIGN KEY (student_id) REFERENCES grades(student_id), FOREIGN KEY (course_id) REFERENCES courses(course_id) ); 11. 向“score”表格中插入以下数据: 学生ID 课程ID 分数 1 1 88 1 2 92 2 1 85 2 3 90 3 2 80 答案: INSERT INTO score (student_id, course_id, score) VALUES (1, 1, 88), (1, 2, 92), (2, 1, 85), (2, 3, 90), (3, 2, 80); 12. 从“score”表格中选择所有学生的姓名、课程名称和分数,并按学生ID升序排列。 答案: SELECT grades.name, courses.name, score.score FROM score INNER JOIN grades ON score.student_id = grades.student_id INNER JOIN courses ON score.course_id = courses.course_id ORDER BY grades.student_id ASC;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值