赞
踩
目录
第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;
第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 c
- hange 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;
第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_emp
- SET Name="Tracy",DeptId=302,Salary=4300.00
- WHERE id=3;
-
- ########## End ##########
-
- SELECT * FROM tb_emp;
-
- ########## End ##########
-
- DESCRIBE tb_emp;
第3关:删除数据
- USE Company;
-
- #请在此处添加实现代码
- ########## Begin ##########
-
- ########## delete the value ##########
- DELETE FROM tb_emp
- WHERE Salary>3000;
-
- ########## End ##########
-
- SELECT * FROM tb_emp;
-
- ########## End ##########
-
- DESCRIBE tb_emp;
第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 ##########
-
第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 #########
第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 ##########
第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 className
- from 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 className
- from 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 and
- s1.score>90 order by score desc;
- ########## End ##########
第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 ##########
第1关:交换工资
- #请在此添加实现代码
- ########## Begin ##########
- UPDATE tb_Salary
- SET
- sex = 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,name
- FROM tb_Seat ORDER BY Id;
- ########## End ##########
-
第3关:分数排名
- #请在此添加实现代码
- ########## 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 ##########
第4关:体育馆的人流量
- #请在此添加实现代码
- ########## Begin ##########
- select distinct a.* from gymnasium a,gymnasium b,gymnasium c
- where a.visitors_flow>=100 and b.visitors_flow>=100
- and c.visitors_flow>=100
- and(
- (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.maths
- from(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 s
- where c.stuname=s.name and s.maths>=60 group by c.classname)t2
- where t1.classname=t2.classname;
-
- ########## End ##########
第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_id
- GROUP BY b.s_id,b.s_name HAVING avg_score <60
- union
- select a.s_id,a.s_name,0 as avg_score from student a
- where a.s_id not in (select distinct s_id from score);
-
- ########## End ##########
第2关:查询修课相同学生信息
-
- #请在此添加实现代码
- ########## Begin ##########
- create view temp as(select s_id,group_concat(c_id)as c from score group by s_id);
- select * from student where s_id in(select s_id from temp where c=(select c from temp where s_id="01")and s_id<>"01");
- ########## End ##########
第3关:查询各科成绩并排序
- #请在此添加实现代码
- ########## Begin ##########
- select a.*,count(b.s_score)+1 rank from score a left join score b
- on a.c_id = b.c_id and a.s_score <b.s_score
- group by a.c_id,a.s_id
- order by a.c_id,count(b.s_score);
-
- ########## End ##########
第4关:查询张老师课程成绩最高的学生信息
- #请在此添加实现代码
- ########## Begin ##########
- select a.*,b.s_score,b.c_id,c.c_name from student a
- INNER JOIN score b ON a.s_id = b.s_id
- INNER JOIN course c ON b.c_id = c.c_id
- where b.c_id =
- (
- select c_id from course c,teacher d
- where c.t_id=d.t_id and d.t_name="张三"
- )
- and b.s_score in (select MAX(s_score)from score where c_id="02");
-
- ########## End ##########
第5关:查询两门课程不及格同学信息
- #请在此添加实现代码
- ########## Begin ##########
- select a.s_id,a.s_name,ROUND(AVG(b.s_score))
- avg_score from student a
- inner join score b on a.s_id = b.s_id
- where a.s_id in(
- select s_id from score where s_score<60
- GROUP BY s_id
- having count(*)>=2
- )
- GROUP BY a.s_id,a.s_name;
-
- ########## End ##########
第1关:COUNT( )函数
- USE School;
-
- #请在此处添加实现代码
- ########## Begin ##########
-
- ########## 查询该表中一共有多少条数据 ##########
- select count(*) from tb_class;
-
- ########## 查询此表中367班有多少位学生 ##########
- select classid,count(*) from tb_class
- where classid=367;
-
- ########## End ##########
第2关:SUM( )函数
- USE School;
-
- #请在此处添加实现代码
- ########## Begin ##########
-
- ########## 查询所有学生总分数 ##########
- select sum(score) from tb_class;
-
- ########## 查询学生语文科目的总分数 ##########
- select course,sum(score) from tb_class
- where course="语文";
-
- ########## End ##########
第3关:AVG( )函数
- USE School;
-
- #请在此处添加实现代码
- ########## Begin ##########
-
- ########## 查询学生语文科目的平均分数 ##########
- select course,avg(score)from tb_class
- where course="语文";
-
- ########## 查询学生英语科目的平均分数 ##########
- select course,avg(score) from tb_class
- where course="英语";
-
- ########## End ##########
第4关:MAX( )函数
- USE School;
-
- #请在此处添加实现代码
- ########## Begin ##########
-
- ########## 查询语文课程中的最高分数 ##########
- select course,max(score) from tb_class
- where course="语文";
-
- ########## 查询英语课程中的最高分数 ##########
- select course,max(score) from tb_class
- where course="英语";
-
- ########## End ##########
第5关:MIN( )函数
- USE School;
-
- #请在此处添加实现代码
- ########## Begin ##########
-
- ########## 查询语文课程中的最低分数 ##########
- select course,min(score) from tb_class
- where course="语文";
-
- ########## 查询英语课程中的最低分数 ##########
- select course,min(score) from tb_class
- where course="英语";
-
- ########## End ##########
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。