MYSQL database system final test questions and reference answers (2)

Final Exam Questions :

1. Create database Game
2. Data table operation
1. Create table players to record game player information:
player_id: player ID, primary key
player_name: player name, cannot be empty
age: age, must be greater than or equal to 18 years old
country: country, default value For 'China'
unique_player_id: player ID unique constraint
2. Create table games to record game information:
game_id: game ID, primary key
game_name: game name, cannot be empty
release_date: release date
genre: game type
unique_game_name: game name unique constraint
3. Create the table scores to record the player's game score:
score_id: score ID, primary key
player_id: player ID, foreign key reference players table player_id
game_id: game ID, foreign key reference games table game_id
score: score
fk_player: player ID foreign key constraint, Associate players table
fk_game: game ID foreign key constraint, associate games table
3. Data operation
 1. Insert data into table players:
66407b4765db40c3928f02aae2a4bf11.png
 2. Insert data into table games:
6243d5dda1384cf591fa1a1ba112c59a.png
 3. Insert data into table scores:
(1,1, 1, 85 ),
(2,2, 2, 92),
(3,3, 3, 78),
(4,4, 4, 89),
(5,5, 5, 95),
(6,6, 6, 80),
(7,7, 7, 87),
(8,8, 8, 90),
(9,1, 2, 85),
(10,2, 1, 92),
(11,3, 6, 78),
(12,3, 7, 89),
(13,3, 8, 95),
(14,1, 2, 80),
(15,4, 7, 87),
(16,6, 7, 90),
(17,3, 1, 95),
(18,1, 4, 80),
(19,4, 3, 87),
(20,6, 8, 90);
4. Complete the following query questions
1. Query score ID, player name, game name and score
2. Query the game name, game release date, and game type (without repeating information) of people with a score greater than 88 points
3. The player ID, player name, and age of players with a total score greater than 250 ,Country
4. Check the same games played by Ma Qi and Zhuge Liang
5. Calculate the total score of each player
6. Change Ma Chao's country, Wei Guo to Shu Guo
7. Query the names of players older than 25
8. Query the players who play King of Glory in Shu Kingdom
9. Query the total number of players playing each game
10. Query the name of the game whose game type is shooting

Reference answer: 

-- 一,	创建数据库Game
-- 1,删除数据库
	DROP DATABASE IF EXISTS Game;
-- 2,创建数据库
	CREATE DATABASE Game;
-- 3,修改数据库编码方式和字符集排列顺序
	ALTER DATABASE Game CHARACTER SET utf8 COLLATE utf8_bin;
-- 4,使用数据库
	USE Game;
-- 二,数据表操作
-- 1、创建表格players,记录游戏玩家信息:
DROP TABLE IF EXISTS players;
CREATE TABLE players (
    player_id INT PRIMARY KEY, -- 玩家ID,主键
    player_name VARCHAR(50) NOT NULL, -- 玩家姓名,不能为空
    age INT CHECK (age >= 18), -- 年龄,必须大于等于18岁
    country VARCHAR(50) DEFAULT '中国', -- 国家, 默认值为'中国'
    CONSTRAINT unique_player_id UNIQUE (player_id) -- 玩家ID唯一约束
);
-- 2、创建表格games,记录游戏信息:
DROP TABLE IF EXISTS games;
CREATE TABLE games (
    game_id INT PRIMARY KEY, -- 游戏ID,主键
    game_name VARCHAR(50) NOT NULL, -- 游戏名称,不能为空
    release_date DATE, -- 发布日期
    genre VARCHAR(50), -- 游戏类型
    CONSTRAINT unique_game_name UNIQUE (game_name) -- 游戏名称唯一约束
);
-- 3、创建表格scores,记录玩家游戏得分:
DROP TABLE IF EXISTS scores;
CREATE TABLE scores (
    score_id INT PRIMARY KEY, -- 得分ID,主键
    player_id INT, -- 玩家ID,外键
    game_id INT, -- 游戏ID,外键
    score INT, -- 得分
    CONSTRAINT fk_player FOREIGN KEY (player_id) REFERENCES players(player_id), -- 玩家ID外键约束,关联players表
    CONSTRAINT fk_game FOREIGN KEY (game_id) REFERENCES games(game_id) -- 游戏ID外键约束,关联games表
);
-- 三,数据操作
SELECT * FROM players;
SELECT * FROM games;
SELECT * FROM scores;
-- 1、向表格players插入数据:
INSERT INTO players (player_id, player_name, age, country) VALUES
(1,'刘备',21,'蜀国'),
(2,'关羽',23,'蜀国'),
(3,'张飞',45,'蜀国'),
(4,'诸葛亮',28,'蜀国'),
(5,'赵云',30,'蜀国'),
(6,'马超',18,'魏国'),
(7,'黄忠',19,'蜀国'),
(8,'曹贼',27,'魏国');
-- 2、向表格games插入数据:
INSERT INTO games (game_id, game_name, release_date, genre) VALUES
(1,'绝地求生','2023-07-01','射击'),
(2,'英雄联盟','2023-07-02','MOBA'),
(3,'守望先锋','2023-07-03','射击'),
(4,'王者荣耀','2023-07-04','MOBA'),
(5,'蛋仔派对','2023-07-05','休闲'),
(6,'开心消消乐','2023-07-06','休闲'),
(7,'使命召唤','2023-07-07','射击'),
(8,'刺激战场','2023-07-08','射击');
-- 3、向表格scores插入数据:
INSERT INTO scores (score_id, player_id, game_id, score) VALUES
(1,1, 1, 85),
(2,2, 2, 72),
(3,3, 3, 78),
(4,4, 4, 89),
(5,5, 5, 95),
(6,6, 6, 80),
(7,7, 7, 87),
(8,8, 8, 90),
(9,1, 2, 85),
(10,2, 1, 22),
(11,3, 6, 78),
(12,3, 7, 83),
(13,3, 8, 95),
(14,1, 2, 80),
(15,4, 7, 81),
(16,6, 7, 90),
(17,3, 1, 95),
(18,1, 4, 80),
(19,4, 3, 87),
(20,6, 8, 90);
-- 四,完成如下查询题目
-- 1.查询得分ID、玩家姓名、游戏名称和得分
SELECT scores.score_id AS 得分ID, players.player_name AS 玩家姓名, games.game_name AS 游戏名称, scores.score AS 得分
FROM scores
JOIN players ON scores.player_id = players.player_id
JOIN games ON scores.game_id = games.game_id;

-- 2.查询有得分大于88分的人的游戏名称,游戏发布日期,游戏类型(不重复信息)
-- 第一种方法
SELECT DISTINCT g.game_name AS 游戏名称, g.release_date AS 游戏发布日期, g.genre AS 游戏类型
FROM games g
INNER JOIN scores s ON g.game_id = s.game_id
WHERE s.score > 88;
-- 第二种方法
SELECT DISTINCT games.game_name AS 游戏名称, games.release_date AS 游戏发布日期, games.genre AS 游戏类型
FROM scores
JOIN games ON scores.game_id = games.game_id
WHERE scores.score > 88;

-- 3.总得分大于250的玩家的玩家ID,玩家姓名,年龄,国家
-- 第一种方法
SELECT p.player_id AS 玩家ID, p.player_name AS 玩家姓名, p.age AS 年龄, p.country AS 国家
FROM players p
INNER JOIN scores s ON p.player_id = s.player_id
GROUP BY p.player_id, p.player_name, p.age, p.country
HAVING SUM(s.score) > 250;
-- 第二种方法
SELECT players.player_id AS 玩家ID, players.player_name AS 玩家姓名, players.age AS 年龄, players.country AS 国家
FROM scores
JOIN players ON scores.player_id = players.player_id
GROUP BY players.player_id
HAVING SUM(scores.score) > 250;

-- 4.查询马起和诸葛亮玩的相同游戏有哪些
SELECT DISTINCT g.game_name AS 相同游戏
FROM games g
INNER JOIN scores s1 ON g.game_id = s1.game_id
INNER JOIN scores s2 ON s1.game_id = s2.game_id
WHERE s1.player_id = 6 -- 马超的player_id
AND s2.player_id = 4 -- 诸葛亮的player_id;

-- 5.将每个玩家的总得分计算出来
SELECT p.player_name AS 玩家姓名, SUM(s.score) AS 总得分
FROM players p
JOIN scores s ON p.player_id = s.player_id
GROUP BY p.player_name;

-- 6.将马超的国家,魏国改成蜀国
UPDATE players SET country = '蜀国' WHERE player_name = '马超';

-- 7.查询年龄大于25的玩家姓名
SELECT player_name AS 姓名 FROM players WHERE age> 25;

-- 8.查询蜀国玩王者荣耀有哪些玩家
SELECT player_name AS 玩家姓名
FROM players
JOIN scores ON players.player_id = scores.player_id
JOIN games ON scores.game_id = games.game_id
WHERE country = '蜀国' AND game_name = '王者荣耀';

-- 9.查询玩各个游戏的总人数
SELECT games.game_name AS 游戏名称, COUNT(DISTINCT scores.player_id) AS 总人数
FROM games
LEFT JOIN scores ON games.game_id = scores.game_id
GROUP BY games.game_name;

-- 10.查询游戏类型为射击的游戏名称
SELECT game_name AS 游戏名称
FROM games
WHERE genre= '射击';

Guess you like

Origin blog.csdn.net/m0_74293254/article/details/131649112