创建数据库并使用:
sql
create database mydb1_indexstud;
use mydb1_indexstud;

创建三张表:
创建学生表并查询:
sql
create table student(Sno int primary key auto_increment,Sname varchar(30) not null unique, Ssex varchar(2) check (Ssex='男' or Ssex='女') not null,Sage int not null, Sdept varchar(10) default '计算机' not null );

创建课程表并查询:
sql
create table SC(Sno int not null,Cno int not null,Score int not null,primary key(Sno, Cno));

创建选课表并查询:
sql
create table SC(Sno int not null,Cno int not null,Score int not null,primary key(Sno, Cno));

处理表:
1、修改表中Sage的数据类型为smallint
sql
alter table Student modify column Sage smallint not null;

2、为course表的cno字段设置索引,并查看索引
sql
create index idx_course_cno on Course(Cno);


3、为SC表建立sno+cno组合的升序主键索引索引名为SC_INDEX
先删除SC表已有主键在建立新的
sql
alter table SC drop primary key;
alter table SC add constraint SC_INDEX primary key(Sno asc, Cno asc);

4、创建视图stu_info查询学生姓名、性别、课程名、成绩
sql
create view stu_info as
select s.Sname, s.Ssex, c.Cname, sc.Score
from Student s
join SC sc on s.Sno = sc.Sno
join Course c on sc.Cno = c.Cno;

5、删除所有索引
删除course表的普通索引
删除SC表的主键索引
sql
drop index idx_course_cno on course;
alter table SC drop primary key;
