MySQL view, stored procedure review

1. Create a student table and teacher table

CREATE TABLE `student` (
  `studentID` int(11) NOT NULL AUTO_INCREMENT COMMENT '学生ID',
  `teacherID` int(11) NOT NULL COMMENT 'teacher ID',
  `studentName` varchar(25) NOT NULL COMMENT 'student name',
  `studentAge` int(2) NOT NULL COMMENT 'Student Age',
  PRIMARY KEY (`studentID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

CREATE TABLE `teacher` (
  `teacherID` int(11) NOT NULL AUTO_INCREMENT COMMENT '教师ID',
  `teacherName` varchar(25) NOT NULL COMMENT 'TeacherName',
  `teacherAge` int(2) NOT NULL COMMENT 'teacher age',
  PRIMARY KEY (`teacherID`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

 2. Insert data into the table



 



 

3. Create a view

CREATE VIEW `student_teacher` AS
SELECT s.studentName, t.teacherName
FROM student s, teacher t
where s.teacherID = t.teacherID

 4. Use Views

select * from student_teacher

 5. Create a stored procedure - every time this procedure is executed, all student and teacher information is inserted into the s_t table

First create the s_t table

CREATE TABLE `s_t` (
  `s_t_ID` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `studentID` int(11) NOT NULL COMMENT '学生ID',
  `studentName` varchar(25) NOT NULL COMMENT 'student name',
  `studentAge` int(2) NOT NULL COMMENT 'Student Age',
  `teacherID` int(11) NOT NULL COMMENT 'teacher ID',
  `teacherName` varchar(25) NOT NULL COMMENT 'TeacherName',
  `teacherAge` int(2) NOT NULL COMMENT 'teacher age',
  PRIMARY KEY (`s_t_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 Create stored procedure

BEGIN

	/* define variable start */
	DECLARE v_no_more   	INT           DEFAULT 0;
  DECLARE v_error     	INT           DEFAULT 0;

	DECLARE	v_studentID 	INT						DEFAULT 0;
	DECLARE	v_studentName	VARCHAR(25)		DEFAULT "";
	DECLARE	v_studentAge	INT						DEFAULT	0;
	DECLARE	v_teacherID		INT						DEFAULT	0;
	DECLARE	v_teacherName	VARCHAR(25)		DEFAULT	"";
	DECLARE	v_teacherAge	INT						DEFAULT	0;
	/* define variable end */

	DECLARE cur_s_t	CURSOR FOR
		SELECT
			s.studentID,
			s.studentName,
			s.studentAge,
			t.teacherID,
			t.teacherName,
			t.teacherAge
		FROM student s
		INNER JOIN teacher t
		WHERE s.teacherID = t.teacherID;
		
	DECLARE CONTINUE HANDLER FOR NOT FOUND      SET v_no_more = 1;
  DECLARE CONTINUE HANDLER FOR SQLEXCEPTION   SET v_error = 1;

	-- open transaction
	OPEN cur_s_t;
	FETCH cur_s_t INTO v_studentID, v_studentName, v_studentAge, v_teacherID, v_teacherName, v_teacherAge;
	WHILE v_no_more != 1 DO
		INSERT INTO s_t (studentID,studentName,studentAge,teacherID,teacherName,teacherAge) VALUES (v_studentID,v_studentName,v_studentAge,v_teacherID,v_teacherName,v_teacherAge);
		FETCH cur_s_t INTO v_studentID, v_studentName, v_studentAge, v_teacherID, v_teacherName, v_teacherAge;
	END WHILE;

	CLOSE cur_s_t;
	-- end transaction

	-- transaction processing
	IF v_error = 1 THEN  
			ROLLBACK;  
	ELSE  
			COMMIT;  
	END IF;

END

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326995283&siteId=291194637