草庐IT

mysql - 添加第二个自增字段并允许重复

coder 2023-10-12 原文

我有一个 EXISTING 表,它有一个名为 ID 的主键和 6 个与发票相关的其他字段。我需要从旧表中插入值并将所有值插入到新的但最近创建的表中。旧表列出了发票编号,有时发票编号重复。我需要我正在尝试创建的这个新列,称为 invoice_id 到 AUTO_INCREMENT 当没有为将要插入的 future 值插入任何值时,并允许对现有值和 future 值进行重复。当没有插入值时,需要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 请求,它就会使我的查询“作为主键”。

最佳答案

您可以使用触发器来完成。这是一个例子。

所以你有你的旧表:

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;

看看你的 table

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 用于确保唯一性,而不是无缝序列。

关于mysql - 添加第二个自增字段并允许重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17793020/

有关mysql - 添加第二个自增字段并允许重复的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  3. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  4. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  5. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  6. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  7. ruby-on-rails - RSpec:避免使用允许接收的任何实例 - 2

    我正在处理旧代码的一部分。beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)endRubocop错误如下:Avoidstubbingusing'allow_any_instance_of'我读到了RuboCop::RSpec:AnyInstance我试着像下面那样改变它。由此beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)end对此:let(:sport_

  8. ruby - 可以通过多少种方法将方法添加到 ruby​​ 对象? - 2

    当谈到运行时自省(introspection)和动态代码生成时,我认为ruby​​没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby​​的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资

  9. ruby - 如何在 Ruby 中向现有方法定义添加语句 - 2

    我注意到类定义,如果我打开classMyClass,并在不覆盖的情况下添加一些东西我仍然得到了之前定义的原始方法。添加的新语句扩充了现有语句。但是对于方法定义,我仍然想要与类定义相同的行为,但是当我打开defmy_method时似乎,def中的现有语句和end被覆盖了,我需要重写一遍。那么有什么方法可以使方法定义的行为与定义相同,类似于super,但不一定是子类? 最佳答案 我想您正在寻找alias_method:classAalias_method:old_func,:funcdeffuncold_func#similartoca

  10. ruby-on-rails - 添加回形针新样式不影响旧上传的图像 - 2

    我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司

随机推荐