数据库库、表的创建及处理
创建数据库并使用:
create database mydb1_indexstud;
use mydb1_indexstud;
创建三张表:
创建学生表并查询:
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 );
创建课程表并查询:
create table SC(Sno int not null,Cno int not null,Score int not null,primary key(Sno, Cno));
创建选课表并查询:
create table SC(Sno int not null,Cno int not null,Score int not null,primary key(Sno, Cno));
处理表:
1、修改表中Sage的数据类型为smallint
alter table Student modify column Sage smallint not null;
2、为course表的cno字段设置索引,并查看索引
create index idx_course_cno on Course(Cno);

3、为SC表建立sno+cno组合的升序主键索引索引名为SC_INDEX
先删除SC表已有主键在建立新的
alter table SC drop primary key;
alter table SC add constraint SC_INDEX primary key(Sno asc, Cno asc);
4、创建视图stu_info查询学生姓名、性别、课程名、成绩
create view stu_info as select s.Sname, s.Ssex, c.Cname, sc.Scorefrom Student sjoin SC sc on s.Sno = sc.Snojoin Course c on sc.Cno = c.Cno;
5、删除所有索引
删除course表的普通索引
删除SC表的主键索引
drop index idx_course_cno on course;
alter table SC drop primary key;
