【问题标题】:MySQL Create Trigger Insert Content into another table from the same databaseMySQL 创建触发器将内容插入同一数据库中的另一个表
【发布时间】:2013-12-07 13:16:26
【问题描述】:

我想创建一个触发器来创建一个新的表行。我是“触发语言”的新手,需要您的帮助。

我的数据库如下所示。

user
----------------------------------------------------
| userid | username | password | full_name | email |
----------------------------------------------------
| 67     | testuser | xxxxxxxx | thefullna | email |
----------------------------------------------------
| 68     | testuse2 | xxxxxxxx | thefulln2 | email |
----------------------------------------------------
| 69     | testuse3 | xxxxxxxx | thefulln3 | email |
----------------------------------------------------

highscore_easy
------------------------------------------------------
| ID   | user     | time     | date | level | userid |
------------------------------------------------------
| 200  | testuser | 11113233 | date | 444   | 0      |
------------------------------------------------------
| 201  | testuse2 | 11113235 | date | 444   | 0      |
------------------------------------------------------
| 203  | testuse3 | 11113236 | date | 444   | 0      |
------------------------------------------------------

如果创建了一个新用户,则会在“用户”表中创建一个新行。创建新行时,我想创建一个触发器,在“highscore_easy”中创建一个“DEFAULT”行。

默认行必须采用“user”表中填写的用户名,默认行中的时间、日期和级别可以为“0”,“highscore_easy”中的USERID必须与用户ID相同在“用户”表中。

所以如果我现在创建一个用户:

user
----------------------------------------------------
| userid | username | password | full_name | email |
----------------------------------------------------
| 88     | example  | xxxxxxxx | thefullna | email |
----------------------------------------------------

highscore_easy
------------------------------------------------------
| ID   | user     | time     | date | level | userid |
------------------------------------------------------
| 200  | example  | 0        | 0    | 0     | 88     |
------------------------------------------------------

ID 将在 highscore_easy 中自行创建。

如果我犯了一些错误,对不起我的英语不好。如果你能帮助我,那就太好了。

也许有一种方法可以在没有触发器的情况下解决这个问题,但我不知道其他解决方案。

【问题讨论】:

    标签: php mysql sql database triggers


    【解决方案1】:

    你可以试试这样的:

    DELIMITER $$
    CREATE TRIGGER `ai_user` AFTER INSERT ON user
      FOR EACH ROW BEGIN
      INSERT INTO highscore_easy 
      SET 
        user   = NEW.username, 
        userid = NEW.userid, 
        time   = 0, 
        date   = 0, 
        level  = 0;
    END
    

    在单个触发器中,您可以根据需要进行任意数量的插入:

    DELIMITER $$
    CREATE TRIGGER `ai_user` AFTER INSERT ON user
      FOR EACH ROW BEGIN
    
      INSERT INTO highscore_easy 
      SET 
        user   = NEW.username, 
        userid = NEW.userid, 
        time   = 0, 
        date   = 0, 
        level  = 0;
    
      INSERT INTO highscore_expert
      SET 
        user   = NEW.username, 
        userid = NEW.userid, 
        time   = 0, 
        date   = 0, 
        level  = 0;
    
    END
    

    【讨论】:

    • 您好,谢谢您的回答。我只是无法执行它向我显示的 SQL 命令:pastebin.de/37813
    • 是否可以向触发器添加多个表?喜欢 -> INSTER INTO highscores_easy、highscores_normal、highscores_expert SET
    • 如果您从命令行执行此查询,请记住在最后一行之后添加 $$,因为将分隔符更改为 $$ :) 这样应该没问题:pastebin.de/37815
    • 是的,您可以在触发器内有多个插入,但作为单独的查询,因为 MySQL 不支持 INSERT INTO table1,table2,... 等语法。我已经更新了我的回复。
    猜你喜欢
    • 1970-01-01
    • 2019-05-20
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    相关资源
    最近更新 更多