MYsql命令练习11-18

mysql命令练习题3
代码块
-- 1.创建test数据库
CREATE DATABASE test CHARACTER set utf8

use test

-- 2.在test数据库中创建emp表,表结构如下图所示(id设置为主键自增长)
CREATE table emp(
    empno int(11) PRIMARY key auto_increment not null,
    ename VARCHAR(50),
    job VARCHAR(50),
    mgr int DEFAULT null,
    hiredate date,
    sal DECIMAL(7,2),
    comm DECIMAL(7,2) DEFAULT null,
    deptno int(11)
)

-- 3.向emp表中添加记录如下图所示
insert into emp VALUES(1001,"甘宁","文员",1013,"2000-12-17",8000.00,DEFAULT,20),
        (1002,"黛绮丝","销售员",1006,"2001-02-20",16000.00,3000.00,30),
        (1003,"殷天正","销售员",1006,"2001-02-22",12500.00,5000.00,30),
        (1004,"刘备","经理",1009,"2001-04-02",29750.00,DEFAULT,20),
        (1005,"谢逊","销售员",1006,"2001-09-28",12500.00,14000.00,30),
        (1006,"关羽","经理",1009,"2001-05-01",28500.00,DEFAULT,30),
        (1007,"张飞","经理",1009,"2001-09-01",24500.00,DEFAULT,10),
        (1008,"诸葛亮","分析师",1004,"2007-04-19",30000.00,DEFAULT,20),
        (1009,"曾阿牛","董事长",DEFAULT,"2001-11-17",50000.00,DEFAULT,10),
        (1010,"韦一笑","销售员",1006,"2001-09-08",15000.00,0.00,30),
        (1011,"周泰","文员",1008,"2007-05-23",11000.00,DEFAULT,20),
        (1012,"程普","文员",1006,"2001-12-03",9500.00,DEFAULT,30),
        (1013,"庞统","分析师",1004,"2001-12-03",30000.00,DEFAULT,20),
        (1014,"黄盖","文员",1007,"2002-01-23",13000.00,DEFAULT,10),
        (1015,"张三","文员",1007,"2002-01-23",53000.00,DEFAULT,50)

-- 1查询表中所有内容(10分)
select * from emp

-- 2.查询表中姓名是张三的所有消息记录(10分)
select * from emp  where ename ="张三"

-- 3.查询表中姓名是三个字组成的所有员工的ename,job,sal字段的对应信息(10分)
select * from emp where ename like '___'
select * from emp where ename like '曾%'
-- 4.查询表中empno字段从1004至1008所有员工的记录(10分)
select * from emp where mgr >1003 and mgr <1009

-- 5.查询表中所有job字段是文员并且姓名是黄盖的员工的所有信息(10分)
select * from emp where job ="文员" and  ename ="黄盖"

-- 6.查询表中在2001年以后入职的员工信息(10分)
select * from emp where hiredate  > "2001-1-1"

-- 7.查询表中奖金(COMM)是NULL的员工信息(10分)

select * from emp where comm is null;

mysql命令练习题11
代码块
create table student(
        id int PRIMARY KEY auto_increment,
        name VARCHAR(20) not null,
        score int not null,
        address VARCHAR(50) not null,
        useremail varchar(20) not null
)
insert into student values(1,'张三',98,'北京','111111@qq,com'),
(2,'李四',88,'上海','[email protected]'),(3,'王五',78,'广州','[email protected]'),
(4,'赵六',68,'深圳','[email protected]'),(5,'孙七',58,'杭州','[email protected]'),
(6,'小红',48,'北京','[email protected]'),(7,'小黑',99,'上海','[email protected]'),
(8,'小绿',100,'杭州','[email protected]'),(9,'小粉',60,'杭州','[email protected]'),
(10,'小紫',70,'黑龙江','[email protected]')

4.  使用sql语句查询出表中的所有内容
SELECT * FROM student 
5.  使用sql语句查询出表中所有score>70的同学的id,name,score
SELECT * FROM student WHERE score>70

6.  更改name字段的数据类型为varchar(50)
    ALTER TABLE student MODIFY name varchar(50)

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

8.  更改姓名是张三的同学的分数为88
    UPDATE student set score= score -10 WHERE id =1

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

10. 把姓名是“小红”的同学的分数在原来的基础上+40
    UPDATE student set score = score +40 WHERE id =6

11. 使用关键字in,查询id值是1或5或7的同学的基本信息
        SELECT * FROM student WHERE id =1 or id =5 or id=7
SELECT * FROM student WHERE id in("1","5","7")

12. 查询id值在5至8的所有同学的基本信息
 SELECT * FROM student WHERE id between 5 and 8
 SELECT * FROM student WHERE  id>4 and id<9

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 is NOT null

16. 查询score字段值不是NULL的同学的id和name
SELECT id,name FROM student WHERE score is NOT null 
mysql命令练习题12
代码块
create table studentx(
        id int PRIMARY KEY auto_increment,
        name VARCHAR(20) not null,
        score int not null,
        address VARCHAR(50) not null,
        useremail varchar(20) not null
)
insert into studentx values(1,'张三',98,'北京','111111@qq,com'),
(2,'李四',88,'上海','[email protected]'),(3,'王五',78,'广州','[email protected]'),
(4,'赵六',68,'深圳','[email protected]'),(5,'孙七',58,'杭州','[email protected]'),
(6,'小红',48,'北京','[email protected]'),(7,'小黑',99,'上海','[email protected]'),
(8,'小绿',100,'杭州','[email protected]'),(9,'小粉',60,'杭州','[email protected]'),
(10,'小紫',70,'黑龙江','[email protected]')


4.  使用sql语句查询出表中id,name,和address字段的所有内容
     SELECT id,name,address FROM studentx

5.  使用sql语句查询出表中所有同学的id,name,score
     SELECT id,name,score FROM studentx

6.  更改useremail字段的数据类型为varchar(50)
   ALTER TABLE studentx MODIFY useremail varchar(50)

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

8.  更改姓名是张三的同学的分数为92
   UPDATE studentx set score = score-6 WHERE id =1
    SELECT * FROM studentx

9.  如果80分为及格线,查询出所有不及格的同学的详细信息
   SELECT * FROM studentx WHERE score <80 

10. 把姓名是“小红”的同学的分数在原来的基础上+20
        UPDATE studentx set score=score+20 WHERE name= "小红"

11. 使用关键字in,查询id值是1或5或7的同学的基本信息
    SELECT * FROM studentx WHERE id in("1","5","7")

12. 查询id值在4至9的所有同学的基本信息
        SELECT * FROM studentx WHERE id BETWEEN 4 and 9

13. 查询姓名是小红并且分数大于60的同学的基本信息
        SELECT * FROM studentx WHERE name="小红" and score >60

14. 查询姓名是小红或者分数大于90的同学的基本信息
    SELECT * FROM studentx WHERE name="小红" or score >90

15. 查询score字段值是NULL的同学的基本信息
        SELECT * FROM studentx WHERE score is NOT null

16. 查询name不是张三的同学的id,name,和score
        SELECT id, name,score FROM studentx WHERE name!="张三" 
mysql命令练习题13
代码块
create table studentxx(
        id int PRIMARY KEY auto_increment,
        name VARCHAR(20) not null,
        score int not null,
        address VARCHAR(50) not null,
        useremail varchar(20) not null
)
insert into studentxx values(1,'张三',98,'北京','111111@qq,com'),
(2,'李四',88,'上海','[email protected]'),(3,'王五',78,'广州','[email protected]'),
(4,'赵六',68,'深圳','[email protected]'),(5,'孙七',58,'杭州','[email protected]'),
(6,'小红',48,'北京','[email protected]'),(7,'小黑',99,'上海','[email protected]'),
(8,'小绿',100,'杭州','[email protected]'),(9,'小粉',60,'杭州','[email protected]'),
(10,'小紫',70,'黑龙江','[email protected]')

4.  使用sql语句查询出表中的所有内容
        SELECT * FROM studentxx

5.  使用sql语句查询出表中所有同学的id,name,score
        SELECT id,name,score FROM studentxx

6.  更改name字段的数据类型为varchar(50)
        ALTER TABLE studentxx MODIFY name varchar(50)

7.  向表中添加一个字段,字段名称为“pingjia”,字段类型为varchar(50)
        ALTER TABLE studentxx add pingjia varchar(50)

8.  更改姓名是小紫的同学的分数为100
        UPDATE studentxx set score = score+30 WHERE name = "小紫"
    
9.  如果80分为及格线,查询出所有及格的同学的详细信息
        SELECT * FROM studentxx WHERE score>80
        
10. 把姓名是“小红”的同学的分数在原来的基础上+30
        UPDATE studentxx set score = score+30 WHERE name = "小红"

11. 使用关键字in,查询id值是2或4或8的同学的基本信息
        SELECT * FROM studentxx WHERE id in("2","4","8")
        
12. 查询id值在5至8的所有同学的基本信息
        SELECT * FROM studentxx WHERE id BETWEEN 5 and 8
        
13. 查询姓名是小红并且分数大于60的同学的基本信息
        SELECT * FROM studentxx WHERE name="小红" AND score>60

14. 查询姓名是小红或者分数大于90的同学的基本信息
        SELECT * FROM studentxx WHERE name="小红" or score>90

15. 查询score字段值是不为NULL的同学的基本信息
        SELECT * FROM studentxx WHERE score is not null

16. 查询id值在6至8的同学的name和score
        SELECT id,name,score FROM studentxx WHERE id BETWEEN 6 AND 8
mysql命令练习题14
代码块
create table studenta(
        id int PRIMARY KEY auto_increment,
        name VARCHAR(20) not null,
        score int not null,
        address VARCHAR(50) not null,
        useremail varchar(20) not null
)
insert into studenta values(1,'张三',98,'北京','111111@qq,com'),
(2,'李四',88,'上海','[email protected]'),(3,'王五',78,'广州','[email protected]'),
(4,'赵六',68,'深圳','[email protected]'),(5,'孙七',58,'杭州','[email protected]'),
(6,'小红',48,'北京','[email protected]'),(7,'小黑',99,'上海','[email protected]'),
(8,'小绿',100,'杭州','[email protected]'),(9,'小粉',60,'杭州','[email protected]'),
(10,'小紫',70,'黑龙江','[email protected]')

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

5.  使用sql语句查询出表中所有同学的id,name,score
        SELECT id,name,score from studenta
6.  更改useremail字段的数据类型为varchar(50)
        ALTER TABLE studenta MODIFY useremail varchar(50)

7.  向表中添加一个字段,字段名称为“pingjia”,字段类型为varchar(20)
        ALTER TABLE studenta add  pingjia varchar(50)

8.  更改姓名是李四的同学的分数为48
        UPDATE studenta set  score =48 WHERE name ="李四"

9.  如果80分为及格线,查询出所有不及格的同学的详细信息
        SELECT * from studenta WHERE score< 80 

10. 把姓名是“小红”的同学的分数在48的基础上+40
        UPDATE studenta set score= score+40 WHERE name="小红"

11. 使用关键字in,查询id值是3或9或10的同学的基本信息
        SELECT * from studenta WHERE id in("3","9","10")

12. 查询id值在2至6的所有同学的基本信息
        SELECT * from studenta WHERE id BETWEEN 2 and 6

13. 查询姓名是小红并且分数大于60的同学的基本信息
        SELECT * from studenta WHERE name="小红" and  score>60

14. 查询姓名是张三或者分数大于90或者address字段值是杭州的同学的基本信息
        SELECT * from studenta WHERE name="张三" or score>90 or address ="杭州"

15. 查询score字段值是NULL的同学的基本信息
        SELECT * from studenta WHERE score is not null 

16. 查询score的值在68-80的同学的id和name
        SELECT id,name,score from studenta WHERE score>67 and score<81
mysql命令练习题15
代码块
-- 创建db_test数据库

CREATE DATABASE db_testx CHARSET='utf8'
use db_testx

-- 在test数据库中创建teacher表,表结构如下图所示(id设置为主键自增长)
CREATE TABLE teacher(
Number int PRIMARY KEY auto_increment,
Name VARCHAR(30),
Sex enum('男','女'),
DepNo int,
Salary int,
Address VARCHAR(30) 
)

-- 向teacher表中添加记录如下图所示

INSERT INTO teacher VALUES(201,'刘芸','女',604,4500,'河北省沧州'),
(202,'周星驰','男',601,1600,'山东省济南'),
(203,'秦牧之','男',604,2800,'北京昌平'),
(204,'周润发','男',602,3500,'河北省沧州'),
(205,'林双','女',605,1200,'河北省保定'),
(206,'张帆','女',602,3000,'北京市昌平'),
(207,'陈友凉','男',604,2800,'山西省大同'),
(208,'张国荣','男',601,2500,'山东省烟台')

-- 修改表名为”TS”
alter table teacher rename ts

-- 向表中添加记录,字段对应值分别为(209,毛不易,男,604,4000,黑龙江齐齐哈尔)
insert into ts(Number,Name,Sex,DepNo,Salary,Address) values(201,"刘芸","女",604,4500,"河北省沧州")

-- 查询TS表中所有记录
select * from ts


-- 删除表中Number是201并且sex是女的教师信息
DELETE FROM ts WHERE Number = 201 and Sex ="女"

-- 查询表中Number字段的值是202,205或207教师的所有记录
select * from ts WHERE Number in('202','205','207')

-- 修改表中Number值是202教师的姓名为“马云”
update ts set Name ="马云" where Number ="202" and Name = "周星驰"

-- 按照工资对员工信息进行降序排序
select * from ts  order by Salary desc, Name asc;

mysql命令练习题16
代码块
-- 在test数据库中创建teacher表,表结构如下图所示(id设置为主键自增长)
CREATE TABLE teacher(
Number int PRIMARY KEY auto_increment,
Name VARCHAR(30),
Sex enum('男','女'),
DepNo int,
Salary int,
Address VARCHAR(30) 
)

-- 向teacher表中添加记录如下图所示
INSERT INTO teacher VALUES(201,'刘芸','女',604,4500,'河北省沧州'),
(202,'周星驰','男',601,1600,'山东省济南'),
(203,'秦牧之','男',604,2800,'北京昌平'),
(204,'周润发','男',602,3500,'河北省沧州'),
(205,'林双','女',605,1200,'河北省保定'),
(206,'张帆','女',602,3000,'北京市昌平'),
(207,'陈友凉','男',604,2800,'山西省大同'),
(208,'张国荣','男',601,2500,'山东省烟台')

-- 修改表名为”TS”
alter table teacher rename tsx

-- 向表中添加记录,字段对应值分别为(209,林青霞,女,604,4000,黑龙江齐齐哈尔)
insert into tsx(Number,Name,Sex,DepNo,Salary,Address) values (209,"林青霞","女",604,4000,"黑龙江齐齐哈尔")

-- 查询TS表中Number,Name,Sex字段对应的记录
select Number,Name,Sex from tsx 


-- 删除表中Number是201或者Name是陈友谅的教师信息
delete from tsx  where Number=201 or Name ="陈友凉"


-- 查询表中Number字段的值从202至206教师的所有记录
select * from tsx where Number >201 and Number <207
select * from tsx where Number BETWEEN 202 and 206
-- 修改表中Name值是张国荣教师的性别为“女”
update tsx set Sex = "女"  where Name="张国荣" and Sex="男"

-- 按照工资对员工信息进行升序排序
select * from tsx  order by Salary asc;

mysql命令练习题17
代码块
CREATE TABLE teacher(
Number int PRIMARY KEY auto_increment,
Name VARCHAR(30),
Sex enum('男','女'),
DepNo int,
Salary int,
Address VARCHAR(30) 
)

3.向teacher表中添加记录如下图所示
INSERT INTO tsxx VALUES(201,'刘芸','女',604,4500,'河北省沧州'),
(202,'周星驰','男',601,1600,'山东省济南'),
(203,'秦牧之','男',604,2800,'北京昌平'),
(204,'周润发','男',602,3500,'河北省沧州'),
(205,'林双','女',605,1200,'河北省保定'),
(206,'张帆','女',602,3000,'北京市昌平'),
(207,'陈友凉','男',604,2800,'山西省大同'),
(208,'张国荣','男',601,2500,'山东省烟台')

alter table teacher rename TSxx

6.向表中添加记录,字段对应值分别为(209,林青霞,女,604,4000,黑龙江齐齐哈尔)
insert into tsxx VALUES (209,"林青霞","女",604,4000,"黑龙江齐齐哈尔")

select * from tsxx
7.向表中添加字段Hobby,设置类型为varchar(50),设置唯一约束
ALTER TABLE tsxx ADD  Hobby varchar(50) UNIQUE 

8.使用desc语句查看teacher表的表结构
  DESC tsxx


9.查询表中Number字段的值从202至206教师的所有记录
    select * from tsxx WHERE Number>201 and Number<207
    select * from tsxx WHERE Number between 202 and 206

10.修改表中Name值是张国荣教师的性别为“女”
    UPDATE tsxx SET     Sex = "女" WHERE Name = "张国荣" and Sex="男"

11.删除表中Sex是“男”或者Name是刘芸的记录
 delete from tsxx WHERE Name="刘芸" or Sex = "男"

mysql命令练习题18
代码块
2.在test数据库中创建teacher表,表结构如下图所示(id设置为主键自增长)
CREATE TABLE teacher(
Number int PRIMARY KEY auto_increment,
Name VARCHAR(30),
Sex enum('男','女'),
DepNo int,
Salary int,
Address VARCHAR(30) 
)

3.向teacher表中添加记录如下图所示
INSERT INTO teacher VALUES(201,'刘芸','女',604,4500,'河北省沧州'),
(202,'周星驰','男',601,1600,'山东省济南'),
(203,'秦牧之','男',604,2800,'北京昌平'),
(204,'周润发','男',602,3500,'河北省沧州'),
(205,'林双','女',605,1200,'河北省保定'),
(206,'张帆','女',602,3000,'北京市昌平'),
(207,'陈友凉','男',604,2800,'山西省大同'),
(208,'张国荣','男',601,2500,'山东省烟台')


5.修改表名为”TEACHER”
ALTER TABLE teacher rename TEACHERX

6.向表中添加记录,字段对应值分别为(209,林青霞,女,604,4000,黑龙江齐齐哈尔)
    INSERT INTO TEACHERX VALUES(209,"林青霞","女",604,4000,"黑龙江齐齐哈尔")

7.向表中添加字段Hobby,设置类型为varchar(50),设置唯一约束
    select * from TEACHERX
ALTER TABLE TEACHERX add Hobby varchar(50) UNIQUE


8.修改DepNo字段的类型为varchar(20)
   alter table TEACHERX change DepNo DepNo varchar(20)

9.查询表中Number字段的值从202至206教师的所有记录
 select * from TEACHERX WHERE Number BETWEEN 202 and 206
SELECT * from TEACHERX WHERE Number>201 and Number<207
10.修改表中Name值是张国荣教师的性别为“女”
    UPDATE TEACHERX set Sex ="女" WHERE Name ="张国荣" 
    
11.删除表中Number是203或205或207的教师的记录
DELETE FROM TEACHERX WHERE Number in("203","205","207") 
第二题
代码块
-- 学生
create table student(
    sno varchar(20) primary key,
    sname  varchar(10),
    ssex varchar(10),
    sbirthday datetime,
    class varchar(20)
        )

-- 课程
create table course(
    cno varchar(20),    
        cname varchar(20) not null,
        tno varchar(20) not null,
        foreign key(tno) references teacher(tno)
        );

-- 老师
create table teacher(
        tno varchar(20) primary key,
        tname varchar(20) not null,
        tsex varchar(20) not null,
        tbirthday datetime,
        pro varchar(20) not null,
        depart varchar(20) not null
        )

-- 成绩
create table score(
        sno varchar(20) not null,
        cno varchar(20) not null,
        degree decimal
        
        )



select * from student;

select * from teacher;

select * from course;

select * from score;

student 数据

insert into student values
('108','曾华','男','1977-09-01','95033'),
("105","框玥","男",'1977-09-01','95031'),
("107","王丽","女",'1977-09-01','95033'),
("109","李军","男",'1977-09-01','95033'),
("103","王芳","女",'1977-09-01','95031'),
("104","陆君","男",'1977-09-01','95031')

select * from student




score数据

103 3-245   86
109 6-166   81
105 1-105   79
104 1-105   89
106 6-166   89
107 3-245   64
102 3-105   89
101 3-105   89

insert into score values('103','3-245',86),('109','6-166',81),
('105','1-105',79),('104','1-105',79),('106','6-166',89),
('107','3-245',64),('102','3-105',89),('101','3-105',89)


teacher数据

804 李诚  男   1958-12-02 00:00:00 副教授 计算机系
856 张旭  男   1969-03-13 00:00:00 讲师  电子工程系
825 王萍  女   1972-05-05 00:00:00 助教  计算机系
831 刘冰  女   1977-08-14 00:00:00 助教  电子工程系


insert into teacher values('804','李诚','男','1958-12-02 00:00:00','副教授','计算机系'),
                                                    ('856','张旭','男','1969-03-13 00:00:00','讲师','电子工程系'),
                                                    ('825','王萍','女','1972-05-05 00:00:00','助教','计算机系'),
                                                    ('831','刘冰','女','1977-08-14 00:00:00','助教','电子工程系')

course数据


3-105   计算机导论825
3-245   操作系统    804
6-166   数字电路    856
9-888   高等数学    831

insert into course values('3-105','计算机导论','825'),
                                                    ('3-245','操作系统','804'),
                                                    ('6-166','数字电路','856'),
                                                    ('9-888','高等数学','831')





-简单的查询练习:- 1-
1,查询student表中的所有记录
select * from student

2,检查student表中的所有记录的sname,Ssex和class。
select sname,ssex,class from student

3、查询老师所有的单位即不重复的depart列
select * from teacher group by depart

4.查询socre表中成绩在从68到80之间的记录。
select * from score where degree between 68 and 80

5.查询score表成绩中为85、86或88。-
select * from score where degree in (85,86,88)

6、查询student表中“95031”班或性别为“女”同学的同学记录
select * from student where class = 95031 or ssex = '女'

7.以class降序查询student表的所有记录
select * from student order by class desc

8.以cno升序。degree降序查询score表的所有记录
select * from score order by cno asc,degree desc

9、查询“95031”班学生人数。
select count(*) from student where class = 95031

10、查询score表中的最高分的学生学号和课程号(子查询或者排序)
select sno,cno from score where degree = (select max(degree) from score)


--  11.查看每门课的平均成绩 (分组查询)
select cno,sum(degree)/count(degree) from score group by cno


--  12.查询score表中至少有2名同学选修的并以3开头的课程的平均分数 having(组级过滤)
select cno,avg(degree) from score where cno like '3%' group by cno having count(cno) > 1


-- 13.查询分数大于70,小于90的sno列(条件查询)
select sno from score where degree between 70 and 90



-- 14.查询所有学生的 sname,con,和degree 列。(多表查询)
select stu.sname,s.cno,s.degree from score as s inner join student as stu on s.sno = stu.sno



--  15.查询所有学生的sno,cname和degree
select s.sno,s.degree,c.cname from score as s inner join course as c on s.cno = c.cno



-- 16. 查询所有学生的sname,cname和degree(三表联查)
select stu.sname,c.cname,s.degree from student as stu inner join course as c inner join score as s on stu.sno = s.sno and s.cno = c.cno



17.查阅"95031"班学生每门课的平均分
select cno,AVG(degree) from score where sno in (select sno from student where class = '95031') group by cno


-- 18.查询选修”3-105“课程的 成绩高于”109“号同学成绩的所有同学的记录

-- 子查询的练习

-- 1.先查询”109“号同学成绩
select degree from score where sno = 109


-- 2.第二步找到3-105课程成绩高于109
select * from score where cno = '3-105' and degree > (select degree from score where sno = 109)




-- 19。查询成绩高于学号109,课程号为3-105的成绩的所有记录 子查询
select * from score where cno = '3-105' and degree > (select degree from score where sno = 109)



-- 20.查询和学号为108.101的同学同年出生的所有学生的son。sname和sbirthday
-- year的函数与带in 关键字的子查询
select sbirthday from student where sno in (108,101)
select sno,sname,sbirthday from student where year(sbirthday) = (select year(sbirthday) from student where sno in (108,101))

--21. 查询”张旭“教师任课的学生成绩

-- 写这个的时候先理清表之间的关系  想好我们到底要查的是什么数据 在对表中的关系进行处理

-- 1.先查询教师的id号
select tno from teacher where tname = '张旭'

-- 2.根据教师的id号来查询课程id
select cno from course where tno = (select tno from teacher where tname = '张旭')

-- 3。查询score表根据课程号查询学生的分数
select degree from score where cno = (select cno from course where tno = (select tno from teacher where tname = '张旭'))

第10题
代码块
1、功能描述

    1 创建一个班级表(classes),包含字段id,name
    
    2创建一个学生表(students),包含字段id,name,birthday,gender,score,class_id 

3 在班级表中添加三条记录 id:1 name:一年级,id:2 name:二年级,id:3 name:三年级 
    4 在员工表里添加六条记录,

id:1 name:’张三’ birthday:”2010-11-10” gender:’男’ score:100 class_id:1
id:2 name:’李四’ birthday:”2012-11-10” gender:’女’ score:75 class _id:1
id:3 name:’王五’ birthday:”2014-11-10” gender:’男’ score:80 class _id:2
id:4 name:’小明’ birthday:”2013-11-10” gender:’女’ score:60 class _id:2
id:5 name:’小李’ birthday:”2015-11-10” gender:’女’ score:30 class _id:2
id:6 name:’小刘’ birthday:”2008-11-10” gender:’男’ score:90 class _id:3


5 使用pymysql模块来查询每个班级的平均分

如 一年级  100
   二年级  80
   三年级   70
6 修改张三的名字为张五

7 删除班级名字为”三年级”的所有学生

8 查询一年级分数最高的人

9 查询有学生的年级信息(用子查询实现)

10 查询以‘小’开头的名字为一个字学生

11查询出生日期在2008-11-10到2012-11-10的学生


CREATE database db_test10

use db_test10

-- 创建班级表
CREATE TABLE classes (id int PRIMARY key auto_increment,name VARCHAR(20) not null)

-- 创建学生表
CREATE TABLE stu (
id int PRIMARY key auto_increment,
name VARCHAR(20) not null,
birthday date,
gender VARCHAR(20),
score int ,
class_id int)


-- 在班级表中添加数据
insert  into  classes values
(1,"一年级"),
(2,"二年级"),
(3,"三年级")


-- 在学生表中添加数据

insert into stu VALUES
(1 ,"张三","2010-11-10","男",100,1),
(2 ,"李四","2012-11-10","女",75,1),
(3 ,"王五","2014-11-10","男",80,2),
(4 ,"小明","2013-11-10","女",60,2),
(5 ,"小李","2015-11-10","女",30,2),
(6 ,"小刘","2008-11-10","男",90,3)

INSERT into stu VALUES(7 ,"小刘1","2008-11-10","男",90,3)

-- 添加外键,需要使用foreign key关键字
alter table stu add foreign key(class_id) references classes(id)


-- 5 使用pymysql模块来查询每个班级的平均分

-- 如 一年级  100
--    二年级  80
--    三年级   70
SELECT cla.name,avg(stu.score)as 平均分 from  stu  INNER JOIN classes as cla ON stu.class_id=cla.id GROUP BY  cla.name 


-- 6 修改张三的名字为张五
UPDATE stu set name='张五' WHERE name='张三'

SELECT* FROM stu
-- 7 删除班级名字为”三年级”的所有学生
DELETE from stu WHERE class_id=(SELECT id FROM classes WHERE name="三年级")

SELECT * FROM stu 
-- 8 查询一年级分数最高的人
SELECT stu.name,MAX(score) FROM stu WHERE class_id=(SELECT id FROM classes WHERE name="一年级")

SELECT * FROM stu 

-- 9 查询有学生的年级信息(用子查询实现)
SELECT id,name  from classes WHERE id in(SELECT class_id from  stu)

-- 10 查询以‘小’开头的名字为一个字学生
SELECT * from stu WHERE name like "小%"

-- 11查询出生日期在2008-11-10到2012-11-10的学生
SELECT * from stu WHERE birthday  BETWEEN "2008-11-10" and "2012-11-10"


---------------------------------------------------------------------------

1、  功能描述
    1 创建一个分类表(cate),包含字段id,name
2创建一个商品表(goods),包含字段id,name,number,create_time,price,cate_id 

3 在分类表中添加三条记录 id:1 name:服装,id:2 name:玩具,id:3 name:家电

4 在商品表里添加六条记录,
id:1 name:’羽绒服’ create_time:”2010-11-10” number:10 price:”1.00” cate_id:1
id:2 name:’轻便服’ create_time:”2012-11-10” number:30  price:”2.00” cate _id:1
id:3 name:’汽车’ create_time:”2014-11-10” number:40  price:”2.00” cate _id:2
id:4 name:’公主’ create_time:”2013-11-10” number:50  price:”6.00” cate _id:2
id:5 name:’橡皮泥’ create_time:”2015-11-10” number:23 price:”3.50” cate _id:2
id:6 name:’电饭煲’ create_time:”2008-11-10” number:3 price:”3.00” cate _id:3

5 使用pymysql模块来查询所有的分类及商品
6 把名字为‘羽绒服’修改成‘棉衣’
7 删除名字为‘电饭煲’的记录
8 查询‘服装’分类下面库存最多的商品信息
9 查询分类下有商品分类信息
10 查询以‘服’结束的商品信息
11查询创建日期在2008-11-10到2012-11-10的之间的商品

-------------------------------------------作业---------------------------------


CREATE database db_test102

use db_test102

1、  功能描述
1 创建一个分类表(cate),包含字段id,name
CREATE TABLE cate(id int PRIMARY key auto_increment,name VARCHAR(20) not null)


2创建一个商品表(goods),包含字段id,name,number,create_time,price,cate_id 
CREATE TABLE goods(
id int PRIMARY key auto_increment,
name VARCHAR(20)not null,
number int not null,
create_time date,
price VARCHAR(50),
cate_id int)


3 在分类表中添加三条记录 id:1 name:服装,id:2 name:玩具,id:3 name:家电
INSERT into cate VALUES(1,"服装"),(2,"玩具"),(3,"家电")

4 在商品表里添加六条记录,
id:1 name:’羽绒服’ create_time:”2010-11-10” number:10 price:”1.00” cate_id:1
id:2 name:’轻便服’ create_time:”2012-11-10” number:30  price:”2.00” cate _id:1
id:3 name:’汽车’ create_time:”2014-11-10” number:40  price:”2.00” cate _id:2
id:4 name:’公主’ create_time:”2013-11-10” number:50  price:”6.00” cate _id:2
id:5 name:’橡皮泥’ create_time:”2015-11-10” number:23 price:”3.50” cate _id:2
id:6 name:’电饭煲’ create_time:”2008-11-10” number:3 price:”3.00” cate _id:3

insert  into goods VALUES
(1,"羽绒服",10,"2010-11-10","1.00",1),
(2,"轻便服",30,"2012-11-10","2.00",1),
(3,"汽车"  ,40,"2014-11-10","2.00",2),
(4,"公主"  ,50,"2013-11-10","6.00",2),
(5,"橡皮泥",23,"2015-11-10","3.50",2),
(6,"电饭煲",3 ,"2008-11-10","3.00",3)

SELECT * from goods
5 使用pymysql模块来查询所有的分类及商品


6 把名字为‘羽绒服’修改成‘棉衣’
UPDATE  goods set name="棉衣"  WHERE name="羽绒服"

SELECT * from goods
7 删除名字为‘电饭煲’的记录
DELETE FROM goods WHERE name="电饭煲"

8 查询‘服装’分类下面库存最多的商品信息
SELECT *FROM goods WHERE number=(SELECT MAX(number)from goods WHERE cate_id=(SELECT id from cate WHERE name="服装"))

9 查询分类下有商品分类信息
SELECT *  from cate WHERE id in(SELECT cate_id from  goods)


10 查询以‘服’结束的商品信息
SELECT * from goods WHERE name LIKE "%服"

11查询创建日期在2008-11-10到2012-11-10的之间的商品
SELECT * from goods WHERE create_time BETWEEN "2008-11-10" and "2012-11-10"





1.创建一个数据库 cart
CREATE database db_test103

use db_test103

2.使用新创建的数据库,在数据库下建立两张表
用户表(users): id  姓名 身高 
create table users(id int primary key auto_increment,
                                    name varchar(20),
                                    height float(5,2))

购物车表(user_cart):id 商品名称  购买数量  总价  是否删除  用户id

create table user_cart(id int primary key auto_increment,
                                            name varchar(20),
                                            number int,
                                            price decimal(7,2),
                                            isdelete int,
                                            user_id int)

5.向用户表添加以下记录

1张三  1.80
2李四  1.75
3小明   1.34

insert into users values
(1,'张三',1.80),
(2,'李四',1.75),
(3,'小明',1.34)


6.在购物车表中加入以下记录(1代表未删除,0代表删除)

1  商品一   10  100  1  1
2  商品二   3   89   0  1
3  商品三   2   30   1  1
4  商品四  10   50   1  2
5  商品五   3   10   1  2
6  商品六   1   76   1  2


insert into user_cart values
(1,'商品一',10,100,1,1),
(2,'商品二',3,89,0,1),
(3,'商品三',2,30,1,1),
(4,'商品四',10,50,1,2),
(5,'商品五',3,10,1,2),
(6,'商品六',1,76,1,2)


7.更新李四的购物车中 商品五的名字为商品5
update user_cart set name = '商品5' where name = '商品五' and user_id = (select id from users where name = '李四')
 

8.查询身高大于1米   并且 购物车没删除  的  用户和购物车信息
select * from users as user inner join user_cart as cart on user.id = cart.user_id where user.height > 1 and cart.isdelete = 1


9.查询购物车中删除和没删除的总价和为多少
select sum(price) from user_cart


10.查询每个用户没删除的购物车的数量大于1的记录,并按降序排列,取三条记录
select * from user_cart where isdelete = 1 and number > 1 order by number desc limit 0,3


11.按是否删除分组,查询平均总价格大于10的用户和购物车信息--------------------------------------------------------错误

select * from user_cart as cart group by isdelete having sum(cart.price) / count(*) > 10


12.查询有购物车的用户和购物车信息
select * from users as user inner join user_cart as cart on user.id = cart.user_id


13.删除张三购物车中已删除的购物车信息
delete from user_cart where isdelete = 0 and user_id = (select id from users where name = '张三')










你可能感兴趣的:(MYsql命令练习11-18)