问题
I am interested in creating a TRIGGER
that creates a TRIGGER
when a table is created.
Poking around in mysql databases, I noticed that the tables for a schema are returned with:
select TABLE_NAME, TABLE_SCHEMA from information_schema.TABLES;
Is the following a proper method to create a TRIGGER
, to create a TRIGGER
bound to a different schema?
DELIMITER //
CREATE TRIGGER `information_schema`.`argus_table_creation`
AFTER INSERT on `TABLES`
BEGIN
--trigger here
CREATE TRIGGER `argus`.`after_argus_insert` --create trigger bound to DB 'argus', titled 'after_insert'
AFTER INSERT ON `argus_table_2013_06_27` FOR EACH ROW -- perform the trigger, `after insert`, on the `argus_table_2013_06_27` table, `for each row` that is inserted.
BEGIN
INSERT INTO argus.historic_argus_saddrdaddr(saddr, daddr)
(
SELECT saddr, daddr
FROM (SELECT NEW.saddr, NEW.daddr) as derived
WHERE NOT EXISTS (SELECT saddr, daddr FROM argus.historic_argus_saddrdaddr where historic_argus_saddrdaddr.saddr = derived.saddr and historic_argus_saddrdaddr.daddr = derived.daddr)
);
END//
END//
DELIMITER;
Reasoning:
I use a client application (rasqlinsert) that automatically creates new tables that match a given naming scheme (where you can utilize strftime(f)). I need to create a trigger on these auto-generated tables as they are generated. This is why I am looking for a solution.
Another possible solution
Is it possible to advise the developer of the software to use a command similar to CREATE TABLE n LIKE x
, but have it copy triggers?
P.S.
Contacted dev. Will roll change in with 3.0.7.11
to accept arbitrary SQL to perform along with creation of new DB. Expects code to hit dev repo by Monday.
回答1:
You cannot set triggers on tables in the information_schema
database.
Can you please provide a citation? – mbrownnyc
http://dev.mysql.com/doc/refman/5.1/en/information-schema.html
MySQL 5.1 Reference Manual
Chapter 20. INFORMATION_SCHEMA Tables
Usage Notes for the INFORMATION_SCHEMA Database
INFORMATION_SCHEMA is a database within each MySQL instance, the place that stores information about all the other databases that the MySQL server maintains. The INFORMATION_SCHEMA database contains several read-only tables. They are actually views, not base tables, so there are no files associated with them, and you cannot set triggers on them.
来源:https://stackoverflow.com/questions/17353080/create-a-trigger-to-create-a-trigger-when-a-table-is-created