问题1:
帮我创建2个表student与score表,要求student表有id,createDate,userName,phone,age,sex,introduce,
要求score表有id,scoreName,result,studentId(student表的id外键)。
要求student表中插入5条学生信息,都要是中文的。
要求score表中对应studentId插入10条信息,每个学生ID要有2个科目的成绩。
给出DDL与DML语句。
-- 创建student表
CREATE TABLE student (
id INT PRIMARY KEY AUTO_INCREMENT,
createDate DATE NOT NULL,
userName VARCHAR(50) NOT NULL,
phone VARCHAR(20) NOT NULL,
age INT NOT NULL,
sex ENUM('男', '女') NOT NULL,
introduce TEXT
);
-- 创建score表
CREATE TABLE score (
id INT PRIMARY KEY AUTO_INCREMENT,
scoreName VARCHAR(50) NOT NULL,
result INT NOT NULL,
studentId INT,
FOREIGN KEY (studentId) REFERENCES student(id) ON DELETE CASCADE
);
-- 插入5条学生信息
INSERT INTO student (createDate, userName, phone, age, sex, introduce) VALUES
('2023-01-01', '张三', '13800138000', 20, '男', '来自北方,喜欢运动'),
('2023-02-02', '李四', '13900139000', 21, '女', '来自南方,爱好音乐'),
('2023-03-03', '王五', '13700137000', 22, '男', '来自东部,擅长编程'),
('2023-04-04', '赵六', '13600136000', 23, '女', '来自西部,喜欢旅行'),
('2023-05-05', '孙七', '13500135000', 24, '男', '来自中部,热爱阅读');
-- 插入10条成绩信息
INSERT INTO score (scoreName, result, studentId) VALUES
('数学', 90, (SELECT id FROM student WHERE userName = '张三')),
('语文', 85, (SELECT id FROM student WHERE userName = '张三')),
('数学', 88, (SELECT id FROM student WHERE userName = '李四')),
('英语', 92, (SELECT id FROM student WHERE userName = '李四')),
('物理', 89, (SELECT id FROM student WHERE userName = '王五')),
('化学', 93, (SELECT id FROM student WHERE userName = '王五')),
('数学', 86, (SELECT id FROM student WHERE userName = '赵六')),
('生物', 91, (SELECT id FROM student WHERE userName = '赵六')),
('历史', 87, (SELECT id FROM student WHERE userName = '孙七')),
('地理', 94, (SELECT id FROM student WHERE userName = '孙七'));
问题2:
帮我创建一个多表联合查询以student为主表,查看学生的所有科目成绩总分。
SELECT
s.id AS student_id,
s.userName AS student_name,
SUM(sc.result) AS '总分',
AVG(sc.result) AS '平均分'
FROM
student s
LEFT JOIN
score sc ON s.id = sc.studentId
GROUP BY
s.id, s.userName
having AVG(sc.result)>85
order by AVG(sc.result)>85 desc;
问题3:
帮我根据两个DDL创建一个插入信息的存储过程,先插入10条学生表信息,再根据插入的学生表信息插入100条对应studentId的score成绩,
所有信息随机生成,学生表的自增序号从6开始。不要使用DECLARE声明变量,要使用set生明。
DELIMITER //
CREATE PROCEDURE InsertRandomStudentAndScoreData()
BEGIN
-- 插入10条学生信息
REPEAT
INSERT INTO student (createDate, userName, phone, age, sex, introduce)
VALUES (
CURDATE() - INTERVAL FLOOR(RAND() * 30) DAY, -- 随机生成过去30天内的日期
CONCAT('Student', FLOOR(RAND() * 10000)), -- 随机生成用户名
CONCAT('1', FLOOR(RAND() * 1000000000), FLOOR(RAND() * 10000)), -- 随机生成电话号码
FLOOR(RAND() * 30) + 1, -- 随机生成年龄(1-30岁)
IF(RAND() > 0.5, '男', '女'), -- 随机生成性别
CONCAT('This is a random introduction for student ', FLOOR(RAND() * 10000)) -- 随机生成介绍
);
UNTIL ROW_COUNT() = 10 END REPEAT; -- 重复直到插入10条记录
-- 为每个学生插入10条成绩信息
INSERT INTO score (scoreName, result, studentId)
SELECT
CONCAT('Subject', FLOOR(RAND() * 100)), -- 随机生成科目名
FLOOR(RAND() * 100), -- 随机生成成绩(0-99分)
s.id -- 学生ID,来自刚刚插入的学生记录
FROM (
SELECT id FROM student ORDER BY id DESC LIMIT 10 -- 获取最后插入的10个学生的ID
) AS s
CROSS JOIN (SELECT 1 n UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10) AS numbers -- 交叉连接生成10行
ORDER BY s.id, numbers.n; -- 按学生ID和数字排序以确保每个学生有10条记录
END //
DELIMITER ;
CALL InsertRandomStudentAndScoreData();
问题4:
帮我创建一个修改score表scoreName的触发器,当修改scoreName的时候触发,判断修改的scoreName是否是数学,如果是数学就改成(天书)。
并且给出测试示例。
DELIMITER //
CREATE TRIGGER trg_update_scoreName
BEFORE UPDATE ON score
FOR EACH ROW
BEGIN
IF NEW.scoreName = '数学' THEN
SET NEW.scoreName = '天书';
END IF;
END;
//
DELIMITER ;
-- 尝试将id为1的score记录的scoreName更新为数学
UPDATE score SET scoreName = '数学' WHERE id = 13;
-- 查询score表,查看触发器是否生效
SELECT * FROM score;
问题5:
创建一个两个表的综合【游标查询】,要求根据学生姓名进行查询,查询的结果返回两个表的综合信息。
DELIMITER //
CREATE PROCEDURE GetStudentScoresByName(IN p_userName VARCHAR(50))
BEGIN
-- 声明变量来存储查询结果
DECLARE done INT DEFAULT FALSE;
DECLARE v_createDate DATE;
DECLARE v_userName VARCHAR(50);
DECLARE v_phone VARCHAR(20);
DECLARE v_age INT;
DECLARE v_sex ENUM('男', '女');
DECLARE v_introduce TEXT;
DECLARE v_scoreName VARCHAR(50);
DECLARE v_result INT;
-- 声明游标
DECLARE cur CURSOR FOR
SELECT
s.createDate,
s.userName,
s.phone,
s.age,
s.sex,
s.introduce,
sc.scoreName,
sc.result
FROM
student s
JOIN
score sc ON s.id = sc.studentId
WHERE
s.userName = p_userName;
-- 声明游标的结束处理
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
-- 打开游标
OPEN cur;
-- 读取游标中的数据
read_loop: LOOP
FETCH cur INTO v_createDate, v_userName, v_phone, v_age, v_sex, v_introduce, v_scoreName, v_result;
IF done THEN
LEAVE read_loop;
END IF;
-- 这里你可以选择不同的方式来使用这些数据,比如输出到控制台或返回给调用者
-- 示例:输出到控制台
SELECT
v_createDate AS createDate,
v_userName AS userName,
v_phone AS phone,
v_age AS age,
v_sex AS sex,
v_introduce AS introduce,
v_scoreName AS scoreName,
v_result AS result;
END LOOP;
-- 关闭游标
CLOSE cur;
END //
DELIMITER ;
CALL GetStudentScoresByName('张三');