作业题目:
1、创建数据库和表:
mysql> create database mydb15_indexstu;
Query OK, 1 row affected (0.01 sec)
mysql> use mydb15_indexstu;
Database changed
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);
Query OK, 0 rows affected (0.05 sec)
mysql> desc student;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| sno | int | NO | PRI | NULL | auto_increment |
| sname | varchar(30) | NO | UNI | NULL | |
| ssex | varchar(2) | NO | | NULL | |
| sage | int | NO | | NULL | |
| sdept | varchar(10) | NO | | 计算机 | |
+-------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql>
mysql> create table course(cno int primary key not null, cname varchar(20) not null);
Query OK, 0 rows affected (0.03 sec)
mysql> desc course;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| cno | int | NO | PRI | NULL | |
| cname | varchar(20) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> create table sc(sno int not null, con varchar(10) primary key not null, score int not null);
Query OK, 0 rows affected (0.03 sec)
mysql>
mysql> desc sc;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sno | int | NO | | NULL | |
| con | varchar(10) | NO | PRI | NULL | |
| score | int | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
2、处理表
(1)修改student 表中年龄(sage)字段属性,数据类型由int 改变为smallint
mysql> alter table student modify sage smallint not null;
Query OK, 0 rows affected (0.11 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student ;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| sno | int | NO | PRI | NULL | auto_increment |
| sname | varchar(30) | NO | UNI | NULL | |
| ssex | varchar(2) | NO | | NULL | |
| sage | smallint | NO | | NULL | |
| sdept | varchar(10) | NO | | 计算机 | |
+-------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
(2)为Course表中Cno 课程号字段设置索引,并查看索引
mysql> create index idx_cno on course(cno);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from course;
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| course | 0 | PRIMARY | 1 | cno | A | 0 | NULL | NULL | | BTREE | | | YES | NULL |
| course | 1 | idx_cno | 1 | cno | A | 0 | NULL | NULL | | BTREE | | | YES | NULL |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
2 rows in set (0.01 sec)
3、为SC表建立按学号(sno)和课程号(cno)组合的升序的主键索引,索引名为SC_INDEX
mysql> create index sc_index on sc(sno,con);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from sc;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| sc | 0 | PRIMARY | 1 | con | A | 0 | NULL | NULL | | BTREE | | | YES | NULL |
| sc | 1 | sc_index | 1 | sno | A | 0 | NULL | NULL | | BTREE | | | YES | NULL |
| sc | 1 | sc_index | 2 | con | A | 0 | NULL | NULL | | BTREE | | | YES | NULL |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
3 rows in set (0.01 sec)
4、创建一视图 stu info,查询全体学生的姓名,性别,课程名,成绩
mysql> create view stu_info as
-> select s.sname,s.ssex,c.cname,sc.score from student s
-> join sc on s.sno=sc.sno
-> join course c on sc.con=c.cno;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from stu_info;
Empty set (0.00 sec)
5、删除所有索引
mysql> drop index idx_cno on course;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> drop index sc_index on sc;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0