Auto commit

This commit is contained in:
smallkun 2025-03-07 09:53:57 +08:00
parent fe970ef1a1
commit 8c0d6360a2

View File

@ -227,4 +227,71 @@ GROUP BY h.hid;
### MySQL-2
### MySQL-2
```sql
#1添加主键
ALTER TABLE tb_student ADD PRIMARY KEY(student_id);
#2设置默认值
ALTER TABLE tb_student ALTER gender SET DEFAULT '男';
#3 插入记录
INSERT INTO tb_student()
VALUES(10025, '张三', DEFAULT, '2012-10-01', 1001);
#4 更新记录
UPDATE tb_teacher t
SET t.teacher_age = 36
WHERE t.teacher_name = '王老师';
#5 tj11
SELECT MAX(t.age)
FROM tb_teacher t;
#6 tj12
SELECT c.class_name, s.student_name, s.gender
FROM tb_teacher t, tb_student s, tb_class C
where s.class_id = c.class_id AND c.teacher_id = t.teacher_id;
#7 视图
CREATE VIEW v_teacher AS
SELECT c.class_name, t.teacher_name, COUNT(*)
FROM tb_teacher t, tb_student s, tb_class C
where s.class_id = c.class_id AND c.teacher_id = t.teacher_id
GROUP BY c.class_id;
#8
DELIMITER $$
DROP TRIGGER IF EXISTS tri_updateTeacherAge;
CREATE TRIGGER tri_updateTeacherAge
BEFORE UPDATE ON tb_teacher
FOR EACH ROW
BEGIN
IF NEW.teacher_age < 30 THEN
SET NEW.age = '青年';
ELSEIF NEW.teacher_age < 50 THEN
SET NEW.age = '中年';
ELSE
SET NEW.age = '老年';
END IF;
END $$
DELIMITER ;
#9
DELIMITER $$
DROP PROCEDURE IF EXISTS Pro_GetOldClassStudent;
CREATE PROCEDURE Pro_GetOldClassStudent(IN in_class_id INT, OUT out_student_name VARCHAR(50))
BEGIN
SELECT s.student_name INTO out_student_name
FROM tb_student s, tb_class c
WHERE s.class_id = c.class_id AND c.class_id = in_class_id
ORDER BY s.birthday
LIMIT 1;
END $$
DELIMITER ;
CALL Pro_GetOldClassStudent(1001, @result);
SELECT @result;
```