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

MYSQL第四次作业

创建表

mysql> create database mydb15_indexstu;
Query OK, 1 row affected (0.05 sec)mysql> use  mydb15_indexstu;
Database changed
mysql> create table student(-> 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.04 sec)

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

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  | int         | NO   |     | NULL    |                |
| Sdept | varchar(10) | NO   |     | 计算机  |                |
+-------+-------------+------+-----+---------+----------------+
5 rows in set (0.03 sec)mysql> alter table student modify Sage smallint;
Query OK, 0 rows affected (0.12 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql> 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.03 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql> 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: index_cnoSeq_in_index: 1Column_name: CnoCollation: ACardinality: 0Sub_part: NULLPacked: NULLNull:Index_type: BTREEComment:
Index_comment:Visible: YESExpression: NULL
2 rows in set (0.02 sec)

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

mysql> create unique index SC_INDEX ON sc(Sno,Cno asc);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql> show index from sc\G
*************************** 1. row ***************************Table: scNon_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: scNon_unique: 0Key_name: SC_INDEXSeq_in_index: 1Column_name: SnoCollation: ACardinality: 0Sub_part: NULLPacked: NULLNull:Index_type: BTREEComment:
Index_comment:Visible: YESExpression: NULL
*************************** 3. row ***************************Table: scNon_unique: 0Key_name: SC_INDEXSeq_in_index: 2Column_name: CnoCollation: ACardinality: 0Sub_part: NULLPacked: NULLNull:Index_type: BTREEComment:
Index_comment:Visible: YESExpression: NULL
3 rows in set (0.02 sec)

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

首先要先将三张表连到一起再进行视图创建

mysql> create or replace 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.02 sec)mysql> show tables;
+---------------------------+
| Tables_in_mydb15_indexstu |
+---------------------------+
| course                    |
| sc                        |
| stu_info                  |
| student                   |
+---------------------------+
4 rows in set (0.01 sec)

5、删除所有索引

mysql> drop index index_cno on course;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql> drop index SC_INDEX on sc;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0
http://www.dtcms.com/a/569292.html

相关文章:

  • 某游戏大厂分布式系统经典实战面试题解析
  • 某游戏大厂计算机网络面试问题深度解析(一)
  • C#基础:如何从现有类库复制一个新的类库,并且加入解决方案
  • C# 中 Entity Framework (EF) 和 EF Core 里的 `AsNoTracking` 方法
  • 基于视觉分析的加油站通话行为安全预警系统 构建加油安全新防线 通话行为模式识别 边缘计算通话动作监测设备
  • Traefik vs Spring Cloud:微服务架构的两种截然不同的技术路线
  • 郑州百度seo网站优广州网站开发外包哪家好
  • 高端网站建设推来客网络做seo推广公司
  • 数据挖掘6-AI总结
  • 网站首页域名如何设置访问快宿迁沭阳网站建设
  • 数据结构 —— 栈
  • 微信小程序开发案例 | 个人相册小程序(下)
  • 网站域名账号网址申请注册
  • 电商 API 数据交互最佳实践:JSON 格式优化、数据校验与异常处理
  • 重庆网站建设 沛宣织梦cms 官方网站
  • 零基础新手小白快速了解掌握服务集群与自动化运维(十七)ELK日志分析模块--Elasticsearch介绍与配置
  • 如何使用elasticdump进行elasticsearch数据还原
  • 【运维记录】Centos 7 基础命令缺失
  • 手写 RPC 框架
  • etcd 高可用分布式键值存储
  • 【ETCD】ETCD单节点二进制部署(TLS)
  • 小网站 收入请简述网站制作流程
  • 抗辐照MCU芯片在无人叉车领域的性能评估与选型建议
  • 什么是LLM?
  • Java/PHP源码解析:一站式上门维修服务系统的全栈实现
  • MPU6050 DMP 移植中 mpu_run_self_test () 自检失败的原因与解决方法
  • 系统端实现看门狗功能
  • 算法--二分查找(二)
  • 没有网站备案可以做诚信认证嘛商城网站大概多少钱
  • 保定市场产品投放策略分析