MySQL学⽣表(Student)、课程表(Course)、成绩表
(Score)以及教师信。。。
设有⼀数据库,包括四个表:学⽣表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。四个表的结构分别如表1-1的表(⼀)~表(四)所⽰,数据如表1-2的表(⼀)~表(四)所⽰。⽤SQL语句创建四个表并完成相关题⽬。
表1-1数据库的表结构
表(⼀)Student (学⽣表)
字段名数据类型可否为空含 义
Sno Varchar2(3)否学号(主键)
Sname Varchar2(8)否学⽣姓名
Ssex Varchar2(2)否学⽣性别
Sbirthday Date可学⽣出⽣年⽉
SClass Varchar2(5)可学⽣所在班级
表(⼆)Course(课程表)
属性名数据类型可否为空含 义
Cno Varchar2(5)否课程号(主键)
Cname Varchar(10)否课程名称
Tno Varchar2(3)否教⼯编号(外键)
表(三)Score(成绩表)
属性名数据类型可否为空含 义
Sno Varchar2(3)否学号(外键)
Cno Varchar2(5)否课程号(外键)
Degree Number(4,1)可成绩
主键:Sno+ Cno
表(四)Teacher(教师表)
属性名数据类型可否为空含 义
Tno Varchar2(3)否教⼯编号(主键)
Tname Varchar2(4)否教⼯姓名
Tsex Varchar2(2)否教⼯性别
Tbirthday Date可教⼯出⽣年⽉
Prof Varchar2(6)可职称
Depart Varchar(10)否教⼯所在部门
表1-2数据库中的数据
表(⼀)Student
Sno Sname Ssex Sbirthday class 108曾华男1977/09/0195033 105匡明男1975/10/0295031 107王丽⼥1976/01/2395033 101李军男1976/02/2095033 109王芳⼥1975/02/1095031 103陆君男1974/06/0395031
表(⼆)Course
Cno Cname Tno
3-105计算机导论825
3-245操作系统804
6-166数字电路856
9-888⾼等数学831
表(三)Score
Sno Cno Degree
1033-24586
1053-24575
1093-24568
1033-10592
视图mysql是什么
1053-10588
1093-10576
1013-10564
1073-10591
1083-10578
1016-16685
1076-16679
1086-16681
表(四)Teacher
Tno Tname Tsex Tbirthday Prof Depart 804李诚男1958/12/02副教授计算机系856张旭男1969/03/12讲师电⼦⼯程系825王萍⼥1972/05/05助教计算机系831刘冰⼥1977/08/14助教电⼦⼯程系
mysql面试题学生表
建表sql:玳瑁猫承包农场后
create DATABASE testdb;
use  testdb;
DROP TABLE IF EXISTS `Course`;
codehub合并代码出现的问题
CREATE TABLE `Course` (
`Cno` varchar(5) NOT NULL,
`Cname` varchar(10) NOT NULL,
`Tno` varchar(3) NOT NULL,
PRIMARY KEY (`Cno`),
KEY `Tno` (`Tno`),
CONSTRAINT `Course_ibfk_1` FOREIGN KEY (`Tno`) REFERENCES `Teacher` (`Tno`)
) ENGINE = InnoDB CHARSET = utf8;
INSERT INTO `Course` (`Cno`, `Cname`, `Tno`)
VALUES ('3-105', '计算机导论', '825'),
('3-245', '操作系统', '804'),
('6-166', '数字电路', '856'),
('9-888', '⾼等数学', '831');
/*Table structure for table `Score` */
DROP TABLE IF EXISTS `Score`;
CREATE TABLE `Score` (
`Sno` varchar(3) NOT NULL,
`Cno` varchar(5) NOT NULL,
`Degree` decimal(4, 1) DEFAULT NULL,
PRIMARY KEY (`Sno`, `Cno`),
KEY `Cno` (`Cno`),
CONSTRAINT `Score_ibfk_1` FOREIGN KEY (`Sno`) REFERENCES `Student` (`Sno`), CONSTRAINT `Score_ibfk_2` FOREIGN KEY (`Cno`) REFERENCES `Course` (`Cno`) ) ENGINE = InnoDB CHARSET = utf8;二维数组怎么按行输出
INSERT INTO `Score` (`Sno`, `Cno`, `Degree`)
VALUES ('101', '3-105', '64.0'),
('101', '6-166', '85.0'),
('103', '3-105', '92.0'),
('103', '3-245', '86.0'),
('105', '3-105', '88.0'),
('105', '3-245', '75.0'),
('107', '3-105', '91.0'),
('107', '6-166', '79.0'),
('108', '3-105', '78.0'),
('108', '6-166', '81.0'),
('109', '3-105', '76.0'),
('109', '3-245', '68.0');
/*Table structure for table `Student` */
DROP TABLE IF EXISTS `Student`;
CREATE TABLE `Student` (
`Sno` varchar(3) NOT NULL,
`Sname` varchar(8) NOT NULL,
`Ssex` varchar(2) NOT NULL,
`Sbirthday` date DEFAULT NULL,
`Sclass` varchar(5) DEFAULT NULL,
PRIMARY KEY (`Sno`)
) ENGINE = InnoDB CHARSET = utf8;
INSERT INTO `Student` (`Sno`, `Sname`, `Ssex`, `Sbirthday`, `Sclass`)
VALUES ('101', '李军', '男', '1976-02-20', '95033'),
('103', '陆君', '男', '1974-06-03', '95031'),
('105', '匡明', '男', '1975-10-02', '95031'),
('107', '王丽', '⼥', '1976-01-23', '95033'),
('108', '曾华', '男', '1977-09-01', '95033'),
('109', '王芳', '⼥', '1975-02-10', '95031');
/*Table structure for table `Teacher` */
DROP TABLE IF EXISTS `Teacher`;
CREATE TABLE `Teacher` (
`Tno` varchar(3) NOT NULL,
`Tname` varchar(4) NOT NULL,
`Tsex` varchar(2) NOT NULL,
`Tbirthday` date DEFAULT NULL,
`Tprof` varchar(6) DEFAULT NULL,
`Depart` varchar(10) NOT NULL,
PRIMARY KEY (`Tno`)
)
ENGINE = InnoDB CHARSET = utf8;
INSERT INTO `Teacher` (`Tno`, `Tname`, `Tsex`, `Tbirthday`, `Tprof`
, `Depart`)
VALUES ('804', '李诚', '男', '1958-12-02', '副教授', '计算机系'),
('825', '王萍', '⼥', '1972-05-05', '助教', '计算机系'),
('831', '刘冰', '⼥', '1977-08-14', '助教', '电⼦⼯程系'),
('856', '张旭', '男', '1969-03-12', '讲师', '电⼦⼯程系');
select t.* from Student t;
select t.* from Course t;
select t.* from Teacher t;
select t.* from Score t;
-
- 1、查询教师所有的单位即不重复的Depart列。
select distinct Depart from Teacher;
-- 2、查询Score表中成绩在60到80之间的所有记录。
select * from Score where degree > 60 and  degree < 80;
select * from Score where degree between 60 and 80;
-- 3、查询Score表中成绩为85,86或88的记录。
select * from Score where Degree=85 or Degree=86 or degree=88;
select * from Score where Degree in (85,86,88);
-- 4、查询Student表中“95031”班或性别为“⼥”的同学记录。
select * from Student where sclass=95031 or ssex='⼥';
-- 5、以Sclass降序查询Student表的所有记录。
select * from Student order by sclass desc;
-- 6、以Cno升序、Degree降序查询Score表的所有记录。
select * from Score order by sno asc,degree desc;
-- 7、查询“95031”班的学⽣⼈数。
select count(sno) 学⽣⼈数,sclass from Student where sclass = '95031';
-- 8、查询Score表中的最⾼分的学⽣学号和课程号。(⼦查询或者排序)
select t.sno,to,t.degree from Score t where t.degree in (select max(degree) from Score);
select sno,cno,degree from Score order by Degree desc limit 0,1;
-- 9、查询每门课的平均成绩。
select cno,avg(degree) from Score group by cno;
-- 10、查询Score表中⾄少有5名学⽣选修的并以3开头的课程的平均分数。
select cno,avg(degree) from Score where cno like '3%' group by cno having count(cno)>5;
-- 11、查询所有学⽣的Sname、Cno和Degree列。
-- 多表查询
-- 可以⽤where,内连接,⼦查询
-- 左连接以左表为准匹配,没有的右侧都为null:
select t.sname,so,s.degree from Student t,Score s where t.Sno = s.sno;
select t.sname,so,s.degree from Student t inner join Score s on t.sno = s.sno;
select t.sname,so,s.degree from Student t left join Score s on t.sno = s.sno;
-- 拓展,如果条件写在on上,则把on的所有条件作为匹配条件,不符合的右表都为null。where与on的区别(没看出来啊)select t.sname,so,s.degree from Student t left join Score s on t.sno = s.sno and so = '3-105';
select t.sname,so,s.degree from Student t left join Score s on t.sno = s.sno where so = '3-105';
-- 12、查询所有学⽣的Sno、Cname和Degree列。
-- 多表查询
-- 可以⽤where,内连接,⼦查询
select t.sno,came,t.degree from Score t,Course c where to = co;
select t.sno,came,t.degree from Score t left join Course c on to = co;unity transform
-- 13、查询所有学⽣的Sname、Cname和Degree列。
-- 多表查询
-- 可以⽤where,内连接,⼦查询
select t.sname,came,s.degree from Student t,Score s,Course c where t.sno = s.sno and so = co;
select t.sname,came,s.degree from Student t join Score s on t.sno = s.sno join Course c on so = co;
select t.sname,came,s.degree from Student t inner join Score s on t.Sno=s.Sno inner join Course c on so = co;
-- 14、查询“95033”班学⽣的平均分。
select s.sclass,avg(t.degree) from Score t,Student s where s.sno = t.sno and s.sclass='95033' ;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。