0
点赞
收藏
分享

微信扫一扫

s q l2

科牛 2021-09-29 阅读 21
测试e班

1.创建test数据库 (A题)
create database test

use test

2.在test数据库中创建student表id设置为主键自增长
create table student(
id int primary key auto_increment,
name varchar(20),
score int,
address varchar(50),
usermail varchar(20)
)

3.向student表中添加记录
insert into student(id,name,score,address,usermail)
values(1,'张三',98,'北京','111111111@qq.com'),
(2,'李四',88,'上海','111111112@qq.com'),
(3,'王五',78,'广州','111111113@qq.com'),
(4,'赵六',68,'深圳','111111114@qq.com'),
(5,'孙七',58,'杭州','111111115@qq.com'),
(6,'小红',48,'北京','111111116@qq.com'),
(7,'小黑',99,'上海','111111117@qq.com'),
(8,'小绿',100,'杭州','111111118@qq.com'),
(9,'小粉',60,'杭州','111111119@qq.com'),
(10,'小紫',70,'黑龙江','111111110@qq.com')

4.使用sql语句查询出表中的所有内容
select * from student

5.使用sql语句查询出表中所有同学的id,name,score
select id,name,score from student

6.更改useremail字段的数据类型为varchar(50)
alter table student modify usermail varchar(50)

7.向表中添加一个字段,字段名称为“pingjia”,字段类型为varchar(20)
alter table student add pingjia varchar(20)

8.更改姓名是张三的同学的分数为88
update student set score='88' where name=‘张三’

9.如果80分为及格线,查询出所有及格的同学的详细信息
select * from student where score>=80

10.把姓名是“小红”的同学的分数在原来的基础上+40
update student set score=score+40 where name='小红'

11.使用关键字in,查询id值是1或5或7的同学的基本信息
select * from student where id in(1,5,7)

12.查询id值在5至8的所有同学的基本信息
select * from student id where id>=5 and id<=8

13.查询姓名是小红并且分数大于60的同学的基本信息
select * from student where name='小红' and score>60

14.查询姓名是小红或者分数大于90的同学的基本信息
select * from student where name='小红' or score>90

15.查询score字段值是NULL的同学的基本信息
select * from student where score='null'

16.查询score字段值不是NULL的同学的id和name
select id,name from student where score is not null


1.创建数据库 (C)
create database db_test

use db_test

2.在test数据库中创建yuangong表
create table yuangong(
sid int primary key auto_increment,
sname varchar(20) not null,
sex varchar(4) deeault '男',
job varchar(20)not null,
birthday date,
salary decimal,
comm decimal,
widthhold int
)

4.表中添加如下记录
insert into yuangong (sid,sname,sex,job,birthday,salary,comm,widthhold)values
(1001,'张三','男','高级程师','1975-1-1',2200,1100,200),
(1002,'李四','女','助工','1985-1-1',1200,200,100),
(1003,'王五','男','工程师','1978-11-11',1900,700,200),
(1004,'赵六','男','工程师','1979-1-1',1960,700,150)

4.修改表名为emp
alter table yuangong RENAME emp

5.向表中添加字段Hobby,设置类型为varchar(50),设置唯一约束
alter table emp add hobby varchar(50) unique

6.使用desc语句查看表结构
show create table emp

7.向表中添加记录,字段对应值分别为(1005,林青霞,女,架构师,1969-12-12,8000,NULL,100,阅读)
insert into emp values(1005,'林青霞','女','架构师','1969-12-12',8000,null,100,'阅读')

8.修改sname字段的类型为varchar(20)
alter table emp modify sname VARCHAR(20)

9.查询表中sid字段的值从是1002或1003或1005员工的所有记录
select * from emp where sid = 1002 OR sid = 1003 OR sid = 1005

10.修改表中job值是高级工程师员工的job为“架构师”
update emp set job='架构师' where job='高级程师'

11.删除表中sid是1003并且job是王五的员工的记录
delete from emp where sid = 1003 and job = '王五'

12.修改表中姓名是1004员工的salary在原来的基础上-300
update emp set salary = salary - 300 where sid = 1004


1创建一个学校数据库(jiyun) (B)
create database jiyun

2创建课程(lesson)id(整形主键)name(课程名称)
create table lesson(
id int primary key auto_increment,
name varchar(50)
)

3创建一个班级(cls),包含字段id(整形主键)name(班级名称)number(班级人数)lesson_id(课程名称id)
CREATE TABLE cls (
id int primary key auto_increment,
name varchar(50),
number varchar(50),
lesson_id varchar(50)
)

4在lesson表中插入两数据
insert into lesson(name)VALUES
('python高级'),
('数据库vue')

5在班级表中插入6条记录
insert into cls(name,number,lesson_id)values
('1905A','22','1'),
('1906A','44','1'),
('1904A','33','1'),
('1901A','18','2'),
('1902A','48','2'),
('1903A','56','2')

6.查询类型为 'python高级' 的所有班级、人员数量
select * from cls where lesson_id in(select id from lesson where name='python高级')

7.查询每个班级中 人数最多、最少
select max(number) as '人数最多',min(number) as'人数最少' from cls

8.查询所有人员数量大于平均人数的班级,并且按班级人数降序排序
select * from cls where number < (select avg(number) from cls) order by number desc

9.修改cls表中'1902A'班级数量为44
update cls set number='44' where name='1902A'


1.创建库 (D)
create database databook

2.自定义类Book,表名为book
use databook
create table book(
id int primary key auto_increment,
bookname varchar(10),
price int,
author varchar(20),
publish varchar(20)
)

2.向表里添加10条数据
insert into book(bookname,price,author,publish) values
('北平无故事',25,'刘和平','作家出版社'),
('人间失格',16,'太宰治著','作家出版社'),
('高兴',14,'贾平凹','人民出版社'),
('源氏物语',57,'刘和平','人民出版社'),
('卡夫卡文集',9,'卡夫卡','邮电出版社'),
('大家',12,'王蒙','邮电出版社'),
('拉片子',37,'杨健','清华出版社'),
('古代散文',5,'归有光','安徽出版社'),
('百花散文',6,'孙虹选','百花文艺出版社'),
('方令孺散文集',5,'方令孺','安徽文艺')

3.查询所有图书的信息,并按价格降序显示
select * from book where price order by price asc

4.查询所有作家出版社的图书信息,并按价格降序显示
select author from book where price order by price desc

5.查询出所有刘和平的图书信息 ,并输出
select * from book where author = '刘和平'

6.删除ID是2的记录,如果没有相关记录则提示
delete from book where id = 2 select * from book

7.将所有价格不足10元的图书调到10元,并查看信息
update book set price = 10 where price <10

8.查看所有图书的价格情况,并升序显示
select price from book where price order by price asc

9.查看所有价格低于20元的图收信息
select publish from book where price <=20

10.所有图书的价格上调20%,并查看信息
select * from book
update book set price = price*1.2

举报

相关推荐

0 条评论