数据库头歌实训

本文详细介绍了MySQL数据库中对表和字段的各种操作,包括修改表名、字段名及数据类型,增加和删除列,调整字段顺序,以及删除外键约束。此外,还涵盖了插入、更新和删除数据,以及单表查询的各种方法,如IN、BETWEEN、LIKE、空值查询、排序和分组等。
摘要由CSDN通过智能技术生成

MySQL数据库 - 数据库和表的基本操作(一)

第一关:

修改表名:alter table 旧表名 rename 新表名;
查看表的基本结构:describe 表名;
USE Company;

#请在此处添加实现代码
########## Begin ##########

########## modify the table name ##########

alter table tb_emp rename jd_emp;

########## show tables in this database ##########

show tables;

########## describe the table ##########

describe jd_emp;

########## End ##########

第二关:

修改字段名:alter table 表名 change 旧字段名 新字段名 新数据类型;
修改数据类型:alter table 表名 change modify 字段名 数据类型;
USE Company;

#请在此处添加实现代码
########## Begin ##########

########## change the column name ##########

alter table tb_emp change id prod_id int(11);

########## change the data type of column ##########

alter table tb_emp change Name Name varchar(30);

########## End ##########

DESCRIBE tb_emp;

第三关:

增加列:alter table 表名 add 新字段名 数据类型;
删除列:alter table 表名 drop 字段名;
USE Company;

#请在此处添加实现代码
########## Begin ##########

########## add the column ##########

alter table tb_emp ADD Country varchar(20) after Name;
 
########## delete the column ##########

alter table tb_emp drop Salary;

########## End ##########

DESCRIBE tb_emp;

第四关:

修改字段排列位置:alter table 表名 modify 字段1 数据类型 [first/after] 字段二
USE Company;

#请在此处添加实现代码
########## Begin ##########

########## modify the column to top ##########

alter table tb_emp modify Name varchar(25) first;

########## modify the column to the rear of another column ##########

alter table tb_emp modify DeptId int(11) after Salary;

########## End ##########

DESCRIBE tb_emp;

第五关:

删除外键约束:alter table 表名 drop foreign key 外键约束名;
USE Company;

#请在此处添加实现代码
########## Begin ##########

########## delete the foreign key ##########

alter table tb_emp drop foreign key emp_dept;

########## End ##########
SHOW CREATE TABLE tb_emp \G;

数据库和表的基本操作(二)

第一关:

添加数据:insert into 表名(字段1,字段2,字段3...) values(对应数据...);
USE Company;

#请在此处添加实现代码
########## Begin ##########

########## bundle insert the value ##########

insert into tb_emp(Id, Name, DeptId, Salary) values(1, "Nancy", 301, 2300.00), (2, "Tod", 303, 5600.00), (3, "Carly", 301, 3200.00);

########## End ##########
SELECT * FROM tb_emp;

第二关:

更新数据:
update 表名
set 字段名1 = ,字段名2 = ,...
where 字段名 = ();
USE Company;

#请在此处添加实现代码
########## Begin ##########

########## update the value ##########

update tb_emp
set Name = "Tracy", DeptId = 302, Salary = 4300.00
where Id = 3;

########## End ##########

SELECT * FROM tb_emp;

第三关:

删除指定条件的数据:delete from 表名 where 条件;
USE Company;

#请在此处添加实现代码
########## Begin ##########

########## delete the value ##########

delete from tb_emp where Salary > 3000;

########## End ##########

SELECT * FROM tb_emp;

单表查询

第一关:

查询几个字段的信息:select 字段1,字段2,... from 表名;
查询数据表所有内容:select * from 表名;
USE Company;

#请在此处添加实现代码
########## Begin ##########

########## retrieving the Name and Salary ##########

select Name, Salary from tb_emp;

########## retrieving all the table ##########

select * from tb_emp;

########## End ##########

第二关:

IN关键字查询:
select 字段1,字段2,... from 表名
where 条件;
USE Company;

#请在此处添加实现代码
########## Begin ##########

########## retrieving the Name and Salary with IN statement ##########

select Name, Salary from tb_emp
where Id != 1;

########## End ##########

第三关:

between and 查询:
select 字段1,字段2,... from 表名
between 条件1 and 条件2;
USE Company;

#请在此处添加实现代码
########## Begin ##########

########## retrieving the Name and Salary with BETWEEN AND statement ##########

select Name, Salary from tb_emp
where Salary between 3000 and 5000;

########## End ##########

第四关:

like匹配查询:
select 字段1,字段2 from 表名
where 字段 like 匹配格式;
USE Company;

######### Begin #########

select Name, Salary from tb_emp
where Name like 'C%';

######### End #########

第五关:

查询空值:
select 字段名 from 表名
where 字段名 is NULL;
去重:
select distinct 字段名 from 表名;
USE Company;

######### Begin #########

select * from tb_emp
where DeptId is NULL;

######### End #########

######### Begin #########

select distinct Name from tb_emp;

######### End #########

第六关:

带 and 与 or 的多条件查询:
select 字段名 from 表名
where 字段名 条件;
USE Company;

######### Begin #########

select * from tb_emp
where DeptId = 301 and Salary > 3000;

######### End #########

######### Begin #########

select * from tb_emp
where DeptId in(301, 303);

######### End #########

第七关:

对排序结果进行排序:
select 字段名 from 表名 order by 字段名 条件;
* ASC:升序关键字
* DESC:降序关键字
USE School;

#请在此处添加实现代码
########## Begin ##########

########## 查询1班同学的所有信息以成绩降序的方式显示结果 ##########

select * from tb_score
where class_id = 1 order by score desc;

########## End ##########

第八关:

分组查询(返回每个分组种首次出现的一条记录):
select 字段名 from 表名 group by 字段名;
USE School;

#请在此处添加实现代码
########## Begin ##########

########## 对班级名称进行分组查询 ##########

select * from tb_class group by class_id;

########## End ##########

第九关:

limit对查询结果的限制
select 字段名 from 表名 limit 起始位置的后一个,查询的结果数;
USE School;

#请在此处添加实现代码
########## Begin ##########

########## 查询班级中第2名到第5名的学生信息 ##########

select * from tb_score order by score desc limit 1, 4;

########## End ##########

连接查询和子查询

第一关:

as关键字可以给表另起别名
* jion (连接的表) on (判断条件) 连接查询
* where 等值查询

########## 查询数据表中学生姓名和对应的班级 ##########
#请在此处添加实现代码
########## Begin ##########

select tb_student as studentName, tb_class as className
from tb_student join tb_class on tb_student.class_id=tb_class.id;

########## End ##########

第二关:

* 左外连接:在内连接的基础上,还包含表1中所有不符合条件的数据行,并在其中的表2列填写 NULL;
* 右外连接:在内连接的基础上,还包含表2中所有不符合条件的数据行,并在其中的表1列填写 NULL。
USE School;

########## 使用左外连接查询所有学生姓名和对应的班级 ##########

#请在此处添加实现代码
########## Begin ##########

select tb_student.name as studentName, tb_class.name as className
from tb_student left join tb_class on tb_student.class_id = tb_class.id;

########## End ##########

########## 使用右外连接查询所有学生姓名和对应的班级 ##########

#请在此处添加实现代码
########## Begin ##########

select tb_student.name as studentName, tb_class.name as className
from tb_student right join tb_class on tb_student.class_id = tb_class.id;

########## End ##########

第三关:

ALL关键字:返回与所有值比较的结果
ANY关键字:返回与任意值比较的结果
IN关键字:返回指定的一个值是否在这个集合中
USE Company;
#请在此处添加实现代码
########## Begin ##########

#1.使用 ALL 关键字进行查询

select position, salary from tb_salary
where salary > all(select salary from tb_salary where position = 'Java');

#2.使用 ANY 关键字进行查询

select position, salary from tb_salary
where salary > any(select salary from tb_salary where position = 'Java');

#3.使用 IN 关键字进行查询

select position, salary from tb_salary
where position = 'Java';

########## End ##########

使用聚合函数查询

第一关

CONUT:统计记录的总条数
select count (*/字段名) from 表名;
USE School;

#请在此处添加实现代码
########## Begin ##########

########## 查询该表中一共有多少条数据 ##########

select count(*) from tb_class;

########## 查询此表中367班有多少位学生 ##########

select classid, count(*) from tb_class
where classid = 367;

########## End ##########

第二关

SUM:对数据表的某列进行求和
select sum(字段名) from 表名;
USE School;

#请在此处添加实现代码
########## Begin ##########

########## 查询所有学生总分数 ##########

select sum(score) from tb_class;

########## 查询学生语文科目的总分数 ##########

select course, sum(score) from tb_class
where course = "语文";

########## End ##########

第三关

AVG:对数据表某列进行求平均值操作
select avg(字段名) from 表名;
USE School;

#请在此处添加实现代码
########## Begin ##########

########## 查询学生语文科目的平均分数 ##########

select course, avg(score) from tb_class
where course = "语文";

########## 查询学生英语科目的平均分数 ##########

select course, avg(score) from tb_class
where course = "英语";

########## End ##########

第四关

MAX:求某列的最大数值
select max(字段名) from 表名;
USE School;

#请在此处添加实现代码
########## Begin ##########

########## 查询语文课程中的最高分数 ##########

select course, max(score) from tb_class
where course = "语文";

########## 查询英语课程中的最高分数 ##########

select course, max(score) from tb_class
where course = "英语";

########## End ##########

第五关

MIN:求某列的最小数值
select min(字段名) from 表名;
USE School;

#请在此处添加实现代码
########## Begin ##########

########## 查询语文课程中的最低分数 ##########

select course, min(score) from tb_class
where course = "语文";

########## 查询英语课程中的最低分数 ##########

select course, min(score) from tb_class
where course = "英语";

########## End ##########

复杂查询

第一关

#请在此添加实现代码
########## Begin ##########

update tb_Salary
set
    sex = case sex when 'm' then 'f'
          else 'm'
    end;        

########## End ##########

第二关

#请在此添加实现代码
########## Begin ##########

select (
    case
    when mod(id, 2) != 0 and id != (select count(*) from tb_Seat) then id + 1
    when mod(id, 2) != 0 and id = (select count(*) from tb_Seat) then id
    else id - 1 
    end
)
as id, name from tb_Seat order by id;

########## End ##########

第三关

#请在此添加实现代码
########## Begin ##########

select Score,(select count(distinct score) from score where score >=s.score) as Rank from score as s
order by Score desc;

select Score,(select count(*) from score as s2 where s2.score >s1.score)+1 as Rank from score as s1
order by Rank;

########## End ##########

第四关


  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值