MySQL视图索引操作

创建学生表;

sql 复制代码
mysql> create table Student(
    -> Sno int primary key auto_increment,
    -> Sname varchar(30) not null unique,
    -> Ssex char(2) check (Ssex='男' or Ssex='女') not null,
    -> Sage int not null,
    -> Sdept varchar(10) default '计算机' not null
    -> );
Query OK, 0 rows affected (0.04 sec)

创建课程表;

sql 复制代码
mysql> create table Course(
    -> Cno int primary key not null,
    -> Cname varchar(20) not null
    -> );
Query OK, 0 rows affected (0.03 sec)

创建选课表;

sql 复制代码
mysql> create table SC(
    -> Sno int not null,
    -> Cno varchar(10) primary key not null,
    -> Score int not null
    -> );
Query OK, 0 rows affected (0.02 sec)

1、修改 Student 表中年龄(Sage)字段属性

sql 复制代码
mysql> alter table Student modify column Sage smallint;
Query OK, 0 rows affected (0.07 sec)
Records: 0  Duplicates: 0  Warnings: 0

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

sql 复制代码
mysql> create index course_cno_index on Course(Cno);
Query OK, 0 rows affected (0.02 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 | course_cno_index |            1 | Cno         | A         |           0 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
+--------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
2 rows in set (0.01 sec)

3、为 SC 表建立按学号(Sno)和课程号(Cno)组合的升序的主键索引,需要先删除可能存在的主键索引。

sql 复制代码
mysql> alter table SC drop primary key;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table SC add primary key (Sno,Cno);
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

4、创建视图 stu_info

sql 复制代码
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.Cno = C.Cno;
Query OK, 0 rows affected (0.01 sec)

5、删除所有索引:

对于 Student 表,由于没有显式创建索引,无需删除索引操作。

对于 Course 表:

sql 复制代码
mysql> drop index course_cno_index on Course;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

对于 SC 表,通过alter table添加的主键索引:

sql 复制代码
mysql> alter table SC drop primary key;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0
相关推荐
半盏茶香1 小时前
启幕数据结构算法雅航新章,穿梭C++梦幻领域的探索之旅——二叉树序列构造探秘——堆的奥义与实现诗篇
android·数据结构·c++·python·算法·leetcode
QING6181 小时前
Android 应用【内存优化】指南
android·性能优化·app
QING6181 小时前
Android 应用【内存泄漏】优化指南
android·性能优化·app
fatiaozhang95271 小时前
中兴B860AV3.2-T/B860AV3.1-T2_S905L3-B_2+8G_安卓9.0_先线刷+后卡刷固件-完美修复反复重启瑕疵
android·adb·电视盒子·机顶盒rom·魔百盒刷机
顾林海2 小时前
一文吃透Android Context:从原理到实战
android
AJi2 小时前
Android音视频框架探索(一):多媒体系统服务MediaServer
android·ffmpeg·音视频开发
小鱼冻干2 小时前
数据验证
前端·数据库·mysql
小鱼冻干2 小时前
mysql-访问器和虚拟字段
前端·数据库·mysql
每次的天空3 小时前
Android第三次面试总结(activity和线程池)
android·面试·职场和发展
安於宿命3 小时前
【MySQL】基本查询(下)
数据库·mysql