最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

MySQL视图简介及基本操作教程

来源:动视网 责编:小采 时间:2020-11-09 21:15:40
文档

MySQL视图简介及基本操作教程

MySQL视图简介及基本操作教程:前言 视图是数据库系统中一种非常有用的数据库对象。MySQL 5.0 之后的版本添加了对视图的支持。 认识视图 视图是一个虚拟表,其内容由查询定义。同真实表一样,视图包含一系列带有名称的列和行数据,但视图并不是数据库真实存储的数据表。 视图是从一个、多
推荐度:
导读MySQL视图简介及基本操作教程:前言 视图是数据库系统中一种非常有用的数据库对象。MySQL 5.0 之后的版本添加了对视图的支持。 认识视图 视图是一个虚拟表,其内容由查询定义。同真实表一样,视图包含一系列带有名称的列和行数据,但视图并不是数据库真实存储的数据表。 视图是从一个、多


修改视图语法

修改视图名称可以先删除,再用相同的语句创建。

#更新视图结构
alter view <视图名称> as <select语句>;
#更新视图数据相当于更新实际表,不适用基于多表创建的视图
update ....

注意:部分视图的数据是无法更新,也就是无法使用update,insert等语句更新,比如:

a、select语句包含多个表

b、视图中包含having子句

c、试图中包含distinct关键字

......

删除视图语法

drop view <视图名称>

3、视图的操作

基于单表创建视图

mysql> create view bal_view 
 -> as
 -> select * from balance;
Query OK, 0 rows affected (0.22 sec)

创建完成后,查看bal_view的结构和记录。可以发现通过视图查询到数据和通过真实表查询得到的结果完全一样。

#查询bal_view的结构
mysql> desc bal_view;
+------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| id | int(10) | NO | | NULL | |
| customerId | int(10) | NO | | NULL | |
| balance | decimal(10,2) | YES | | NULL | |
+------------+---------------+------+-----+---------+-------+
3 rows in set (0.07 sec)
#查询bal_view中的记录
mysql> select * from bal_view;
+----+------------+----------+
| id | customerId | balance |
+----+------------+----------+
| 1 | 1 | 900.55 |
| 2 | 2 | 900.55 |
| 3 | 3 | 10000.00 |
+----+------------+----------+
3 rows in set (0.01 sec)

通过创建视图的语句不难得出结论:当真实表中的数据发生改变时,视图中的数据也会随之改变。那么当视图中的数据发生改变时,真实表中的数据会变化吗?来实验一下,修改id=1的客户balance为2000。

mysql> update bal_view set balance=2000 where id=1;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0

来看一下真实表balance中的数据。

mysql> select * from bal_view where id=1;
+----+------------+---------+
| id | customerId | balance |
+----+------------+---------+
| 1 | 1 | 2000.00 |
+----+------------+---------+
1 row in set (0.03 sec)

结论:视图表中的数据发生变化时,真实表中的数据也会随之改变。

基于多表创建视图

创建视图cus_bal,共两个字段客户名称和余额。

mysql> create view cus_bal
 -> (cname,bal)
 -> as
 -> select customer.name,balance.balance from customer ,balance
 -> where customer.id=balance.customerId;
Query OK, 0 rows affected (0.05 sec)
#查看cus_bal中的数据
mysql> select * from cus_bal;
+----------+----------+
| cname | bal |
+----------+----------+
| xiaoming | 2000.00 |
| xiaohong | 900.55 |
| xiaocui | 10000.00 |
+----------+----------+
3 rows in set (0.28 sec)

修改视图

将cus_bal视图中的cname改成cusname。

mysql> alter view cus_bal
 -> (cusname,bal)
 -> as
 -> select customer.name,balance.balance from customer ,balance
 -> where customer.id=balance.customerId;
Query OK, 0 rows affected (0.06 sec)
#查看修改后视图结构。
mysql> desc cus_bal;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| cusname | char(20) | NO | | NULL | |
| bal | decimal(10,2) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

修改基于多表创建的视图

mysql> insert into cus_bal(cusname,bal) values ("ee",11);
ERROR 1393 (HY000): Can not modify more than one base table through a join view 'rms.cus_bal'

删除视图

删除视图cus_bal

drop view cus_bal;
mysql> drop view cus_bal;
Query OK, 0 rows affected (0.00 sec)

总结

文档

MySQL视图简介及基本操作教程

MySQL视图简介及基本操作教程:前言 视图是数据库系统中一种非常有用的数据库对象。MySQL 5.0 之后的版本添加了对视图的支持。 认识视图 视图是一个虚拟表,其内容由查询定义。同真实表一样,视图包含一系列带有名称的列和行数据,但视图并不是数据库真实存储的数据表。 视图是从一个、多
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top