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

数据库基础练习4(有关索引,视图完整解答)

建立需要的表

学生表

mysql> create table studnet(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.08 sec)

课程表,选课表

mysql> create table course (cno int primary key not null ,cname varchar(20) not null);
Query OK, 0 rows affected (0.04 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.05 sec)

处理表 

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

mysql> alter table student modify column sage smallint;
ERROR 1146 (42S02): Table 'mydb15_indexstu.student' doesn't exist
mysql> alter table studnet modify column sage smallint;
Query OK, 0 rows affected (0.19 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql> alter table studnet rename student;
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  | smallint    | YES  |     | 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.04 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 | idx_cno  |            1 | cno         | A         |           0 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
2 rows in set (0.02 sec)mysql> show index from course\G
*************************** 1. row ***************************Table: courseNon_unique: 0Key_name: PRIMARYSeq_in_index: 1Column_name: cnoCollation: ACardinality: 0Sub_part: NULLPacked: NULLNull:Index_type: BTREEComment:
Index_comment:Visible: YESExpression: NULL
*************************** 2. row ***************************Table: courseNon_unique: 1Key_name: idx_cnoSeq_in_index: 1Column_name: cnoCollation: ACardinality: 0Sub_part: NULLPacked: NULLNull:Index_type: BTREEComment:
Index_comment:Visible: YESExpression: NULL
2 rows in set (0.01 sec)

加上\G使观看更加简单

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

mysql> alter table sc drop primary key;
Query OK, 0 rows affected (0.12 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql> alter table sc add constraint SC_INDEX primary key(sno,cno);
Query OK, 0 rows affected (0.08 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql> create index SC_INDEX on sc(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. 删除所有索引 

mysql> alter table course drop  index idx_cno;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql> alter table student drop index sname;mysql> alter table sc drop index SC_INDEX;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

http://www.lryc.cn/news/532962.html

相关文章:

  • 实操给触摸一体机接入大模型语音交互
  • Excel中对单列数据进行去重筛选
  • K8s —基础指南(K8s - Basic Guide)
  • ABAP开发中的前导零和末尾零
  • Baklib赋能数字内容体验个性化推荐提升用户体验的未来之路
  • 关于Redis的持久化
  • 【C语言标准库函数】指数与对数函数:exp(), log(), log10()
  • 2024美团春招硬件开发笔试真题及答案解析
  • Python内置函数map(), list(), len(), iter(), hex(), hash()的详细解析,包括功能、语法、示例及注意事项
  • [LVGL] 在VC_MFC中移植LVGL
  • MySQL视图索引操作
  • 一次奇怪的空指针问题分析:事务、死锁与隐式回滚
  • 解决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情感分析苹果股票新闻数据及提示工程优化应用...