-- SQL workarea -- 3/12 tee 2025_3_12_sql_log.txt -- Prepared statements. SET @sql = "SELECT * FROM toyu.student"; PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @sql = "SELECT * FROM toyu.student"; PREPARE stmt FROM @sql; EXECUTE stmt; EXECUTE stmt; EXECUTE stmt; DEALLOCATE PREPARE stmt; -- with placeholders. Parametrized SQL. SET @sql = "SELECT * FROM toyu.student WHERE major = ? AND ach >= ?"; PREPARE stmt FROM @sql; SET @major = 'CSCI'; SET @ach = 38; EXECUTE stmt USING @major, @ach; EXECUTE stmt USING 'CSCI', 38; SET @major = 'CINF'; SET @ach = 15; EXECUTE stmt USING @major, @ach; SET @major = 'ITEC'; SET @ach = 25; EXECUTE stmt USING @major, @ach; SELECT s.schoolCode, s.schoolName, COUNT(d.deptCode) AS n_departments FROM school AS s LEFT JOIN department AS d ON (s.schoolCode = d.schoolCode) GROUP BY s.schoolCode, s.schoolName; CREATE OR REPLACE VIEW school_summary( schoolCode, schoolName, n_departments) AS SELECT s.schoolCode, s.schoolName, COUNT(d.deptCode) AS n_departments FROM school AS s LEFT JOIN department AS d ON (s.schoolCode = d.schoolCode) GROUP BY s.schoolCode, s.schoolName; DESC school_summary; SELECT * FROM school_summary WHERE n_departments > 0; -- get the full name of a student. DELIMITER $$ CREATE OR REPLACE FUNCTION GetStudentFullName( student_id INT ) RETURNS VARCHAR(61) DETERMINISTIC BEGIN DECLARE full_name VARCHAR(61); SELECT CONCAT(fname, ' ', lname) -- INTO full_name FROM Student WHERE stuId = student_id; RETURN full_name; END $$ DELIMITER ; SELECT GetStudentFullName(100000); SELECT GetStudentFullName(100001); -- In MySQL, parameter modes (IN, OUT, and INOUT) dictate how data is passed to and returned from stored procedures. IN is the default, meaning the parameter only accepts input; OUT is used for parameters that return values, and INOUT enables both input and output. DELIMITER // CREATE OR REPLACE PROCEDURE deptInfo(IN dCode VARCHAR(4), OUT numFaculty INT) BEGIN -- Display some information. SELECT d.deptName, d.SchoolCode, t1.n_majors, t2.n_minors FROM department AS d INNER JOIN (SELECT COUNT(stuId) AS n_majors FROM student WHERE major = dCode) AS t1 INNER JOIN (SELECT COUNT(stuId) AS n_minors FROM student WHERE minor = dCode) AS t2 WHERE d.deptCode = dCode; -- MySQL does not direct sending output to console. -- It is necessary to use a SQL statement. SELECT 'Debuggin comment can be put here.'; SELECT CONCAT('Faculty in the department: ', dCode) AS faculty; SELECT * FROM faculty AS f WHERE f.deptCode = dCode; SELECT COUNT(f.facId) INTO numFaculty FROM faculty AS f WHERE f.deptCode = dCode; END // DELIMITER ; SHOW CREATE PROCEDURE deptInfo; SET @numFaculty = 0; SET @dCode = 'CSCI'; CALL deptInfo(@dCode, @numFaculty); SELECT @dCode, @numFaculty; SET @dCode = 'ITEC'; CALL deptInfo(@dCode, @numFaculty); SELECT @dCode, @numFaculty;