头歌 关系数据库系统的查询优化答案

第1关:快速批量插入数据

任务描述

本关任务:掌握快速往表中插入大量数据。

答案:


use yy;
create table student(
student_id int unsigned auto_increment,
score_math int,
score_eng int,
score_art int,
primary key(student_id)

);


DROP PROCEDURE IF EXISTS mytest;
DELIMITER $$
CREATE PROCEDURE mytest()
BEGIN
  DECLARE n INT DEFAULT 1;
  SET @exesql = 'insert into student (score_math,score_eng,score_art) values (88,77,90)';
  SET @exedata = '';
  WHILE n < 10000 DO
    SET @exedata = CONCAT(@exedata,',','(70,86,98)');
    
    SET @exesql=CONCAT(@exesql,@exedata);
    
   SET @exedata = '';
    
    SET n = n + 1;
  END WHILE;
  SET @exesql=CONCAT(@exesql,";");
 PREPARE stmt FROM @exesql;
 EXECUTE stmt;
 DEALLOCATE PREPARE stmt; 
END;$$ 
CALL  mytest();



你可能感兴趣的:(数据库,前端,linux)