【问题标题】:Add a second auto-increment field and allow duplicates添加第二个自动增量字段并允许重复
【发布时间】:2013-07-22 16:45:00
【问题描述】:

我有一个 EXISTING 表,其中有一个名为 ID 的主键和 6 个与发票相关的其他字段。我需要从旧表中插入值并将所有值插入到新的但最近创建的表中。旧表列出了发票编号,有时发票编号有重复。我需要我正在尝试创建的这个新列,称为invoice_id 到 AUTO_INCREMENT,当没有为将要插入的未来值插入值时,并允许在现有值和未来值上重复。当没有插入值时,需要auto_increment。

ID (primary) || invoice_ID (needs to auto_increment AND allow duplicates) || other colums
1            || 1
2            || 2
3            || 2
4            || 3

我尝试了一些命令,结果如下:

ALTER TABLE  `invoices` ADD  `invoice_ID` INT NOT NULL AUTO_INCREMENT AFTER  `ID` ,
ADD PRIMARY KEY (  `facture` )

结果:

MySQL said: 
#1075 - Incorrect table definition; there can be only one auto column and it must be 
defined as a key

也试过了:

ALTER TABLE  `invoices` ADD  `invoice_ID` INT NOT NULL AUTO_INCREMENT AFTER  `ID` ,
ADD KEY (  `invoice_ID` ) ,
ADD INDEX (  `invoice_ID` )

结果:

#1075 - Incorrect table definition; **there can be only one auto column** and it must 
be defined as a key

我也尝试了一些不同的选项,比如当然不添加为主键,但似乎只要我添加 auto_increment 请求,它就会使我的查询成为“AS PRIMARY KEY”。

【问题讨论】:

    标签: mysql phpmyadmin


    【解决方案1】:

    你可以用触发器来做到这一点。这是一个例子。

    所以你有你的旧桌子:

    drop table if exists invoices_old;
    create table invoices_old (
    invoice_ID int,
    another_column int
    );
    
    insert into invoices_old values
    (1,11),
    (2,12),
    (2,13),
    (3,14),
    (4,15),
    (5,16),
    (6,17),
    (6,18),
    (7,19);
    

    你想插入到你的新表中:

    drop table if exists invoices_new;
    create table invoices_new (
    id int not null auto_increment,
    invoice_ID int default null, /*it's important here to have a default value*/
    another_column int,
    primary key (id)
    );
    

    你复制你的数据可能是这样的:

    insert into invoices_new (invoice_ID, another_column)
    select invoice_ID, another_column 
    from invoices_old;
    

    现在您的数据已在新表中,您可以在新表上创建触发器来模拟 auto_increment 列。

    drop trigger if exists second_auto_inc;
    delimiter $$
    create trigger second_auto_inc before insert on invoices_new 
    for each row
    begin
    set @my_auto_inc := NULL;
    select max(invoice_ID) into @my_auto_inc from invoices_new;
    set new.invoice_ID = @my_auto_inc + 1;
    end $$
    delimiter ; 
    

    现在当您在新表中插入更多行时

    insert into invoices_new (another_column)
    select 20 union all select 21 union all select 22;
    

    看看你的桌子

    select * from invoices_new;
    

    它有效。

    结果:

    id  invoice_ID  another_column
    1   1           11
    2   2           12
    3   2           13
    4   3           14
    5   4           15
    6   5           16
    7   6           17
    8   6           18
    9   7           19
    16  8           20
    17  9           21
    18  10          22
    

    您可能想知道为什么在真正的 auto_increment 列中,ID 从 9 跳到 16。最近在 SO 上有一篇很好的帖子,但我现在找不到。无论如何,这不是你需要担心的。 Auto_increment 用于确保唯一性,而不是无间隙序列。

    【讨论】:

    • 谢谢fancyPants,回答这个问题一定很费时间,我还没有把它全部看完,但是我读了一遍又一遍,这比这要先进得多我知道的。我对您回答中的描述和 cmets 感到非常满意。所以我开始一步一步地回答你的答案,到目前为止,一切都很好。现在我只需要熟悉您在上面发布的这个 trigger 命令以及整个代码部分。再次感谢您的宝贵时间。哦,我需要 15 声望才能投票赞成你的答案,所以会尽快完成。
    • 很高兴听到这个消息。如果您有更多问题,请随时提问。顺便说一句,虽然你还不能投票,但你可以接受答案:)
    猜你喜欢
    • 2016-10-31
    • 1970-01-01
    • 2014-09-27
    • 2013-03-01
    • 2021-06-13
    • 1970-01-01
    • 2018-11-18
    • 2017-08-29
    • 2012-03-27
    相关资源
    最近更新 更多