当前位置:网站首页>MySQL -- triggers and views
MySQL -- triggers and views
2022-07-19 05:31:00 【Learn to put down ta】
trigger
Trigger is a special stored procedure , happen DML The operation that is automatically performed during the operation , For example, log records :
add to 、 View and delete
grammar :
create trigger + Trigger Name
< before | after> Define trigger timing
<insert | delete | update> Definition DML Operation type
on <tb_name>
for each row// Declare as row level operation , If one line is affected, it will trigger once
begin
-- sql If there is only one sentence, you can not write begin and end
end;
Example :
create trigger tri_text
after insert on students
for each row// Row level operation
insert into stulogs(time,log_text) value(now(),concat(' add to ',NEW.stu_num,' Student information '));
show triggers;// Check triggers
drop trigger + Trigger Name ;// Delete trigger
NEW
update、insert in NEW Update 、 The inserted data
OLD
update、delete in OLD Update 、 Data before deletion
Trigger Usage Summary
advantage
- The trigger is ⾃ Action and execution ⾏ Of , When executing on the table related to the trigger ⾏ Responsive DML In operation ⽴ That is to hold ⾏;
- Triggers can cascade data in a table ( Associated operations ), It helps to ensure the integrity of data ;
- Triggers can be used on DML The data of the operation goes into ⾏ More complex legitimacy verification
shortcoming
- send ⽤ If there is a problem with the business logic implemented by the trigger, it will be difficult to locate , Later maintenance is difficult ;
- ⼤ Quanshi ⽤ Triggers can easily lead to messy code structure , Increases the complexity of the program ;
- When the trigger operates, the amount of data ⽐ a ⼤ when , Of board ⾏ Efficiency meeting ⼤⼤ Reduce .
Suggest
- On the Internet ⽹ term ⽬ in , Adaptation triggers should be avoided ;
- For concurrency, it is not ⼤ The item ⽬ You can choose to make ⽤ stored procedure , But on the Internet ⽹ lead ⽤ It is not advocated to make ⽤ Stored
cheng ( reason : When storing procedures, the logic that implements the business is handed over to the database for processing ,⼀ It increases or decreases the load of the database ,
⼆ It is not conducive to database migration )
View
Concept of view
View , Namely From the database ⼀ A table or multiple tables are constructed from the data queried according to specific conditions Virtual table
The function of view
- Security : If we authorize the data sheet directly to ⽤ Door operation , that ⽤ You can CRUD All numbers in the data table
According to the , Add ⼊ We want to enter some data in the data table ⾏ Protect , Public data can be ⽣ Make a view , to grant authorization
⽤ User access view ;⽤ Users can obtain the data exposed in the data table by querying the view , from ⽽ To achieve the data table
Some data pairs of ⽤ User hidden . - simplicity : If the data we need to query comes from multiple data tables , You can make ⽤ Multi table join query ;
We match the results of these linked table queries to ⽤ Household opening ,⽤ Users can get more information directly by querying the view
Table data , More convenient operation .
Create view
grammar :
create view <view_name>
AS
select Query statement
View data properties
Query operation : If new data is added to the data table ,⽽ And this data is full ⾜ Query statement when creating view
Pieces of , You can also query the new data through the query view ; When deleting the original table full ⾜ When querying the data of criteria , Also from the
Delete .
The new data : If you add data to the view , The data will be added to the original data table
Delete data : If you delete data from a view , The data will also be deleted from the original table
Modify the operating : If you modify the data , The data in the original data table will also be modified
The use of view ⽤ Suggest : Simplify operations on complex queries , And will not enter the data ⾏ In case of modification, it can make ⽤ View .
Query view structure
desc + View name ;
Modify the view
Mode one :
create or replace view + View name AS + Search statement ;
Mode two :
alter view + View name AS + Search statement ;
Delete view
drop view + View name ;
It is different from deleting view data , When deleting the entire view , The data in the table will not be deleted .
边栏推荐
- 表字段属性查询
- [first launch in the whole network] automatic analysis of JVM performance problems
- 特殊指针的一些应用
- C语言的指针函数
- Two or three things to know about operation and maintenance safety
- MySQL 查询当天、本周,本月、上一个月的数据
- 常量与常量指针
- Is the software testing training of lemon class reliable? This successful case of counter attack from the training class tells you
- 8.数据仓库之ODS层搭建
- Excel template export of easypoi
猜你喜欢
随机推荐
Easypoi excel simple export
5.数据采集通道搭建之业务分析
idea导入本地包
typedef
面试官:大量请求 Redis 不存在的数据,从而影响数据库,该如何解决?
mysql 缓存策略和解决方案
User mode protocol stack - UDP implementation based on netmap
Excel imports long data and changes to 000 at the end
UML (use case diagram, class diagram, object diagram, package diagram)
Log4j的使用
使用Flink SQL传输市场数据1:传输VWAP
Syntax differences between PgSQL and Oracle (SQL migration records)
6.数据仓库搭建之数据仓库设计
BUUCTF web WarmUp
MySQL lock
4.东软跨境电商数仓项目--数据采集通道搭建之用户行为数据采集通道搭建(2022.6.1-2022.6.4)
Parent components plus scoped sometimes affect child components
Excel template export of easypoi
Performance bottleneck finding - Flame graph analysis
【全网首发】一个月后,我们又从 MySQL 双主切换成了主-从








