行业知识
头歌——MySQL 基本操作
2026-11-09 07:16:26
来源:互联网转载
目录
数据库1-MySQL数据定义与操作实战
MySQL数据库 - 初识MySQL
MySQL数据库 - 数据库和表的基本操作(一)
MySQL数据库 - 数据库和表的基本操作(二)
MySQL数据库 - 单表查询(一)
MySQL数据库 - 单表查询(二)
MySQL数据库 - 单表查询(三)
MySQL数据库 - 连接查询
MySQL数据库 - 子查询
MySQL数据库 - 复杂查询(一)
MySQL数据库 - 复杂查询(二)
MySQL数据库 - 使用聚合函数查询
数据库1-MySQL数据定义与操作实战
MySQL数据库 - 初识MySQL
第1关:创建数据库
mysql -uroot -p123123 -h127.0.0.1
create database MyDb;
第2关创建表
mysql -uroot -p123123 -h127.0.0.1
create database TestDb;
use TestDb;
create table t_emp ( id int, name varchar(32), deptId int, salary float);
第3关:使用主键约束
mysql -uroot -p123123 -h127.0.0.1
create database MyDb;
use MyDb;
create table t_user1( userId INT PRIMARY KEY, name VARCHAR(32), password VARCHAR(11), phone VARCHAR(11), email VARCHAR(32));
create table t_user2( name VARCHAR(32), phone VARCHAR(11), email VARCHAR(32), PRIMARY KEY(name,phone));
第4关:外键约束
mysql -uroot -p123123 -h127.0.0.1
create database MyDb;
use MyDb;
CREATE TABLE t_class( id INT PRIMARY KEY, name VARCHAR(22) );
CREATE TABLE t_student( id INT PRIMARY KEY, name VARCHAR(22) , classId int, CONSTRAINT fk_stu_class1 FOREIGN KEY(classId) REFERENCES t_class(id));
第5关:添加常用约束
mysql -uroot -p123123 -h127.0.0.1
CREATE DATABASE MyDb;
USE MyDb;
CREATE TABLE t_user( id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(32) NOT NULL UNIQUE, sex VARCHAR(4) DEFAULT '男')DEFAULT CHARSET=utf8;
MySQL数据库 - 数据库和表的基本操作(一)
第1关:查看表结构与修改表名
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 ##########
第2关:修改字段名与字段数据类型
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 MODIFY Name varchar(30);########## End ##########DESCRIBE tb_emp;
第3关:添加与删除字段
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;
第4关:修改字段的排列位置
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;
第5关:删除表的外键约束
USE Company;#请在此处添加实现代码########## Begin #################### delete the foreign key ##########ALTER TABLE tb_emp DROP FOREIGN KEY emp_dept;########## End ##########SHOW CREATE TABLE tb_emp \G;
MySQL数据库 - 数据库和表的基本操作(二)
第1关:插入数据
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;########## End ##########
第2关:更新数据
USE Company;#请在此处添加实现代码########## Begin #################### update the value ##########UPDATE tb_empSET Name="Tracy",DeptId=302,Salary=4300.00WHERE id=3;########## End ##########SELECT * FROM tb_emp;########## End ##########DESCRIBE tb_emp;
第3关:删除数据
USE Company;#请在此处添加实现代码########## Begin #################### delete the value ##########DELETE FROM tb_empWHERE Salary>3000;########## End ##########SELECT * FROM tb_emp;########## End ##########DESCRIBE tb_emp;
MySQL数据库 - 单表查询(一)
第1关:基本查询语句
USE Company;#请在此处添加实现代码########## Begin #################### retrieving the Name and Salary ##########select Name,Salary from tb_emp;########## retrieving all the table ##########select * from tb_emp;########## End ##########
第2关:带 IN 关键字的查询
USE Company;#请在此处添加实现代码########## Begin #################### retrieving the Name and Salary with IN statement ##########SELECT Name,Salary FROM tb_emp WHERE Id NOT IN (1);########## End ##########
第3关:带 BETWEEN AND 的范围查询
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 ##########
MySQL数据库 - 单表查询(二)
第1关:带 LIKE 的字符匹配查询
USE Company;######### Begin #########SELECT Name,Salary FROM tb_emp WHERE Name LIKE "C%";######### End #########
第2关:查询空值与去除重复结果
USE Company;######### Begin #########SELECT * FROM tb_emp WHERE DeptId IS NULL;######### End ################## Begin #########SELECT DISTINCT Name FROM tb_emp;######### End #########
第3关:带 AND 与 OR 的多条件查询
USE Company;######### Begin #########SELECT * FROM tb_emp WHERE DeptId=301 AND Salary > 3000;######### End ################## Begin #########SELECT * FROM tb_emp WHERE DeptId=301 OR DeptId=303;######### End #########
MySQL数据库 - 单表查询(三)
第1关:对查询结果进行排序
USE School;#请在此处添加实现代码########## Begin #################### 查询1班同学的所有信息以成绩降序的方式显示结果 ##########select * from tb_score where class_id = 1 order by score desc;########## End ##########
第2关:分组查询
USE School;#请在此处添加实现代码########## Begin #################### 对班级名称进行分组查询 ##########SELECT * FROM tb_class GROUP BY class_id;########## End ##########
第3关:使用 LIMIT 限制查询结果的数量
USE School;#请在此处添加实现代码########## Begin #################### 查询班级中第2名到第5名的学生信息 ##########SELECT * FROM tb_score order by score desc LIMIT 1,4;########## End ##########
MySQL数据库 - 连接查询
第1关:内连接查询
USE School;########## 查询数据表中学生姓名和对应的班级 ###########请在此处添加实现代码########## Begin ##########select tb_student.name as studentName,tb_class.name as className from tb_student join tb_class on tb_class.id = tb_student.class_id; ########## End ##########
第2关:外连接查询
USE School;########## 使用左外连接查询所有学生姓名和对应的班级 ###########请在此处添加实现代码########## Begin ##########select tb_student.name as studentName,tb_class.name as classNamefrom tb_class right join tb_student on tb_class.id=tb_student.class_id;########## End #################### 使用右外连接查询所有学生姓名和对应的班级 ##########select tb_student.name as studentName,tb_class.name as classNamefrom tb_class left join tb_student on tb_class.id=tb_student.class_id;#请在此处添加实现代码########## Begin #################### End ##########
第3关:复合条件连接查询
USE School;########## 查询所有班级里分数在90分以上的学生的姓名和学生的成绩以及学生所在的班级 ###########请在此处添加实现代码########## Begin ##########select s1.name as studentName,score,s2.name as className from tb_student as s1,tb_class as s2 where s1.class_id=s2.id ands1.score>90 order by score desc;########## End ##########
MySQL数据库 - 子查询
第1关:带比较运算符的子查询
USE Company;#请在此处添加实现代码########## Begin ###########1.查询大于所有平均年龄的员工姓名与年龄select name,age from tb_emp where age>(select avg(age) from tb_emp);########## End ##########
第2关:关键字子查询
USE Company;#请在此处添加实现代码########## Begin ###########1.使用 ALL 关键字进行查询SELECT position,salary FROM tb_salary WHERE salary > ANY(SELECT max(salary) FROM tb_salary where position="java");#2.使用 ANY 关键字进行查询SELECT position,salary FROM tb_salary WHERE salary > ANY(SELECT min(salary) from tb_salary where position="java");#3.使用 IN 关键字进行查询select position,salary from tb_salary where position in("java");########## End ########## MySQL数据库 - 复杂查询(一)
第1关:交换工资
#请在此添加实现代码########## Begin ##########UPDATE tb_SalarySETsex = CASE sex WHEN "m" THEN "f"ELSE "m"END;########## End ##########
第2关:换座位
#请在此添加实现代码########## Begin ##########SELECT if(Id%2=0,Id-1,if(Id=5,Id,Id+1)) AS id,nameFROM tb_Seat ORDER BY Id;########## End ##########
第3关:分数排名
#请在此添加实现代码########## Begin ##########select Score,(select count(distinct score) from score where score >=s.score) as Rankfrom score as s order by Score desc;select Score,(select count(*) from score as s2 where s2.score >s1.score)+1 as Rankfrom score as s1 order by Rank;########## End ##########
第4关:体育馆的人流量
#请在此添加实现代码########## Begin ##########select distinct a.* from gymnasium a,gymnasium b,gymnasium cwhere a.visitors_flow>=100 and b.visitors_flow>=100and c.visitors_flow>=100and( (a.id = b.id-1 and b.id = c.id - 1)or (a.id = b.id-1 and a.id = c.id + 1)or (a.id = b.id+1 and b.id = c.id + 1))order by a.id;########## End ##########
第5关:统计总成绩
#请在此添加实现代码########## Begin ##########select t1.classname,t1.chinese,t2.mathsfrom(select c.classname classname,sum(s.chinese)chinese from tb_class c,tb_score s where c.stuname=s.name and s.chinese>=60 group by c.classname)t1,(select c.classname classname,sum(s.maths)maths from tb_class c,tb_score swhere c.stuname=s.name and s.maths>=60 group by c.classname)t2where t1.classname=t2.classname;########## End ##########
MySQL数据库 - 复杂查询(二)
第1关:查询学生平均分
#请在此添加实现代码########## Begin ##########select b.s_id,b.s_name,ROUND(AVG(a.s_score),2)as avg_score from student b inner join score a on b.s_id = a.s_idGROUP BY b.s_id,b.s_name HAVING avg_score
上一篇:攻破宝塔面板怎么打开?
下一篇:安卓怎么获得服务器?