6.3作业

– 新建学生表,老师表,课程表,成绩表
create table student( sno varchar(20) primary key, sname varchar(10) not null, ssex varchar(10) not null, sbirthday datetime, class varchar(20));
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 course( cno varchar(20) primary key, cname varchar(20) not null, tno varchar(20) not null, foreign key(tno) references teacher(tno));
create table score( sno varchar(20) not null, cno varchar(20) not null, degree decimal, foreign key(sno) references student(sno), foreign key(cno) references course(cno), primary key(sno, cno));

– 添加学生表数据
insert into student values(“101”,“阿三”,“男”,‘1985-05-01’,‘91033’);
insert into student values(“102”,“阿四”,“男”,‘1985-05-01’,‘91031’);
insert into student values(“107”,“阿五”,“女”,‘1985-05-01’,‘91033’);
insert into student values(“109”,“阿六”,“男”,‘1985-05-01’,‘91033’);
insert into student values(“103”,“阿七”,“女”,‘1985-05-01’,‘91031’);
insert into student values(“104”,“阿九”,“男”,‘1985-05-01’,‘91031’);
insert into student values(“105”,“阿二”,“男”,‘1985-05-01’,‘91033’);
insert into student values(“106”,“阿一”,“男”,‘1985-05-01’,‘91033’);

– 添加老师数据
insert into teacher values(“804”,“张三”,“男”,‘1958-12-02’,“教授”,“计算机系”);
insert into teacher values(“856”,“李四”,“男”,‘1969-03-13’,“老师”,“安卓工程系”);
insert into teacher values(“825”,“王五”,“女”,‘1972-05-05’,“老师”,“计算机系”);
insert into teacher values(“831”,“小六”,“女”,‘1977-08-14’,“老师”,“安卓工程系”);

– 添加课程数据
insert into course values(“3-105”,“计算机”,“825”);
insert into course values(“3-245”,“java”,“804”);
insert into course values(“6-166”,“安卓”,“856”);
insert into course values(“9-888”,“大数据”,“831”);

– 添加成绩数据
insert into score values(“103”,“3-245”,86);
insert into score values(“109”,“6-166”,81);
insert into score values(“105”,“3-105”,79);
insert into score values(“104”,“3-105”,89);
insert into score values(“106”,“6-166”,89);
insert into score values(“107”,“3-245”,64);
insert into score values(“102”,“3-105”,89);
insert into score values(“101”,“3-105”,89);

– 1.查看所有数据
select * from student;select * from teacher;select * from course;select * from score;

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

– 3.查询老师所有的单位即不重复的depart列
select distinct depart from teacher;

– 4.查询socre表中成绩在从68到80之间的记录。
select * from score where degree between 60 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;#asc

– 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);
select sno,cno from score order by degree desc limit 0,1

;-- 11.查看每门课的平均成绩 (分组查询)
select * from course;select avg(degree) from score where cno="3-105"select avg(degree) from score where cno="1-105"select avg(degree) from score where cno=“6-166” select avg(degree) from score where cno="3-245"select degree from score where cno="3-105"select cno,avg(degree) from score group by cno;

– 12.查询score表中至少有2名同学选修的并以3开头的课程的平均分数 havimg(组级过滤)
select cno,avg(degree),count(*) from score group by cnohaving count(cno)>=2 and cno like ‘3%’;

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

– 14.查询所有学生的 sname,con,和degree 列。(多表查询)
select sno,sname from student;select cno,degree from score;select sname,cno,degree from student,scorewhere student.sno=score.sno;

– 15.查询所有学生的sno,cname和degreeselect cno,cname from course;
select cno,sno,degree from score;select sno,cname,degree from course,score where course.cno = score.cno;

– 16. 查询所有学生的sname,cname和degree(三表联查)
-sname => student-- – cname => course-- – degree => scoreselect sname,cname,degree from student,course,score where student.sno=score.sno and course.cno=score.cno;select sname,cname,degree,student.sno as stu_sno score.sno,course.cno as cou_cno,score.cno from student,course,scorewhere student.sno=score.sno and course.cno=score.cno;

– 17.查阅"95031"班学生每门课的平均分select

  • from student where class=“95031”;select * from score where sno in (select sno from student where class=“95031”);select cno,avg(degree) from scorewhere sno in (select sno from student where class=“95031”)group by cno;

– 18.查询选修”3-105“课程的成绩高于”109“号同学”3-105“成绩的所有同学的记录-- 子查询的练习-- 1.先查询”109“号同学”3-105“成绩
select degree from score where sno=‘109’ and cno=“3-105”;

– 2.第二步找到3-105课程成绩高于109select
from score where cno=‘3-105’ anddegree>(select degree from score where sno=‘109’ and cno=“3-105”);

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

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

总结
– sql语句的注意 : 1 以;作为结束符 2 不区分大小写

-- 链接数据库
mysql -uroot -pmysql

-- 不显示密码
mysql -uroot -p
mysql

-- 退出数据库
exit/quti/ctrl + d

-- sql语句最后需要有分号;结尾
-- 显示数据库版本 version
select version();

-- 显示时间

select now();

-- 查看当前使用的数据库
select database();

-- 查看所有数据库
show databases;

-- 创建数据库
-- create database 数据库名 charset=utf8;
create database python01;
create database python01 charset=utf8;

-- 查看创建数据库的语句
-- show create database ....
show create database python01;
 

-- 使用数据库
-- use 数据库的名字
use python01;

-- 删除数据库
-- drop database 数据库名;
drop database python01;

– 数据表的操作

-- 查看当前数据库中所有表
show tables;


-- 创建表
-- int unsigned 无符号整形
-- auto_increment 表示自动增长
-- not null 表示不能为空
-- primary key 表示主键
-- default 默认值
-- create table 数据表名字 (字段 类型 约束[, 字段 类型 约束]);
-- unique 唯一的
create table xxx(
	id int unsigned primary key not null auto_increment,
	name varchar(20) not null
);


-- 查看表结构
-- desc 数据表的名字;
desc xxx;

-- 创建 classes 表(id、name)
create table classes(
	id int unsigned primary key auto_increment not null,
	name varchar(20) not null
);

-- 创建 students 表(id、name、age、high (decimal)、gender (enum)、cls_id)
create table students(
	id int unsigned primary key auto_increment not null,
	name varchar(20) not null,
	age int unsigned,
	high decimal(5,2),
	gender enum("男性","女性","中性","保密") default "保密",
	cls_id int unsigned
);


-- 查看表的创建语句
-- show create table 表名字;

show create table xxx;

-- 修改表-添加字段 mascot (吉祥物)
-- alter table 表名 add 列名 类型;

alter table classes add chongwu varchar(20) default"蛇";

-- 修改表-修改字段:不重命名版
-- alter table 表名 modify 列名 类型及约束;
alter table classes modify mascot varchar(30) default"葫芦娃";


-- 修改表-修改字段:重命名版
-- alter table 表名 change 原名 新名 类型及约束;
alter table classes change chongwu mascot varchar(20) default"法拉利";


-- 修改表-删除字段
-- alter table 表名 drop 列名;
alter table classes drop mascot;


-- 删除表
-- drop table 表名;
-- drop database 数据库;




    -- 向students表插入 一个学生信息
	insert into students values(1,"班主任",18,166.66,2,111);
	insert into students values(0,"吴彦祖",42,188.88,1,222);
	insert into students values(null,"晨哥",28,188.88,1,333);

    -- 部分插入
    -- insert into 表名(列1,...) values(值1,...)
    insert into students(name) values("老王");

    -- 多行插入
	insert into students values(null,"老李",28,188.88,1,333),(null,"赵四",28,188.88,1,333);


-- 修改
-- update 表名 set 列1=值1,列2=值2... where 条件;
    -- 全部修改
	update students set high = 170.00;
	-- 按条件修改
	update students set high = 170.00 where id=3;
	-- 按条件修改多个值
	-- update students set gender ="",name = "xxx" ;
	update students set gender="中性",name="111" where id=3;
	
-- 查询基本使用
    -- 查询所有列
    -- select * from 表名;
    select * from students;

    -- 定条件查询
   select * from students where id=1;


    -- 查询指定列
    -- select 列1,列2,... from 表名;
    select name,gender from students;


    -- 可以使用as为列或表指定别名
    -- select 字段[as 别名] , 字段[as 别名] from 数据表;
   select name as "姓名",gender as "性别" from students;


    -- 字段的顺序
    select gender as "性别",name as "姓名" from students;

猜你喜欢

转载自blog.csdn.net/weixin_44887440/article/details/106535896
6.3