MySQL——test4(综合练习)-处理表

时间:2024-10-28 12:34:49

1. 修改student 表中年龄(sage)字段属性,数据类型由int 改变为smallint

mysql> alter table student modify sage smallint;
Query OK, 0 rows affected (0.05 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    | YES  |     | NULL      |                |
| sdept | varchar(10) | NO   |     | 计算机    |                |
+-------+-------------+------+-----+-----------+----------------+
5 rows in set (0.00 sec)

2. 为Course表中Cno 课程号字段设置索引,并查看索引

mysql> create index index_cno on course(cno);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

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

mysql> create index sc_index on sc(sno,cno);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

4. 创建一视图 stu info,查询全体学生的姓名,性别,课程名,成绩

mysql> create view stu_info as  select sname,ssex,cname,score from student join sc on student.sno=sc.sno join course on sc.cno=course.cno;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+---------------------------+
| Tables_in_mydb15_indexstu |
+---------------------------+
| course                    |
| sc                        |
| stu_info                  |
| student                   |
+---------------------------+
4 rows in set (0.00 sec)

5.删除所有索引

mysql> drop index sc_index on sc;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> drop index index_cno on course;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0