数据库MySQL----索引及视图

发布时间:2024年01月18日

学生表:Student (Sno, Sname, Ssex , Sage, Sdept)
学号,姓名,性别,年龄,所在系 Sno为主键
课程表:Course (Cno, Cname,)
课程号,课程名 Cno为主键
学生选课表:SC (Sno, Cno, Score)
学号,课程号,成绩 Sno,Cno为主键

1.用SQL语句创建学生表student,定义主键,姓名不能重名,性别只能输入男或女,所在系的默认值是 “计算机”。

?create table if not exists Student (
? ? -> ? ? ? ? ?Sno int primary key auto_increment,
? ? -> ? ? ? ? ?Sname varchar(50) not null unique,
? ? -> ? ? ? ? ?Ssex varchar(10) check (Ssex in("男", "女")),
? ? -> ? ? ? ? ?Sage int,
? ? -> ? ? ? ? ?Sdept varchar(50) default "计算机"
? ? -> ?);
create table if not exists Course (
?? ??? ?Cno int primary key auto_increment,
?? ??? ?Cname varchar(50) not null unique
?? ?);
create table SC(
?? ?sno int(10),
?? ?cno int(10),?
?? ?score int(10),
?? ?primary key (sno,cno),
?? ?foreign key(sno) references Student(sno),
?? ?foreign key(cno) references Course(cno)
);
2.修改student 表中年龄(age)字段属性,数据类型由int 改变为smallint。

alter table Student modify Sage smallint;
desc Student;

3.为SC表建立按学号(sno)和课程号(cno)组合的升序的主键索引,索引名为SC_INDEX 。

?create unique index SC_INDX on SC(sno asc,cno asc);
4.创建一视图 stu_info,查询全体学生的姓名,性别,课程名,成绩。

create view stu_info as select Student.sname,Student.ssec,Course.cno,SC.score?
from Student,SC,Course where Student.Sno=SC.sno and SC.cno=Course.cno;
?
select * from stu_info;
?

文章来源:https://blog.csdn.net/weixin_61885606/article/details/135684206
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。