1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| SET GLOBAL log_bin_trust_function_creators=TRUE; DROP TABLE IF EXISTS Course; CREATE TABLE Course( cno char(4) PRIMARY KEY, cname char(10) not null, pcno char(4), credit SMALLINT
);
DROP FUNCTION IF EXISTS rand_pcno; DELIMITER $$ CREATE FUNCTION rand_pcno() RETURNS INT(5) BEGIN DECLARE i INT DEFAULT 0; SET i = FLOOR(1+rand()*3); RETURN i; END$$ DELIMITER;
DROP FUNCTION IF EXISTS rand_credit; DELIMITER $$ CREATE FUNCTION rand_credit() RETURNS INT(5) BEGIN DECLARE i INT DEFAULT 0; SET i = FLOOR(1+rand()*4); RETURN i; END$$ DELIMITER;
DROP PROCEDURE IF EXISTS initCourse; DELIMITER $$ CREATE PROCEDURE initCourse() BEGIN DECLARE i INT DEFAULT 5; WHILE i < 20 DO INSERT INTO Course(cno, cname, pcno, credit) VALUES(i,'c_name', rand_pcno() ,rand_credit()); SET i = i + 1; END WHILE; END$$
INSERT INTO Course(cno, cname, pcno, credit) VALUES(1,'math','',5); INSERT INTO Course(cno, cname, pcno, credit) VALUES(2,'C','',3); INSERT INTO Course(cno, cname, pcno, credit) VALUES(3,'Cpp','',3); INSERT INTO Course(cno, cname, pcno, credit) VALUES(4,'Java','',4);
CALL initCourse();
|