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

【MySQL】第三周作业

【MySQL】第三周作业

  • 1、在数据库example下创建college表。
  • 2、在student表上创建视图college_view。
  • 3、查看视图college_view的详细结构
  • 4、 更新视图。
  • 5 、修改视图,
  • 6 、删除视图college_view

1、在数据库example下创建college表。

College表内容如下所示

    字段名     字段描述     数据类型      主键      外键     非空      唯一     自增number     学号        INT(10)      是       否       是       是        否name       姓名      VARCHAR(20)    否       否       是       否        否major      专业       VARCHAR(20)   否       否       是       否        否age        年龄        INT(5)       否       否       否       否        否
mysql> create database example;
Query OK, 1 row affected (0.04 sec)mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| db_test            |
| example            |
| information_schema |
| mysql              |
| performance_schema |
| rele               |
| sys                |
| user               |
+--------------------+
8 rows in set (0.00 sec)mysql> use example;
Database changed
mysql> CREATE TABLE college(->     number INT(10) NOT NULL UNIQUE PRIMARY KEY COMMENT '学号',->     name VARCHAR(20) NOT NULL COMMENT '姓名',->     major VARCHAR(20) NOT NULL COMMENT '专业',->     age INT(5) COMMENT '年龄'-> );
Query OK, 0 rows affected, 2 warnings (0.11 sec)mysql> show tables;
+-------------------+
| Tables_in_example |
+-------------------+
| college           |
+-------------------+
1 row in set (0.00 sec)mysql> desc college;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| number | int         | NO   | PRI | NULL    |       |
| name   | varchar(20) | NO   |     | NULL    |       |
| major  | varchar(20) | NO   |     | NULL    |       |
| age    | int         | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.02 sec)

2、在student表上创建视图college_view。

视图的字段包括student_num、student_name、student_age和department。ALGORITHM设置为MERGE类型,并且为视图加上WITH LOCAL CHECK
OPTION条件

mysql> CREATE  VIEW-> college_view(student_num,student_name,student_age,department)-> AS SELECT number,name,age,major FROM college-> WITH LOCAL CHECK OPTION;
Query OK, 0 rows affected (0.02 sec)

3、查看视图college_view的详细结构

mysql> show create view college_view \g
+--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| View         | Create View                                                                                                                                                                                                                                                                                                               | character_set_client | collation_connection |
+--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| college_view | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `college_view` (`student_num`,`student_name`,`student_age`,`department`) AS select `college`.`number` AS `number`,`college`.`name` AS `name`,`college`.`age` AS `age`,`college`.`major` AS `major` from `college` WITH LOCAL CHECK OPTION | utf8mb4              | utf8mb4_0900_ai_ci   |
+--------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
1 row in set (0.01 sec)

4、 更新视图。

向视图中插入3条记录。记录内容如下表所示
umer name major age
0901 张三 外语 20
0902 李四 计算机 22
0903 王五 计算机 19

mysql> insert into college_view values(0901,'张三',20,'外语');
Query OK, 1 row affected (0.02 sec)mysql> insert into college_view values(0902,'李四',22,'计算机');
Query OK, 1 row affected (0.01 sec)mysql> insert into college_view values(0903,'王五',19,'计算机');
Query OK, 1 row affected (0.21 sec)mysql> select * from college_view;
+-------------+--------------+-------------+------------+
| student_num | student_name | student_age | department |
+-------------+--------------+-------------+------------+
|         901 | 张三         |          20 | 外语       |
|         902 | 李四         |          22 | 计算机     |
|         903 | 王五         |          19 | 计算机     |
+-------------+--------------+-------------+------------+
3 rows in set (0.00 sec)

5 、修改视图,

使其显示专业为计算机的信息,其他条件不变

方法一:
CREATE OR REPLACE ALGORITHM=UNDEFINED VIEW
college_view(student_num,student_name,student_age,department)
AS SELECT number,name,age,major
FROM college WHERE major=’计算机’
WITH LOCAL CHECK OPTION;

方法二:
ALTER ALGORITHM=UNDEFINED VIEW
college_view(student_num,student_name,student_age,department)
AS SELECT number,name,age,major
FROM college WHERE major=’计算机’
WITH LOCAL CHECK OPTION;

mysql> ALTER ALGORITHM=UNDEFINED VIEW->     college_view(student_num,student_name,student_age,department)->     AS SELECT number,name,age,major->     FROM college WHERE major='计算机'->     WITH LOCAL CHECK OPTION;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from college_view;
+-------------+--------------+-------------+------------+
| student_num | student_name | student_age | department |
+-------------+--------------+-------------+------------+
|         902 | 李四         |          22 | 计算机     |
|         903 | 王五         |          19 | 计算机     |
+-------------+--------------+-------------+------------+
2 rows in set (0.01 sec)

6 、删除视图college_view

DROP VIEW college_view;

mysql> drop view college_view;
Query OK, 0 rows affected (0.01 sec)mysql> select * from college_view;
ERROR 1146 (42S02): Table 'rele.college_view' doesn't exist
http://www.lryc.cn/news/354741.html

相关文章:

  • 香橙派 Kunpeng Pro使用教程:从零开始打造个人私密博客
  • 深入探索:中文字符的编码与转移字符的奥秘
  • Ubuntu中 petalinux 安装 移植linux --tftp/tftp-hpa服务的方法
  • JVM(内存区域划分、类加载机制、垃圾回收机制)
  • C语言---基础内容(万字)
  • c语言从入门到函数速成(完结篇)
  • 关于linux磁盘告警问题
  • 冯喜运:5.27黄金暴跌大阴后出现“暂定符”今日黄金原油操作策略
  • 前端JS必用工具【js-tool-big-box】学习,获取全球重点城市时间
  • BioTech - 将蛋白质的 PDB 格式文件 转换成 mmCIF 格式文件 (Python)
  • 【编程题-错题集】奇数位丢弃(模拟 - 规律)
  • Docker安装MongoDB(Linux版)
  • 【设计模式】JAVA Design Patterns——Commander(指挥官模式)
  • 解决vue3项目vite打包忽略.vue扩展名
  • Vue基础(数据绑定、export使用)
  • 【传知代码】基于图神经网络的知识追踪方法(论文复现)
  • Vue与React、Angular的比较
  • LINQ(二) —— 流式语句
  • 怎么查看MySQL服务的最大连接,已经使用的连接数?怎么配置最大连接数?
  • 微信小程序毕业设计-跑腿系统项目开发实战(附源码+演示视频+LW)
  • stm32通过esp8266连接阿里云平台代码讲解
  • 突发!某大厂机房掉电,MySQL数据库无法启动,紧急恢复过程...
  • SpringCloudAlibaba:6.2RocketMQ的普通消息的使用
  • vue+echart :点击趋势图中的某一点或是柱状图,出现弹窗,并传输数据
  • 2024年上半年软考什么时候查成绩?附查询流程
  • css3实现0.5px边框
  • U-Net网络
  • 不拍视频,不直播怎么在视频号卖货赚钱?开一个它就好了!
  • 【vue-5】双向数据绑定v-model及修饰符
  • [STM32-HAL库]AS608-指纹识别模块-STM32CUBEMX开发-HAL库开发系列-主控STM32F103C8T6