1.5 KiB
1.5 KiB
MySQL-1
#1.添加字段
ALTER TABLE t_student ADD descr VARCHAR(200) DEFAULT '无';
#2.插入记录
INSERT INTO t_student()
VALUES(DEFAULT, '林小莉', '女', 8, 10005,'13971555555', '身体瘦弱,训练强度需要调整');
#3.简单查询 tj11
SELECT c.id, c.`name`, CONCAT(YEAR(c.time),'年',MONTH(c.time),'月', DAY(c.time),'日')
FROM t_class c;
#4.排序、分页查询 tj12
SELECT *
FROM t_coach c
ORDER BY c.salary
LIMIT 1, 2;
#5.模糊、多表连接查询 tj13
SELECT co.`name`, cl.`name`, cl.period
FROM t_coach co, t_class cl
WHERE co.id = cl.coachId AND co.`name` LIKE '何%';
#6.更新记录
UPDATE t_coach c
SET c.salary = c.salary + 200
WHERE c.age >= 30;
#7.多表联查+视图
CREATE VIEW v_swim AS
SELECT s.`name` '学员姓名', co.`name` '教练姓名', cl.`name` '班级名称'
FROM t_student s, t_class cl, t_coach co
WHERE s.classId = cl.id AND cl.coachId = co.id;
#8.插入操作的触发器
DELIMITER $$
DROP TRIGGER IF EXISTS tri_familylog;
CREATE TRIGGER tri_familylog
AFTER DELETE ON t_family
FOR EACH ROW
BEGIN
INSERT INTO t_log()
VALUES(DEFAULT, OLD.fid, '删除家庭', '2024-01-01');
END $$
DELIMITER ;
#9.条件查询+聚合函数 存储过程
DELIMITER $$
DROP PROCEDURE IF EXISTS pro_getStudentNumber;
CREATE PROCEDURE pro_getStudentNumber(IN in_classId INT, OUT out_number INT)
BEGIN
SELECT COUNT(*) INTO out_number
FROM t_student s
WHERE s.classId = in_classId;
END $$
DELIMITER ;
#测试存储过程
CALL pro_getStudentNumber(10002, @result);
SELECT @result;