一、创建数据库mydb15_indexstu
mysql> create database mydb15_indexstu;
二、创建表
mysql> 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);
mysql> create table course(Cno int primary key not null,
-> Cname varchar(20) not null);
mysql> create table SC(Sno int not null,Cno varchar(10) primary key not null,Score int not null);
三、练习
1.修改student 表中年龄(sage)字段属性,数据类型由int 改变为smallint
mysql> alter table student modify Sage smallint;
2.为Course表中Cno 课程号字段设置索引,并查看索引
//创建索引
mysql> create index cno_index on course(Cno);
//查看索引
mysql> show index from course;
3.为SC表建立按学号(sno)和课程号(cno)组合的升序的主键索引,索引名为SC_INDEX
mysql> create index sc_index on SC(Sno,Cno asc);
4.创建一视图 stu info,查询全体学生的姓名,性别,课程名,成绩
mysql> create view stu_info as
select Sname,Ssex,Cname,Score
from student,course,SC
where student.Sno=SC.Sno and course.Cno=SC.Cno;
5.删除所有索引
mysql> drop index sc_index on SC;
mysql> drop index cno_index on course;