关卡一
使用命令行连接数据库服务器的命令是什么?
mysql -u用户名 -p密码
列出数据库的命令
* 查看所有数据库:show databases;
* 使用数据库:use 数据库名字;
* 查看当前使用的数据库:select database();
* 创建数据库:create database 数据库名字 charset= utf8;
* 删除数据库:drop database 数据库名字;
创建数据库python:create database python charset=utf8;
列出表的命令
* 查看当前数据库的所有表:show tables;
* 查看指定表的结构:desc 表名;
设计班级表结构为id、name、isdelete,编写创建表的语句:
create table classes (id int unsigned not null primary key auto_increment,
name varchar(20),
isdelete bit default 0
);
列出数据操作语句的语法
* 增加insert into 表名 values();
* 修改update table 表名 set
* 删除delete from 表名 where
* 基本查询select * from 表名;
向班级表中插入数据python1、python2、python3
insert into classes(name) values('python1'),('python2'),('python3');
关卡二
学生表结构设计为:姓名、生日、性别、家乡,并且学生表与班级表为多对一的关系,写出创建学生表的语句
create table students1 (id int unsigned not null primary key auto_increment,
name varchar(20),
birthday date,
gender enum('男','女'),
hometown varchar(20),
clsid int unsigned,
isdelet bit default 0
);
向学生表中插入数据:
* python1班有郭靖、黄蓉,要求:使用全列插入,一次一值
insert into students1 values(0,'郭靖','2009-01-01','男','内蒙古',1,0);
insert into students1 values(0,'黄蓉','2010-11-28','女','中原',1,0);
* python2班有杨过、小龙女,要求:使用指定列插入,一次一值
insert into students1(name,gender,clsid) values('杨过','男',2);
insert into students1(name,gender,clsid) values('小龙女','女',2);
* 未分班的学生有黄药师、洪七公、洪七婆,要求:使用指定列插入,一次多值
insert into students1(name) values('黄药师'),('洪七公'),('洪七婆');
查询学生的姓名、生日,如果没有生日则显示无
select name,ifnull(birthday,'无') from students1;
查询学生的姓名、年龄
select name,year(now())-year(birthday) as age from students1;
逻辑删除洪七婆
update students1 set isdelet=1 where name='洪七婆';
修改洪七公的性别为女
update students1 set gender='女' where name='洪七公';
关卡三
设计科目表subjects,包括科目名称
create table subjects(id int unsigned primary key not null auto_increment,
sname varchar(20),
isdelect bit default 0
);
向表中插入数据,科目名有:python、数据库、前端
insert into subjects values(0,'python',0),(0,'数据库',0),(0,'前端',0);
设计成绩表,字段包括:学生id、科目id、成绩
create table scores(id int unsigned not null primary key auto_increment,
sid int unsigned not null,
kid int unsigned not null,
score int unsigned not null
);
向成绩表中添加一些示例数据
insert into scores(sid,kid,score) values(1,1,90),(2,3,80),(3,2,70),(1,3,95);
网友评论