15分钟
任务 3 在MySQL数据库中创建测试表并插入数据
任务目的
在MySQL数据库实例中创建mytest数据库,在数据库中创建3个测试表,并分别插入不同数量的数据,用作执行SQL语句的测试数据。
任务步骤
1.创建测试数据库
创建数据库mytest用于存放测试表,进入mytest数据库。
CREATE DATABASE mytest;
SHOW DATABASES;
USE mytest;2.创建测试表并插入数据
在mytest数据库中创建3张表,分别插入一些数据作为测试数据。
1)第一张表:员工工资表empsalary,表中包含两列(工号id、工资salary),数据类型均为int整数类型。编写while循环,设置变量i,循环插入10000行数据,使用rand()函数随机生成数据插入。
CREATE TABLE empsalary(id int,salary int);
delimiter //
CREATE procedure testDataSalary()
begin
declare i int;
set i=1;
while(i<=10000)do
insert into empsalary values(i, rand()*10000);
set i=i+1;
end while;
end //
delimiter ;
call testDataSalary();2)第二张表:员工部门表empdepartment,表中包含两列(工号id、部门编号department),数据类型均为int整数类型。编写while循环,设置变量i,循环插入10000行数据,使用rand()函数随机生成部门数据插入。
CREATE TABLE empdepartment(id int,department int);
delimiter //
CREATE procedure testDataDepartment()
begin
declare i int;
set i=1;
while(i<=10000)do
insert into empdepartment values(i, rand()*10);
set i=i+1;
end while;
end //
delimiter ;
call testDataDepartment();3)第三张表:部门经理表leader,表中包含两列(部门编号department、部门经理的工号id),数据类型均为int整数类型。编写while循环,设置变量i,循环插入10行数据,使用rand()函数随机生成部门经理的id数据插入。
CREATE TABLE leader(department int,id int);
delimiter //
CREATE procedure testDataLeader()
begin
declare i int;
set i=1;
while(i<=10)do
insert into leader values(i, rand()*10);
set i=i+1;
end while;
end //
delimiter ;
call testDataLeader();
学员评价