题目是从网上找的,但是做的过程是依靠自己理解做的,如果有更简单的方法,希望大家可以分享一下。
1 建立表
–1.学生表
Student(S#,Sname,Sage,Ssex)
Sid 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
–2.课程表
Course(C#,Cname,T#)
Cid --课程编号,Cname 课程名称,T_id 教师编号
–3.教师表
Teacher(T#,Tname)
T_id 教师编号,Tname 教师姓名
–4.成绩表
Sc
Sid 学生编号,Cid 课程编号,score 分数
1.1 建数据库和表
-- 建立新的数据库
DROP DATABASE IF EXISTS 经典sql50题;
CREATE DATABASE 经典sql50题;
USE 经典sql50题;
-- Table structure for student
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`SId` varchar(10) default NULL,
`Sname` varchar(20) default NULL,
`Sage` date default NULL,
`Ssex` varchar(10) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- table structure for course
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`CId` varchar(10) default NULL,
`Cname` varchar(10) default NULL,
`TId` varchar(10) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- Table structure for teacher
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`TId` varchar(10) default NULL,
`Tname` varchar(20) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- Table structure for sc
DROP TABLE IF EXISTS `sc`;
CREATE TABLE `sc` (
`SId` varchar(10) default NULL,
`CId` varchar(10) default NULL,
`score` int(4) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
1.2 添加数据
Student表:
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
Course 表:
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
teacher表:
insert into Teacher values('01', '张三');
insert into Teacher values('02', '李四');
insert into Teacher values('03', '王五');
Score 表:
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);