为了账号安全,请及时绑定邮箱和手机立即绑定

创建mysql可更新视图

标签:
MySQL

Summary: in this tutorial, we will show you how to create an updateable view and update data in the underlying table through the view.

In MySQL, views are not only read-only but also updateable. However in order to create an updateable view, the SELECT statement that defines the view has to follow several following rules:

  • The SELECT statement must only refer to one database table.

  • The SELECT statement must not use GROUP BY or HAVING clause.

  • The SELECT statement must not use DISTINCT in the column list of the SELECT clause.

  • The SELECT statement must not refer to read-only views.

  • The SELECT statement must not contain any expression (aggregates, functions, computed columns…)

When you create updateable views, make sure that you follow the rules above.

Example of creating updateable view

Let’s practice with an example of creating an updateable view.

First, we create a view named officeInfo against the offices table. The view refers to three columns of the offices table:  officeCodephone and city.

CREATE VIEW officeInfo  AS     SELECT officeCode, phone, city    FROM offices

Next, we can query data from the officeInfo view using the SELECT statement.

SELECT * FROM officeInfo

Then, we can change the phone number of the office with officeCode 4 through the officeInfo view by using the UPDATE statement.

UPDATE officeInfo SET phone = '+33 14 723 5555' WHERE officeCode = 4

Finally, to see the change, we can select the data from the officeInfo view by executing following query:

SELECT * FROM officeInfo WHERE officeCode = 4

In this tutorial, we have shown you how to create an updateable view and how to update data in the underlying table through the view.

Related Tutorials

原文链接:http://outofmemory.cn/mysql/view/create-sql-updatable-views

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消