dtxq82489 2013-05-27 00:30
浏览 57
已采纳

使用带自动增量字段的触发器

So I am currently creating a database for a MMO (Massively mutliplayer online). This database is going to list all of the items (armor, weapon, quests, npcs, e.t.c.) in the game. I currently have it set to where each different category has it's own table within the database. I.e a table for weapons, armor, e.t.c.. The data will be displayed in a table and the name of whatever it is will be a link that will take the person to a detailed page about whatever they clicked on.

For every table I have a ID field, that auto increments so each row has it's own ID. But since I have multiple different tables the ids are going to overlap. This isn't acceptable because of how the links work. I created a 2nd field called "identifier" and the default value is based on each table. So the default value for this column in the armor table is "armor".

I then created a third field called "combined", now here's the hard part! I created a trigger where before data is inserted into the table, the trigger combines the "id" field and the "identifier" field using CONCAT and puts it in the field combined. Thus creating a unique id no matter how many tables I have. But, since the ID field auto increments, the trigger is just putting a 0 where a different number should be. Here is the trigger code:

set new.combined = concat(new.id, new.identifier)   

This is before insert.

I tried doing a after insert but kept getting an error. I've searched through questions here and can't find an answer for my problem. Any help?

  • 写回答

2条回答 默认 最新

  • dongyu9263 2013-06-01 23:07
    关注

    A solution to this is to have a separate table for sequences. Your schema then may look like this

    CREATE TABLE item_seq
    (
      id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
    );
    CREATE TABLE weapon
    (
      id INT NOT NULL PRIMARY KEY DEFAULT 0, 
      name VARCHAR(32)
    );
    CREATE TABLE armor
    (
      id INT NOT NULL PRIMARY KEY DEFAULT 0, 
      name VARCHAR(32)
    );
    

    A trigger for weapon table

    DELIMITER $$
    CREATE TRIGGER tg_weapon_beforeinsert 
    BEFORE INSERT ON weapon
    FOR EACH ROW
    BEGIN
      INSERT INTO item_seq VALUES(NULL);
      SET NEW.id = LAST_INSERT_ID();
    END$$
    DELIMITER ;
    

    A trigger for armor table will be the same

    DELIMITER $$
    CREATE TRIGGER tg_armor_beforeinsert 
    BEFORE INSERT ON armor
    FOR EACH ROW
    BEGIN
      INSERT INTO item_seq VALUES(NULL);
      SET NEW.id = LAST_INSERT_ID();
    END$$
    DELIMITER ;
    

    Now if you insert some data into both tables

    INSERT INTO weapon(name) VALUES('weapon1');
    INSERT INTO armor(name) VALUES('armor1');
    INSERT INTO weapon(name) VALUES('weapon2');
    INSERT INTO armor(name) VALUES('armor2');
    

    Content of weapon table

    | ID |    NAME |
    ----------------
    |  1 | weapon1 |
    |  3 | weapon2 |
    

    Content of armor table

    | ID |   NAME |
    ---------------
    |  2 | armor1 |
    |  4 | armor2 |
    

    Here is SQLFiddle demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:训练序列与输入层维度不一样
  • ¥15 关于Ubuntu20.04.3LTS遇到的问题:在安装完CUDA驱动后,电脑会进入卡死的情况,但可以通过键盘按键进入安全重启,但重启完又会进入该情况!
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥100 AT89C52单片机C语言调试之后再回答
  • ¥15 AT89C52单片机C语言串口助手发送数据包返回值
  • ¥15 C++数组中找第二小的数字程序纠错
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)