一 )学生表:Student (Sno, Sname, Ssex , Sage, Sdept)
学号,姓名,性别,年龄,所在系 Sno为主键
课程表:Course (Cno, Cname,)
课程号,课程名 Cno为主键
学生选课表:SC (Sno, Cno, Score)
学号,课程号,成绩 Sno,Cno为主键
1.用SQL语句创建学生表student,定义主键,姓名不能重名,性别只能输入男或女,所在系的默认值是 “计算机”。
MariaDB [test]> create table student(Sno varchar(20) not null primary key,
-> Sname varchar(20) unique,
-> Ssex varchar(20) check(Ssex=‘男’ or Ssex=‘女’),
-> Sage int(10),
-> Sdept varchar(20) default ‘计算机’);
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]> create table Course(Cno varchar(20) primary key, Cname varchar(20));
2.修改student 表中年龄(age)字段属性,数据类型由int 改变为smallint。
MariaDB [test]> alter table student modify Sage smallint;
3.为SC表建立按学号(sno)和课程号(cno)组合的升序的主键索引,索引名为SC_INDEX 。
create unique index SC_INDEX on sc(sno ASC,cno ASC);
4.向student表添加一条纪录:200201,张三,男,21,计算机。
insert into student values(‘200220’,‘张三’,‘男’,‘21’,‘计算机’);
5.选修了2号课程且成绩低于70的的学生每人成绩增加5分。
update SC set Score=Score+5 where Cno=‘2’ and Score <70;
6.删除选修了课程名称为“单片机”的学生的选课成绩记录。
delete from SC where Sno=‘单片机’;
7.创建一视图 stu_info,查询全体学生的姓名,性别,课程名,成绩。
create view stu_info as select Sname,Ssex,Cno,Score from student,SC where student.Sno=SC.sno;
8.查询不及格学生的姓名。
select Sname from student,SC where SC.Sno=student.Sno and SC.Score >= 60;
9.查询选修四门以上课程的学生学号。
select Sno from SC group by Sno > 4;
10.查询2号课程的最高分的学生的姓名。
select Sname from student where Sno= (select Sno from SC where Cno=2 and Score=(select max(Score) from SC ));
为保证答案一致性,表中添加数据如下:
insert into Student
values(1001,‘张三’,‘男’,21,‘计算机’);