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

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: 0mysql> 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: 0mysql> 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.lryc.cn/news/532951.html

相关文章:

  • 一次奇怪的空指针问题分析:事务、死锁与隐式回滚
  • 解决aspose将Excel转成PDF中文变成方框的乱码问题
  • .net8.0使用EF连接sqlite数据库及使用Gridify实现查询的简易实现
  • 2025.2.5——五、[网鼎杯 2020 青龙组]AreUSerialz
  • 电风扇各国检测认证详细介绍美国FCC+UL欧盟CE+ROHS日本PSE+METI备案+英国UKCA
  • Flutter Isolate解决耗时任务导致卡死
  • 使用deepseek快速创作ppt
  • STM32的HAL库开发---高级定时器---输出比较模式实验
  • python Excel 表读取合并单元格以及清除空格符
  • 额外题目汇总2-链表
  • C#控件开发6—指示灯
  • 探索从传统检索增强生成(RAG)到缓存增强生成(CAG)的转变
  • 【学习总结|DAY036】Vue工程化+ElementPlus
  • 【GitHub】GitHub 2FA 双因素认证 ( 使用 Microsoft Authenticator 应用进行二次验证 )
  • c# 2025/2/7 周五
  • 蓝桥杯思维训练(五)
  • I.MX6ULL 中断介绍下
  • Elasticsearch 生产集群部署终极方案
  • Python用langchain、OpenAI大语言模型LLM情感分析苹果股票新闻数据及提示工程优化应用...
  • 【正点原子K210连载】第六十七章 音频FFT实验 摘自【正点原子】DNK210使用指南-CanMV版指南
  • Centos Ollama + Deepseek-r1+Chatbox运行环境搭建
  • ReactNative进阶(五十九):存量 react-native 项目适配 HarmonyOS NEXT
  • go并发和并行
  • 一种解决SoC总线功能验证完备性的技术
  • Web3 与区块链:开启透明、安全的网络新时代
  • c#中Thread.Join()方法的经典示例
  • 深入了解越权漏洞:概念、危害与防范
  • MySQL 数据库编程-C++
  • dl学习笔记(9):pytorch数据处理的完整流程
  • wps中的vba开发