110 views

#1. After using the view, there is no need to rewrite the sql of the subquery every time, but this is not efficient, and it is not as efficient as our writing subquery

#2. And there is a fatal problem: the view is stored in the database. If the SQL in our program is too dependent on the view stored in the database, it means that once the SQL needs to be modified and involves the part of the view, it must Go to the database to make changes, and usually there is a special DBA responsible for the database in the company. If you want to complete the modification, you must pay a lot of communication costs. The DBA may help you complete the modification, which is extremely inconvenient

# =============================创建视图

select * from emp inner join dep on emp.dep_id = dep.id;


create view emp2dep as select emp.*,dep.name as dep_name from emp inner join dep on emp.dep_id = dep.id;


mysql> update emp2dep set name="EGON" where id=1;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from emp2dep;
+----+-----------+--------+------+--------+--------------+
| id | name      | sex    | age  | dep_id | dep_name     |
+----+-----------+--------+------+--------+--------------+
|  1 | EGON      | male   |   18 |    200 | 技术         |
|  2 | alex      | female |   48 |    201 | 人力资源     |
|  3 | wupeiqi   | male   |   38 |    201 | 人力资源     |
|  4 | yuanhao   | female |   28 |    202 | 销售         |
|  5 | liwenzhou | male   |   18 |    200 | 技术         |
+----+-----------+--------+------+--------+--------------+
5 rows in set (0.00 sec)

mysql>
mysql>
mysql> select * from emp;
+----+------------+--------+------+--------+
| id | name       | sex    | age  | dep_id |
+----+------------+--------+------+--------+
|  1 | EGON       | male   |   18 |    200 |
|  2 | alex       | female |   48 |    201 |
|  3 | wupeiqi    | male   |   38 |    201 |
|  4 | yuanhao    | female |   28 |    202 |
|  5 | liwenzhou  | male   |   18 |    200 |
|  6 | jingliyang | female |   18 |    204 |
|  7 | lili       | female |   48 |   NULL |
+----+------------+--------+------+--------+
7 rows in set (0.00 sec)

mysql>

# =============================修改视图
alter view emp2dep as 查询语句;

# =============================删除视图
drop view emp2dep;

Guess you like

Origin blog.csdn.net/qq_40808228/article/details/108501426