create database dengqinyi2;
use dengqinyi2;
/*1、创建学生表*/
create table v_student(
student_id varchar(20) primary key,
student_name varchar(20) not null,
student_sex varchar(10),
department_id varchar(20) not null,
student_birthday date
);
/*2、创建教师表*/
create table v_teacher(
teacher_id varchar(20) primary key,
teacher_name varchar(20) not null,
teacher_sex varchar(10),
department_id varchar(20) not null,
teacher_birthday date,
teacher_workyear date
);
/*3、创建课程表*/
create table v_course(
course_id varchar(20) primary key,
course_name varchar(20) not null,
course_credit float(1),
course_hour int
);
/*4、创建教材表*/
create table v_textbook(
textbook_id varchar(20) primary key,
textbook_name varchar(20) not null,
press varchar(50),
unit_price float(1),
publish_date date
);
/*5、创建教材选用表*/
create table v_textbook_selection(
course_id varchar(20),
textbook_id varchar(20),
textbook_selection_number int,
primary key (course_id,textbook_id),
foreign key (course_id) references v_course(course_id),
foreign key (textbook_id) references v_textbook(textbook_id)
);
/*6、创建学生成绩表*/
create table v_student_score(
student_id varchar(20),
course_id varchar(20),
grade float(1) check(grade <= 100 and grade >= 0),
primary key(student_id,course_id),
foreign key (student_id) references v_student(student_id) on delete cascade,
foreign key (course_id) references v_course(course_id)
);
/*7、创建院系表*/
create table v_department(
department_id varchar(20) primary key,
department_name varchar(20) not null,
department_administrator varchar(20),
department_phone varchar(20)
);
/*8、创建教师课程任务表*/
create table v_teacher_course_task(
teacher_id varchar(20),
course_id varchar(20),
primary key(teacher_id,course_id),
foreign key (teacher_id) references v_teacher(teacher_id),
foreign key (course_id) references v_course(course_id)
);
/*9、创建课室表*/
create table v_classroom(
classroom_id varchar(20) primary key,
classroom_location varchar(20) not null,
classroom_capacity int
);
/*10、创建课程开设表*/
create table v_course_develop(
course_id varchar(20),
teacher_id varchar(20),
classroom_id varchar(20),
primary key(course_id,teacher_id,classroom_id),
foreign key (course_id) references v_course(course_id),
foreign key (teacher_id) references v_teacher(teacher_id),
foreign key (classroom_id) references v_classroom(classroom_id)
);
/*为学生表添加外键院系编号*/
alter table v_student
add constraint fk_department_id
foreign key (department_id) references v_department(department_id);
/*为教师表添加外键院系编号*/
alter table v_teacher
add constraint fk_department_id2
foreign key (department_id) references v_department(department_id);
/*将院系表的院系名称列数据类型改为可变程度类型varchar(100)*/
alter table v_department
modify department_name varchar(100) not null;
/*1、在院系表中插入数据*/
insert into v_department(
department_id,
department_name,
department_administrator,
department_phone)
values('001', '计算机学院', '周老师', '12345678910');
insert into v_department(
department_id,
department_name,
department_administrator,
department_phone)
values('002', '外国语学院', '林老师', '23456789101');
insert into v_department(
department_id,
department_name,
department_administrator,
department_phone)
values('006', '信息工程学院', '王老师', '34567891012');
insert into v_department(
department_id,
department_name,
department_administrator,
department_phone)
values('133', '外国语学院', '赵老师', '45678910123');
insert into v_department(
department_id,
department_name,
department_administrator,
department_phone)
values('155', '法学院', '钱老师', '56789101234');
insert into v_department(
department_id,
department_name,
department_administrator,
department_phone)
values('122', '经济与工商管理学院', '孙老师', '67891012345');
insert into v_department(
department_id,
department_name,
department_administrator,
department_phone)
values('111', '建设管理与房地产学院', '李老师', '78910123456');
insert into v_department(
department_id,
department_name,
department_administrator,
department_phone)
values('088', '体育学院', '吴老师', '89101234567');
insert into v_department(
department_id,
department_name,
department_administrator,
department_phone)
values('099', '电影学院', '郑老师', '91012345678');
insert into v_department(
department_id,
department_name,
department_administrator,
department_phone)
values('100', '艺术学院', '陈老师', '11111111111');
insert into v_department(
department_id,
department_name,
department_administrator,
department_phone)
values('144', '材料与能源学院', '魏老师', '22222222222');
select * from v_department;
/*2、在学生表中插入数据*/
insert into v_student (
student_id,
student_name,
student_sex,
department_id,
student_birthday)
values('3114005847', '张三', '男', '001', '1996-9-1');
insert into v_student (
student_id,
student_name,
student_sex,
department_id,
student_birthday)
values('3214005848', '李四', '女', '002', '1996-8-1');
insert into v_student (
student_id,
student_name,
student_sex,
department_id,
student_birthday)
values('3114005849', '王五', '男', '006', '1995-1-2');
insert into v_student (
student_id,
student_name,
student_sex,
department_id,
student_birthday)
values('3114005850', '赵六', '男', '133', '1994-1-8');
insert into v_student (
student_id,
student_name,
student_sex,
department_id,
student_birthday)
values('3214005851', '刘七', '女', '155', '1996-2-2');
insert into v_student (
student_id,
student_name,
student_sex,
department_id,
student_birthday)
values('3114005888', '陈小小', '男', '133', '1997-3-6');
insert into v_student (
student_id,
student_name,
student_sex,
department_id,
student_birthday)
values('3114005833', '张大大', '男', '122', '1998-4-6');
insert into v_student (
student_id,
student_name,
student_sex,
department_id,
student_birthday)
values('3114005678', '王中中', '男', '111', '1999-10-11');
insert into v_student (
student_id,
student_name,
student_sex,
department_id,
student_birthday)
values('3214005555', '邓八', '女', '088', '1997-4-5');
insert into v_student (
student_id,
student_name,
student_sex,
department_id,
student_birthday)
values('3114003245', '林九', '男', '099', '1996-10-18');
select * from v_student;
/*3、在教师表中插入数据*/
insert into v_teacher(
teacher_id,
teacher_name,
teacher_sex,
department_id,
teacher_birthday,
teacher_workyear)
values('123', '杨幂', '女', '006', '1980-1-1', '2016-5-12');
insert into v_teacher(
teacher_id,
teacher_name,
teacher_sex,
department_id,
teacher_birthday,
teacher_workyear)
values('8645', '刘诗诗', '女', '133', '1980-1-2', '2016-5-13');
insert into v_teacher(
teacher_id,
teacher_name,
teacher_sex,
department_id,
teacher_birthday,
teacher_workyear)
values('7896', '林青霞', '女', '111', '1980-1-3', '2016-5-14');
insert into v_teacher(
teacher_id,
teacher_name,
teacher_sex,
department_id,
teacher_birthday,
teacher_workyear)
values('56478', '王祖贤', '女', '155', '1980-1-4', '2016-5-15');
insert into v_teacher(
teacher_id,
teacher_name,
teacher_sex,
department_id,
teacher_birthday,
teacher_workyear)
values('2', '钟楚红', '女', '111', '1980-1-5', '2016-5-16');
insert into v_teacher(
teacher_id,
teacher_name,
teacher_sex,
department_id,
teacher_birthday,
teacher_workyear)
values('1233', '梅艳芳', '女', '100', '1980-1-6', '2016-5-17');
insert into v_teacher(
teacher_id,
teacher_name,
teacher_sex,
department_id,
teacher_birthday,
teacher_workyear)
values('12235', '张曼玉', '女', '099', '1980-1-7', '2016-5-18');
insert into v_teacher(
teacher_id,
teacher_name,
teacher_sex,
department_id,
teacher_birthday,
teacher_workyear)
values('145698', '唐嫣', '女', '088', '1980-1-8', '2016-5-19');
insert into v_teacher(
teacher_id,
teacher_name,
teacher_sex,
department_id,
teacher_birthday,
teacher_workyear)
values('520', '郑爽', '女', '122', '1980-1-9', '2016-5-20');
insert into v_teacher(
teacher_id,
teacher_name,
teacher_sex,
department_id,
teacher_birthday,
teacher_workyear)
values('1314', '关之琳', '女', '155', '1980-1-10', '2016-5-21');
insert into v_teacher(
teacher_id,
teacher_name,
teacher_sex,
department_id,
teacher_birthday,
teacher_workyear)
values('3096', '李嘉欣', '女', '006', '1980-1-11', '2016-5-22');
insert into v_teacher(
teacher_id,
teacher_name,
teacher_sex,
department_id,
teacher_birthday,
teacher_workyear)
values('4044', '张柏芝', '女', '002', '1980-1-12', '2016-5-23');
insert into v_teacher(
teacher_id,
teacher_name,
teacher_sex,
department_id,
teacher_birthday,
teacher_workyear)
values('5055', '范冰冰', '女', '001', '1980-1-13', '2016-5-24');
select * from v_teacher;
/*4、在课程表中插入数据*/
insert into v_course(
course_id,
course_name,
course_credit,
course_hour)
values('1', '数据库', 3, 48);
insert into v_course(
course_id,
course_name,
course_credit,
course_hour)
values('2', '高等数学', 5, 80);
insert into v_course(
course_id,
course_name,
course_credit,
course_hour)
values('3', '操作系统', 3.5, 48);
insert into v_course(
course_id,
course_name,
course_credit,
course_hour)
values('4', '数据结构', 2.5, 36);
insert into v_course(
course_id,
course_name,
course_credit,
course_hour)
values('5', '数据挖掘', 2, 24);
insert into v_course(
course_id,
course_name,
course_credit,
course_hour)
values('6', 'Java程序设计', 2, 24);
insert into v_course(
course_id,
course_name,
course_credit,
course_hour)
values('7', '计算机组成原理', 3.5, 48);
insert into v_course(
course_id,
course_name,
course_credit,
course_hour)
values('8', '计算机文化基础', 2, 36);
insert into v_course(
course_id,
course_name,
course_credit,
course_hour)
values('9', '软件工程', 3, 36);
insert into v_course(
course_id,
course_name,
course_credit,
course_hour)
values('10', '人工智能', 3, 56);
select * from v_course;
/*5、在教材表中插入数据*/
insert into v_textbook(
textbook_id,
textbook_name,
press,
unit_price,
publish_date)
values('123', '数据库系统概论', '高等教育出版社', 33.8, '2009/2/3');
insert into v_textbook(
textbook_id,
textbook_name,
press,
unit_price,
publish_date)
values('886', '教材一', '高等教育出版社', 44.4, '2012/3/3');
insert into v_textbook(
textbook_id,
textbook_name,
press,
unit_price,
publish_date)
values('234', '教材二', '高等教育出版社', 24.9, '2009/2/4');
insert into v_textbook(
textbook_id,
textbook_name,
press,
unit_price,
publish_date)
values('2456', '教材三', '高等教育出版社', 26.9, '2009/2/6');
insert into v_textbook(
textbook_id,
textbook_name,
press,
unit_price,
publish_date)
values('456', '教材四', '高等教育出版社', 27.9, '2009/2/7');
insert into v_textbook(
textbook_id,
textbook_name,
press,
unit_price,
publish_date)
values('1111', '教材五', '高等教育出版社', 28.9, '2009/2/8');
insert into v_textbook(
textbook_id,
textbook_name,
press,
unit_price,
publish_date)
values('2222', '教材六', '高等教育出版社', 29.9, '2009/2/9');
insert into v_textbook(
textbook_id,
textbook_name,
press,
unit_price,
publish_date)
values('3333', '教材七', '高等教育出版社', 30.2, '2009/2/10');
insert into v_textbook(
textbook_id,
textbook_name,
press,
unit_price,
publish_date)
values('4444', '教材八', '人民教育出版社', 40, '2009/2/11');
insert into v_textbook(
textbook_id,
textbook_name,
press,
unit_price,
publish_date)
values('5555', '教材九', '人民邮电出版社', 45, '2009/2/12');
insert into v_textbook(
textbook_id,
textbook_name,
press,
unit_price,
publish_date)
values('6666', '教材十', '人民邮电出版社', 46, '2009/2/13');
insert into v_textbook(
textbook_id,
textbook_name,
press,
unit_price,
publish_date)
values('7777', '教材十一', '人民邮电出版社', 23, '2009/2/14');
insert into v_textbook(
textbook_id,
textbook_name,
press,
unit_price,
publish_date)
values('8888', '教材十二', '机械工业出版社', 18, '2009/2/15');
insert into v_textbook(
textbook_id,
textbook_name,
press,
unit_price,
publish_date)
values('9999', '教材十三', '机械工业出版社', 25, '2009/2/16');
select * from v_textbook;
/*6、在教材选用表中插入数据*/
insert into v_textbook_selection(
course_id,
textbook_id,
textbook_selection_number)
values('1', '8888', 100);
insert into v_textbook_selection(
course_id,
textbook_id,
textbook_selection_number)
values('2', '9999', 200);
insert into v_textbook_selection(
course_id,
textbook_id,
textbook_selection_number)
values('3', '123', 300);
insert into v_textbook_selection(
course_id,
textbook_id,
textbook_selection_number)
values('4', '234', 400);
insert into v_textbook_selection(
course_id,
textbook_id,
textbook_selection_number)
values('5', '886', 500);
insert into v_textbook_selection(
course_id,
textbook_id,
textbook_selection_number)
values('6', '2456', 550);
insert into v_textbook_selection(
course_id,
textbook_id,
textbook_selection_number)
values('7', '456', 600);
insert into v_textbook_selection(
course_id,
textbook_id,
textbook_selection_number)
values('8', '1111', 650);
insert into v_textbook_selection(
course_id,
textbook_id,
textbook_selection_number)
values('9', '2222', 450);
insert into v_textbook_selection(
course_id,
textbook_id,
textbook_selection_number)
values('10', '3333', 230);
insert into v_textbook_selection(
course_id,
textbook_id,
textbook_selection_number)
values('2', '4444', 340);
insert into v_textbook_selection(
course_id,
textbook_id,
textbook_selection_number)
values('5', '5555', 280);
insert into v_textbook_selection(
course_id,
textbook_id,
textbook_selection_number)
values('6', '6666', 110);
insert into v_textbook_selection(
course_id,
textbook_id,
textbook_selection_number)
values('6', '7777', 150);
select * from v_textbook_selection;
/*7、在学生成绩表中插入数据*/
insert into v_student_score(
student_id,
course_id,
grade)
values('3114005847', '1', 96);
insert into v_student_score(
student_id,
course_id,
grade)
values('3214005848', '2', 100);
insert into v_student_score(
student_id,
course_id,
grade)
values('3114005849', '3', 58);
insert into v_student_score(
student_id,
course_id,
grade)
values('3114005850', '4', 88.5);
insert into v_student_score(
student_id,
course_id,
grade)
values('3214005851', '5', 75);
insert into v_student_score(
student_id,
course_id,
grade)
values('3114005888', '6', 85);
insert into v_student_score(
student_id,
course_id,
grade)
values('3114005833', '7', 65);
insert into v_student_score(
student_id,
course_id,
grade)
values('3114005678', '8', 88);
insert into v_student_score(
student_id,
course_id,
grade)
values('3214005555', '9', 92);
insert into v_student_score(
student_id,
course_id,
grade)
values('3114003245', '10', 93);
insert into v_student_score(
student_id,
course_id,
grade)
values('3114005847', '5', 96);
insert into v_student_score(
student_id,
course_id,
grade)
values('3114005847', '6', 91);
insert into v_student_score(
student_id,
course_id,
grade)
values('3114005833', '6', 98);
select * from v_student_score;
/*8、在教师课程任务表中插入数据*/
insert into v_teacher_course_task(
teacher_id,
course_id)
values('123', '1');
insert into v_teacher_course_task(
teacher_id,
course_id)
values('8645', '2');
insert into v_teacher_course_task(
teacher_id,
course_id)
values('7896', '3');
insert into v_teacher_course_task(
teacher_id,
course_id)
values('56478', '4');
insert into v_teacher_course_task(
teacher_id,
course_id)
values('2', '5');
insert into v_teacher_course_task(
teacher_id,
course_id)
values('1233', '6');
insert into v_teacher_course_task(
teacher_id,
course_id)
values('12235', '7');
insert into v_teacher_course_task(
teacher_id,
course_id)
values('145698', '8');
insert into v_teacher_course_task(
teacher_id,
course_id)
values('520', '9');
insert into v_teacher_course_task(
teacher_id,
course_id)
values('1314', '10');
insert into v_teacher_course_task(
teacher_id,
course_id)
values('3096', '1');
insert into v_teacher_course_task(
teacher_id,
course_id)
values('4044', '3');
insert into v_teacher_course_task(
teacher_id,
course_id)
values('5055', '3');
insert into v_teacher_course_task(
teacher_id,
course_id)
values('5055', '8');
insert into v_teacher_course_task(
teacher_id,
course_id)
values('5055', '4');
select * from v_teacher_course_task;
/*9、在课室表中插入数据*/
insert into v_classroom(
classroom_id,
classroom_location,
classroom_capacity)
values('10010', '教一221', 100);
insert into v_classroom(
classroom_id,
classroom_location,
classroom_capacity)
values('10011', '教一222', 100);
insert into v_classroom(
classroom_id,
classroom_location,
classroom_capacity)
values('10012', '教一223', 100);
insert into v_classroom(
classroom_id,
classroom_location,
classroom_capacity)
values('10013', '教一224', 100);
insert into v_classroom(
classroom_id,
classroom_location,
classroom_capacity)
values('10014', '教五204', 250);
insert into v_classroom(
classroom_id,
classroom_location,
classroom_capacity)
values('10015', '教五205', 250);
insert into v_classroom(
classroom_id,
classroom_location,
classroom_capacity)
values('10016', '教六105', 250);
insert into v_classroom(
classroom_id,
classroom_location,
classroom_capacity)
values('10017', '教六106', 250);
insert into v_classroom(
classroom_id,
classroom_location,
classroom_capacity)
values('10018', '教六107', 250);
insert into v_classroom(
classroom_id,
classroom_location,
classroom_capacity)
values('10019', '教二402', 100);
insert into v_classroom(
classroom_id,
classroom_location,
classroom_capacity)
values('10020', '教二403', 100);
insert into v_classroom(
classroom_id,
classroom_location,
classroom_capacity)
values('10021', '教二404', 100);
insert into v_classroom(
classroom_id,
classroom_location,
classroom_capacity)
values('10022', '教二405', 100);
select * from v_classroom;
/*10、在课程开设表中插入数据*/
insert into v_course_develop(
course_id,
teacher_id,
classroom_id)
values('1', '123', '10010');
insert into v_course_develop(
course_id,
teacher_id,
classroom_id)
values('2', '8645', '10011');
insert into v_course_develop(
course_id,
teacher_id,
classroom_id)
values('3', '7896', '10012');
insert into v_course_develop(
course_id,
teacher_id,
classroom_id)
values('4', '56478', '10013');
insert into v_course_develop(
course_id,
teacher_id,
classroom_id)
values('5', '2', '10014');
insert into v_course_develop(
course_id,
teacher_id,
classroom_id)
values('6', '1233', '10015');
insert into v_course_develop(
course_id,
teacher_id,
classroom_id)
values('7', '12235', '10016');
insert into v_course_develop(
course_id,
teacher_id,
classroom_id)
values('8', '145698', '10017');
insert into v_course_develop(
course_id,
teacher_id,
classroom_id)
values('9', '520', '10018');
insert into v_course_develop(
course_id,
teacher_id,
classroom_id)
values('10', '1314', '10019');
insert into v_course_develop(
course_id,
teacher_id,
classroom_id)
values('1', '3096', '10020');
insert into v_course_develop(
course_id,
teacher_id,
classroom_id)
values('3', '4044', '10021');
insert into v_course_develop(
course_id,
teacher_id,
classroom_id)
values('3', '5055', '10022');
insert into v_course_develop(
course_id,
teacher_id,
classroom_id)
values('8', '5055', '10017');
insert into v_course_develop(
course_id,
teacher_id,
classroom_id)
values('4', '5055', '10014');
select * from v_course_develop;
/*(1)为课程开设表建立索引,按课程编号升序建唯一索引*/
create index courseno
on v_course_develop(
course_id asc
);
show index
from v_course_develop;
/*(2)查询教师课程任务表中的教师编号*/
select distinct teacher_id
from v_teacher_course_task;
/*(3)查询教师表全体教师的姓名及年龄*/
select teacher_name, 2016-year(teacher_birthday) teacher_age
from v_teacher;
/*(4)在教师课程任务表中查询有任务的教师总人数*/
select count(distinct teacher_id)
from v_teacher_course_task;
/*(5)求各个课程号及对应的教师人数*/
select course_id, count(teacher_id)
from v_course_develop
group by course_id;
/*(6)查询学生成绩不及格的学生学号,姓名,课程名及对应的院系名称及开这门的教师姓名*/
select v_student.student_id, student_name, course_name, department_name, teacher_name
from v_student, v_course, v_department, v_teacher, v_student_score, v_teacher_course_task
where grade < 60
and v_student_score.student_id = v_student.student_id
and v_course.course_id = v_student_score.course_id
and v_student.department_id = v_department.department_id
and v_teacher_course_task.course_id = v_student_score.course_id
and v_teacher_course_task.teacher_id = v_teacher.teacher_id;
/*(7)连接,列出学生学号,学生姓名,其选修课程名,课室位置,课程学分,学时及使用教材名称*/
select distinct u.student_id, u.student_name, cour_name.course_name, class_loca.classroom_location, course_credit, course_hour, textbook_name
from v_student
as u
left join(
select student_id, v_course.course_name
from v_course,v_student_score
where v_course.course_id = v_student_score.course_id
)
as cour_name
on(u.student_id = cour_name.student_id)
left join(
select course_name, classroom_location
from v_course_develop, v_classroom, v_student_score, v_course
where v_classroom.classroom_id = v_course_develop.classroom_id
and v_course_develop.course_id = v_course.course_id
and v_student_score.course_id = v_course.course_id
)
as class_loca
on(cour_name.course_name = class_loca.course_name)
left join(
select course_name, course_credit, course_hour
from v_course, v_student_score
where v_student_score.course_id = v_course.course_id
)
as cour_crehou
on(class_loca.course_name = cour_crehou.course_name)
left join(
select v_textbook.textbook_name, v_course.course_name
from v_textbook, v_course, v_textbook_selection
where v_textbook_selection.textbook_id = v_textbook.textbook_id
and v_textbook_selection.course_id = v_course.course_id
)
as textbook
on(cour_crehou.course_name = textbook.course_name)
;
/*(8)查询成绩在80分到90分之间学生姓名及课程名及分数*/
select student_name, course_name, grade
from v_student, v_course, v_student_score
where grade
between 80 and 90
and v_student.student_id = v_student_score.student_id
and v_course.course_id = v_student_score.course_id;
/*(9)创建美女教师表的视图及查看*/
create view beauty_teacher
as
select teacher_name, teacher_birthday, teacher_id
from v_teacher
where teacher_sex = '女';
select * from beauty_teacher;
/*(10)修改操作学习,将张三的所有科目成绩全部改为60分*/
update v_student_score
set grade=60
where v_student_score.student_id = (
select student_id
from v_student
where student_name = '张三'
);
5、运行结果: