自学内容网 自学内容网

MySQL之视图,存储

 

1.
CREATE VIEW v_emp_dept_id_1 AS
SELECT e.emp_name, e.address
FROM emp e
JOIN dept d ON e.dept_id = d.dept_id
WHERE e.dept_id = 1;

2.
CREATE VIEW v_emp_dept AS
SELECT e.emp_name, e.address, d.dept_name
FROM emp  e 
JOIN dept d ON e.dept_id = d.dept_id
WHERE e.dept_id = 1;

3.
CREATE VIEW v_dept_emp_count AS
SELECT d.dept_name, COUNT(e.emp_id) AS emp_count, AVG(e.salary) AS avg_salary
FROM dept d
LEFT JOIN emp e ON d.dept_id = e.dept_id
GROUP BY d.dept_id;

4.
ALTER VIEW v_emp_dept AS 
SELECT e.emp_name, e.address,d.dept_name,e.salary
FROM emp e  
JOIN dept d ON e.dept_id = d.dept_id
WHERE e.dept_id = 1;

 

5.
SHOW TABLES LIKE 'v_emp_dept_id_1';
SHOW TABLES LIKE 'v_emp_dept';
SHOW TABLES LIKE 'v_dept_emp_count';

6.
SHOW CREATE VIEW v_emp_dept_id_1;
SHOW CREATE VIEW v_emp_dept;
SHOW CREATE VIEW v_dept_emp_count;

7.
DROP VIEW IF EXISTS v_emp_dept_id_1;
DROP VIEW IF EXISTS v_emp_dept;
DROP VIEW IF EXISTS v_dept_emp_count;

DELIMITER //
CREATE PROCEDURE s1()
BEGIN
  SELECT SUM(salary) as total_salary FROM emp;
END; //
DELIMITER ;

CALL s1();
DELIMITER //
CREATE PROCEDURE s2(IN emp_name_param VARCHAR(255))
BEGIN
  SELECT address FROM emp WHERE emp_name = emp_name_param;
END; //
DELIMITER ;

CALL s2('张晓红');
DELIMITER //
CREATE PROCEDURE avg_sai(IN dept_id_param INT, IN gender_param CHAR(1), OUT avg_salary_param DECIMAL(10, 2))
BEGIN
  SELECT AVG(salary) INTO avg_salary_param FROM your_employee_table
  WHERE dept_id = dept_id_param AND gender = gender_param;
END; //
DELIMITER ;

DECLARE @avg_salary DECIMAL(10, 2);
CALL avg_sai(1, '男', @avg_salary);
SELECT @avg_salary;
DROP PROCEDURE IF EXISTS s1;
DROP PROCEDURE IF EXISTS s2;
DROP PROCEDURE IF EXISTS avg_sai;


原文地址:https://blog.csdn.net/callmehansea/article/details/140364709

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!