重学后端(1)-建库
1. 创建数据库
show databases; # 查看有哪些数据库
create database test; #创建名为test的数据库
use test; #使用名为test的数据库
# 创建名为student 的数据库
create table student(
id bigint primary key auto_increment,
name varchar(20),
age int,
chi int,
math int,
english int
) charset=utf8 engine=InnoDB #指定字符集和编码
# 手动插入数据
insert into student(name, age, chi, math, english) values('用户1', 18, 98, 88, 90)
# 创建插入测试数据的自定义函数
create function ins_data(num int) returns int(11)
begin
declare i default 0;
while i < num do
insert into student(name, age, chi, math, english) values(concat('用户', i), floor(rand()*100), floor(rand()*100), floor(rand()*100), floor(rand()*100));
set i = i + 1;
end while;
return i;
end
select ins_data(20); # 创建20条测试数据
2. 其它一些常见命令
# 修改字符集
alter table student charset utf8
# 输出表数据
delete from student
# 删除表
drop table student
# 删除ins_data函数
drop function ins_data
版权声明:本文为weixin_42596335原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。