My SQL Server Triggers

下午3:20:00
Source



We can create a trigger user_trig in MySQL

CREATE TRIGGER user_trig BEFORE INSERT
        ON users FOR EACH ROW
        INSERT INTO trigger_time VALUES(NOW())


When we execute an insert statement on users table, the trigger user_trig is executed and current time is inserted.
Tables users and trigger_time are created as follows:

CREATE TABLE users
(
user_id varchar(45) NOT NULL,
salary int NOT NULL DEFAULT 0,
PRIMARY KEY (user_id)
)

CREATE TABLE trigger_time
(
exec_time datetime NOT NULL
)

When we 
INSERT INTO users VALUES('Kai',6000)

In table trigger_time, we can see 

To see the triggers we created
SHOW TRIGGERS;
which is the same as
SELECT * FROM information_schema.triggers; 

The trigger information is stored in triggers table in database information_schema
To delete trigger user_trig
DROP TRIGGER user_trig;
 Video: Example, create and use triggers in MySQL



備註: 一定要是root權限 GY 我測試過

grant super on *.* to 'my_user'@'localhost';
revoke super on *.* from 'my_user'@'localhost';
都不行

最後換到root就可以=..= 算了 也是合理 因為 我是要在底下去控制跨db建立
技術提供:Blogger.