当前位置: 首页 > article >正文

MySQL视图索引操作

创建学生表;

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)

创建课程表;

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

创建选课表;

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)字段属性

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 课程号字段设置索引并查看索引

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)组合的升序的主键索引,需要先删除可能存在的主键索引。

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

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 表:

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添加的主键索引:

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

 

 


http://www.kler.cn/a/536958.html

相关文章:

  • 【大模型】DeepSeek与chatGPT的区别以及自身的优势
  • 【AI】在Ubuntu中使用docker对DeepSeek的部署与使用
  • hot100(9)
  • 1.31-子序列问题
  • 【python】简单的flask做页面。一组字母组成的所有单词。这里的输入是一组字母,而输出是所有可能得字母组成的单词列表
  • 【Spring Boot】 SpringBoot自动装配-Condition
  • 【Linux】Ubuntu Linux 系统 ——Android开发环境
  • linux进程通讯-信号处理介绍
  • [开源/教程]使用Ollama+ESP32实现本地对话助手(可接入deepseek等模型)
  • 基于微信平台的报刊订阅小程序的设计与实现ssm+论文源码调试讲解
  • 新注册的域名无法访问,是怎么回事?
  • “AI隐患识别系统,安全多了道“智能护盾”
  • 鸿蒙UI(ArkUI-方舟UI框架)- 设置组件导航和页面路由
  • 青少年编程与数学 02-008 Pyhon语言编程基础 24课题、正则表达式
  • MES系统对于中小型制造企业有什么价值?
  • verilog练习:8bit移位寄存器
  • 防火墙与Squid代理服务器
  • FastReport 加载Load(Stream) 模板内包含换行符不能展示
  • 【网络】应用层协议http
  • 避免样式冲突:掌握CSS选择器优先级与层叠规则的终极指南
  • Itext pdf reader解析
  • mysql的语句备份详解
  • 11.享元模式 (Flyweight)
  • windows同时安装两个不同版本的Mysql
  • C++ ——— 多态的概念及其原理和实现
  • BGP边界网关协议(Border Gateway Protocol)选路、属性(一)