赞
踩
mysql> create [or replace] view 视图名[(字段列表)] as select语句 [with [cascaded/local] check option];
注:视图的字段列表写不写都无所谓
mysql> select * from stu; +-----+--------+-----+---------+ | id | name | age | classid | +-----+--------+-----+---------+ | 101 | 小天 | 18 | 1001 | | 102 | 小明 | 20 | 1003 | | 103 | 小红 | 13 | 1002 | +-----+--------+-----+---------+ mysql> create or replace view view_stu as select id,name from stu where age>15; Query OK, 0 rows affected (0.02 sec) mysql> show tables; +-----------------+ | Tables_in_huazi | +-----------------+ | account | | class | | department | | employee | | mytable | | stu | | view_employee | | view_stu | +-----------------+ mysql> select * from view_stu; +-----+--------+ | id | name | +-----+--------+ | 101 | 小天 | | 102 | 小明 | +-----+--------+
mysql> select * from employee; +------+-----------+-----------------+--------+-------+--------+ | id | name | job | salary | bonus | job_id | +------+-----------+-----------------+--------+-------+--------+ | 101 | 麦当 | 后端研发 | 25000 | 5000 | 1003 | | 102 | 咕咚 | 网络运维 | 15000 | 3000 | 1003 | | 103 | 迪亚 | 测试工程师 | 12000 | 2000 | 1003 | | 104 | 米龙 | 后端开发 | 20000 | 3500 | 1003 | | 105 | 极光 | 前端开发 | 15000 | 2500 | 1003 | | 106 | 村长 | 人力资源 | 10000 | 500 | 1001 | | 107 | 五条人 | 销售工程师 | 14000 | 7000 | 1002 | | 108 | 皇帝 | 董事长 | 30000 | 10000 | 1004 | +------+-----------+-----------------+--------+-------+--------+ mysql> create or replace view view_employee(id,name,salary) as select * from employee where salary>15000; ERROR 1353 (HY000): In definition of view, derived table or common table expression, SELECT list and column names list have different column counts #报错原因:视图的字段列表和select的字段列表必须一致,否则就会报错 mysql> create or replace view view_employee(id,name,salary) as select id,name,salary from employee where salary>15000; Query OK, 0 rows affected (0.00 sec) mysql> show tables; +-----------------+ | Tables_in_huazi | +-----------------+ | account | | class | | department | | employee | | mytable | | stu | | view_employee | +-----------------+ mysql> select * from view_employee; +------+--------+--------+ | id | name | salary | +------+--------+--------+ | 101 | 麦当 | 25000 | | 104 | 米龙 | 20000 | | 108 | 皇帝 | 30000 | +------+--------+--------+
mysql> show create view 视图名\G;
mysql> show create view view_employee; +---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+ | View | Create View | character_set_client | collation_connection | +---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+ | 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 | +---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+ mysql> show create view view_employee\G; *************************** 1. row *************************** View: view_employee 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) character_set_client: utf8mb4 collation_connection: utf8mb4_0900_ai_ci
mysql> select * from 视图名;
mysql> select * from view_employee;
+------+--------+--------+
| id | name | salary |
+------+--------+--------+
| 101 | 麦当 | 25000 |
| 104 | 米龙 | 20000 |
| 108 | 皇帝 | 30000 |
+------+--------+--------+
mysql> create [or replace] view 视图名[(字段列表)] as select语句 [with [cascaded/local] check option];
mysql> select * from view_employee; +------+--------+--------+ | id | name | salary | +------+--------+--------+ | 101 | 麦当 | 25000 | | 104 | 米龙 | 20000 | | 108 | 皇帝 | 30000 | +------+--------+--------+ 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 | 皇帝 | +------+--------+
注:视图的字段列表写不写都无所谓
mysql> alter view 视图名[(字段列表)] as select语句 [with [cascaded/local] check option];
mysql> select * from stu; +-----+--------+-----+---------+ | id | name | age | classid | +-----+--------+-----+---------+ | 101 | 小天 | 18 | 1001 | | 102 | 小明 | 20 | 1003 | | 103 | 小红 | 13 | 1002 | +-----+--------+-----+---------+ mysql> select * from view_stu; +-----+--------+ | id | name | +-----+--------+ | 101 | 小天 | | 102 | 小明 | +-----+--------+ mysql> alter view view_stu as select id,name,age from stu where age>15; Query OK, 0 rows affected (0.03 sec) mysql> select * from view_stu; +-----+--------+-----+ | id | name | age | +-----+--------+-----+ | 101 | 小天 | 18 | | 102 | 小明 | 20 | +-----+--------+-----+
注:视图的字段列表写不写都无所谓
mysql> drop view [if exists] 视图名;
mysql> show tables; +-----------------+ | Tables_in_huazi | +-----------------+ | account | | class | | department | | employee | | mytable | | stu | | view_employee | | view_stu | +-----------------+ mysql> drop view if exists view_stu; Query OK, 0 rows affected (0.00 sec) mysql> show tables; +-----------------+ | Tables_in_huazi | +-----------------+ | account | | class | | department | | employee | | mytable | | stu | | view_employee | +-----------------+
注:在视图中插入数据,都是基于基表的插入条件
mysql> create view view_account as select id,name from account where money>=2000; Query OK, 0 rows affected (0.01 sec) mysql> show tables; +-----------------+ | Tables_in_huazi | +-----------------+ | account | | class | | department | | employee | | mytable | | stu | | view_account | | view_employee | +-----------------+ mysql> select * from view_account; +----+--------+ | id | name | +----+--------+ | 1 | 张三 | | 2 | 李四 | +----+--------+ mysql> insert into view_account values -> (3,'小明'); Query OK, 1 row affected (0.01 sec) mysql> select * from view_account; +----+--------+ | id | name | +----+--------+ | 1 | 张三 | | 2 | 李四 | +----+--------+ mysql> select * from account; +----+--------+-------+ | id | name | money | +----+--------+-------+ | 1 | 张三 | 2000 | | 2 | 李四 | 2000 | | 3 | 小明 | NULL | +----+--------+-------+
mysql> select * from account; +----+--------+-------+ | id | name | money | +----+--------+-------+ | 1 | 张三 | 2000 | | 2 | 李四 | 2000 | +----+--------+-------+ #将主表中的money字段修改约束条件为not null mysql> alter table account modify money int not null; Query OK, 0 rows affected (0.06 sec) mysql> show create table account\G; *************************** 1. row *************************** Table: account Create Table: CREATE TABLE `account` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(10) NOT NULL, `money` int NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='账户表' mysql> select * from view_account; +----+--------+ | id | name | +----+--------+ | 1 | 张三 | | 2 | 李四 | +----+--------+ #插入失败的原因:主表中的money字段是not null mysql> insert int view_account values -> (3,'小明'); 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 (3,'小明')' at line 1
作用:
优点:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。