ToB企服应用市场:ToB评测及商务社交产业平台

标题: MySQL之视图详解 [打印本页]

作者: 篮之新喜    时间: 2024-6-11 13:21
标题: MySQL之视图详解
什么是视图?


创建视图

  1. mysql> create [or replace] view 视图名[(字段列表)] as select语句 [with [cascaded/local] check option];
复制代码
注:视图的字段列表写不写都无所谓
  1. mysql> select * from stu;
  2. +-----+--------+-----+---------+
  3. | id  | name   | age | classid |
  4. +-----+--------+-----+---------+
  5. | 101 | 小天   |  18 |    1001 |
  6. | 102 | 小明   |  20 |    1003 |
  7. | 103 | 小红   |  13 |    1002 |
  8. +-----+--------+-----+---------+
  9. mysql> create or replace view view_stu as select id,name from stu where age>15;
  10. Query OK, 0 rows affected (0.02 sec)
  11. mysql> show tables;
  12. +-----------------+
  13. | Tables_in_huazi |
  14. +-----------------+
  15. | account         |
  16. | class           |
  17. | department      |
  18. | employee        |
  19. | mytable         |
  20. | stu             |
  21. | view_employee   |
  22. | view_stu        |
  23. +-----------------+
  24. mysql> select * from view_stu;
  25. +-----+--------+
  26. | id  | name   |
  27. +-----+--------+
  28. | 101 | 小天   |
  29. | 102 | 小明   |
  30. +-----+--------+
复制代码
  1. mysql> select * from employee;
  2. +------+-----------+-----------------+--------+-------+--------+
  3. | id   | name      | job             | salary | bonus | job_id |
  4. +------+-----------+-----------------+--------+-------+--------+
  5. |  101 | 麦当      | 后端研发        |  25000 |  5000 |   1003 |
  6. |  102 | 咕咚      | 网络运维        |  15000 |  3000 |   1003 |
  7. |  103 | 迪亚      | 测试工程师      |  12000 |  2000 |   1003 |
  8. |  104 | 米龙      | 后端开发        |  20000 |  3500 |   1003 |
  9. |  105 | 极光      | 前端开发        |  15000 |  2500 |   1003 |
  10. |  106 | 村长      | 人力资源        |  10000 |   500 |   1001 |
  11. |  107 | 五条人    | 销售工程师      |  14000 |  7000 |   1002 |
  12. |  108 | 皇帝      | 董事长          |  30000 | 10000 |   1004 |
  13. +------+-----------+-----------------+--------+-------+--------+
  14. mysql> create or replace view view_employee(id,name,salary) as select * from employee where salary>15000;
  15. ERROR 1353 (HY000): In definition of view, derived table or common table expression, SELECT list and column names list have different column counts
  16. #报错原因:视图的字段列表和select的字段列表必须一致,否则就会报错
  17. mysql> create or replace view view_employee(id,name,salary) as select id,name,salary from employee where salary>15000;
  18. Query OK, 0 rows affected (0.00 sec)
  19. mysql> show tables;
  20. +-----------------+
  21. | Tables_in_huazi |
  22. +-----------------+
  23. | account         |
  24. | class           |
  25. | department      |
  26. | employee        |
  27. | mytable         |
  28. | stu             |
  29. | view_employee   |
  30. +-----------------+
  31. mysql> select * from view_employee;
  32. +------+--------+--------+
  33. | id   | name   | salary |
  34. +------+--------+--------+
  35. |  101 | 麦当   |  25000 |
  36. |  104 | 米龙   |  20000 |
  37. |  108 | 皇帝   |  30000 |
  38. +------+--------+--------+
复制代码
查询

查看创建视图语句

  1. mysql> show create view 视图名\G;
复制代码
  1. mysql> show create view view_employee;
  2. +---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
  3. | View          | Create View
  4.                                          | character_set_client | collation_connection |
  5. +---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
  6. | view_employee | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `view_employee` (`id`,`name`,`salary`) AS select `employee`.`id` AS `id`,`employee`.`name` AS `name`,`employee`.`salary` AS `salary` from `employee` where (`employee`.`salary` > 15000) | utf8mb4              | utf8mb4_0900_ai_ci   |
  7. +---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
  8. mysql> show create view view_employee\G;
  9. *************************** 1. row ***************************
  10.                 View: view_employee
  11.          Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `view_employee` (`id`,`name`,`salary`) AS select `employee`.`id` AS `id`,`employee`.`name` AS `name`,`employee`.`salary` AS `salary` from `employee` where (`employee`.`salary` > 15000)
  12. character_set_client: utf8mb4
  13. collation_connection: utf8mb4_0900_ai_ci
复制代码
查看视图数据

  1. mysql> select * from 视图名;
复制代码
  1. mysql> select * from view_employee;
  2. +------+--------+--------+
  3. | id   | name   | salary |
  4. +------+--------+--------+
  5. |  101 | 麦当   |  25000 |
  6. |  104 | 米龙   |  20000 |
  7. |  108 | 皇帝   |  30000 |
  8. +------+--------+--------+
复制代码
修改视图布局

方式1

  1. mysql> create [or replace] view 视图名[(字段列表)] as select语句 [with [cascaded/local] check option];
复制代码
  1. mysql> select * from view_employee;
  2. +------+--------+--------+
  3. | id   | name   | salary |
  4. +------+--------+--------+
  5. |  101 | 麦当   |  25000 |
  6. |  104 | 米龙   |  20000 |
  7. |  108 | 皇帝   |  30000 |
  8. +------+--------+--------+
  9. mysql> create or replace view view_employee as select id,name from employee where salary>15000;Query OK, 0 rows affected (0.00 sec)mysql> select * from view_employee;+------+--------+| id   | name   |+------+--------+|  101 | 麦当   ||  104 | 米龙   ||  108 | 皇帝   |+------+--------+
复制代码
注:视图的字段列表写不写都无所谓
方式2

  1. mysql> alter view 视图名[(字段列表)] as select语句 [with [cascaded/local] check option];
复制代码
  1. mysql> select * from stu;
  2. +-----+--------+-----+---------+
  3. | id  | name   | age | classid |
  4. +-----+--------+-----+---------+
  5. | 101 | 小天   |  18 |    1001 |
  6. | 102 | 小明   |  20 |    1003 |
  7. | 103 | 小红   |  13 |    1002 |
  8. +-----+--------+-----+---------+
  9. mysql> select * from view_stu;
  10. +-----+--------+
  11. | id  | name   |
  12. +-----+--------+
  13. | 101 | 小天   |
  14. | 102 | 小明   |
  15. +-----+--------+
  16. mysql> alter view view_stu as select id,name,age from stu where age>15;
  17. Query OK, 0 rows affected (0.03 sec)
  18. mysql> select * from view_stu;
  19. +-----+--------+-----+
  20. | id  | name   | age |
  21. +-----+--------+-----+
  22. | 101 | 小天   |  18 |
  23. | 102 | 小明   |  20 |
  24. +-----+--------+-----+
复制代码
注:视图的字段列表写不写都无所谓
删除视图

  1. mysql> drop view [if exists] 视图名;
复制代码
  1. mysql> show tables;
  2. +-----------------+
  3. | Tables_in_huazi |
  4. +-----------------+
  5. | account         |
  6. | class           |
  7. | department      |
  8. | employee        |
  9. | mytable         |
  10. | stu             |
  11. | view_employee   |
  12. | view_stu        |
  13. +-----------------+
  14. mysql> drop view if exists view_stu;
  15. Query OK, 0 rows affected (0.00 sec)
  16. mysql> show tables;
  17. +-----------------+
  18. | Tables_in_huazi |
  19. +-----------------+
  20. | account         |
  21. | class           |
  22. | department      |
  23. | employee        |
  24. | mytable         |
  25. | stu             |
  26. | view_employee   |
  27. +-----------------+
复制代码
在视图中插入数据


注:在视图中插入数据,都是基于基表的插入条件
  1. mysql> create view view_account as select id,name from account where money>=2000;
  2. Query OK, 0 rows affected (0.01 sec)
  3. mysql> show tables;
  4. +-----------------+
  5. | Tables_in_huazi |
  6. +-----------------+
  7. | account         |
  8. | class           |
  9. | department      |
  10. | employee        |
  11. | mytable         |
  12. | stu             |
  13. | view_account    |
  14. | view_employee   |
  15. +-----------------+
  16. mysql> select * from view_account;
  17. +----+--------+
  18. | id | name   |
  19. +----+--------+
  20. |  1 | 张三   |
  21. |  2 | 李四   |
  22. +----+--------+
  23. mysql> insert into view_account values
  24.     -> (3,'小明');
  25. Query OK, 1 row affected (0.01 sec)
  26. mysql> select * from view_account;
  27. +----+--------+
  28. | id | name   |
  29. +----+--------+
  30. |  1 | 张三   |
  31. |  2 | 李四   |
  32. +----+--------+
  33. mysql> select * from account;
  34. +----+--------+-------+
  35. | id | name   | money |
  36. +----+--------+-------+
  37. |  1 | 张三   |  2000 |
  38. |  2 | 李四   |  2000 |
  39. |  3 | 小明   |  NULL |
  40. +----+--------+-------+
复制代码
  1. mysql> select * from account;
  2. +----+--------+-------+
  3. | id | name   | money |
  4. +----+--------+-------+
  5. |  1 | 张三   |  2000 |
  6. |  2 | 李四   |  2000 |
  7. +----+--------+-------+
  8. #将主表中的money字段修改约束条件为not null
  9. mysql> alter table account modify money int not null;
  10. Query OK, 0 rows affected (0.06 sec)
  11. mysql> show create table account\G;
  12. *************************** 1. row ***************************
  13.        Table: account
  14. Create Table: CREATE TABLE `account` (
  15.   `id` int NOT NULL AUTO_INCREMENT,
  16.   `name` varchar(10) NOT NULL,
  17.   `money` int NOT NULL,
  18.   PRIMARY KEY (`id`)
  19. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='账户表'
  20. mysql> select * from view_account;
  21. +----+--------+
  22. | id | name   |
  23. +----+--------+
  24. |  1 | 张三   |
  25. |  2 | 李四   |
  26. +----+--------+
  27. #插入失败的原因:主表中的money字段是not null
  28. mysql> insert int view_account values
  29.     -> (3,'小明');
  30. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'int view_account values
  31. (3,'小明')' at line 1
复制代码
视图的with查抄选项




视图的更新


视图的作用和优点


作用:

优点:

视图的使用规则

案例




免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4