如何在mysql中重置自动增值?
我的公司有不同类型的发票。 例如: H00001/2013 . . . H99999/2013 及 T00001/2013 . . . T99999/2013 问题是,新年的人数在增加。 如何为每个新年重置自动增值 这是我当前的代码:如何在mysql中重置自动增值?,mysql,Mysql,我的公司有不同类型的发票。 例如: H00001/2013 . . . H99999/2013 及 T00001/2013 . . . T99999/2013 问题是,新年的人数在增加。 如何为每个新年重置自动增值 这是我当前的代码: CREATE TABLE `invoices` ( `invoicenumber` mediumint unsigned NOT NULL auto_increment, `invoicetype` enum('A','B')
CREATE TABLE `invoices` (
`invoicenumber` mediumint unsigned NOT NULL auto_increment,
`invoicetype` enum('A','B') NOT NULL,
`date` date NOT NULL,
`client` varchar(100) NOT NULL,
PRIMARY KEY (invoicetype,invoicenumber)
) COMMENT='' ENGINE='MyISAM';
您可以使用ALTERTABLE语句重置自动增量值。用于重置自动增量值的ALTER TABLE语句的语法如下所示:
ALTER TABLE table_name AUTO_INCREMENT = VALUE;
编辑:
如果你不想每年都运行这个查询,那么你有另外两个选择来做这样的事情,因为我知道这两个
嘿,如果您正在为数据库使用任何客户端应用程序,如MysqlWorkBench或MYSQL查询浏览器 然后,您可以执行以下步骤来设置自动增量编号-
- 右键单击表格并转到ALTERTABLE
- 选择选项选项卡
- 在下面,您可以找到自动递增标签,您可以重置数字
- 也许你已经找到了解决方案,而我给出的答案可能没有用,因为已经七个月了
回到主题,我注意到您有一个包含两列(invoicetype、invoicenumber)的组合主键这样就不能有重复的invoicetype和invoicenumber对了如果您每年重置自动增量,则可能会有重复对,如“A”,两行1,一张发票来自2013年,另一张发票来自2014年。因此您可以删除该主键,以防止违反主键约束。您可以使用自动递增的列定义主键(任何适当的表都有主键),以使每一行都唯一。然后您可以为invoicenumber列定义一个自动递增机制(我将返回到这个问题)
首先,我将这样定义发票表:
CREATE TABLE `invoices` (
`id` int unsigned NOT NULL auto_increment,
`invoicenumber` mediumint unsigned NOT NULL,
`invoicetype` enum('A','B') NOT NULL,
`invoicedate` date NOT NULL, -- not recomended to use reserved words for column names like date
`client` varchar(100) NOT NULL,
PRIMARY KEY (id)
) COMMENT='' ENGINE='MyISAM';
然后我将定义另一个表列表\u id:
上表可用于为发票表中插入的当前行设置invoicenumber的值(如果它是invoicedate年份的第一张发票,则为1,否则为id_inc的最大值(对应invoicedate年份)加1)。使用发票表的插入前类型的触发器完成行。因此,在插入新发票之前,我必须确定invoicenumber的值。如果表list_id中没有列值等于新发票年份的记录,则该值将为1。在这种情况下,我可以在表list_id中插入一条值为(12014)(id_inc,year)的新记录。如果表list_id中有记录,且该列的值等于新发票的年份,则该值将是id_inc的最大值加1。在这种情况下,我可以在表list_id中插入一条新记录,其值为(72014)(id_inc,year)。触发器如下所示:
CREATE TRIGGER `increment_or_reset_new_year`
BEFORE INSERT ON `invoices`
FOR EACH ROW
thisTrigger : begin
declare new_id_year int(11);
declare nr_invoices_year int(11);
declare new_invoice_begin int(11);
declare current_year_row int(11);
set current_year_row = year(new.invoice_date);
set nr_invoices_year = get_nr_invoices_year(current_year_row);
if(get_nr_invoices_year(current_year_row) < 1) then
set new.invoicenumber = 1;
insert into list_id(id_inc,the_year) values (1,current_year_row);
leave thisTrigger;
end if;
if(get_nr_invoices_year(current_year_row) >= 1) then
set new.invoicenumber = get_max_id(year(new.invoice_date)) + 1;
set new_id_year = get_max_id(year(new.invoice_date)) + 1;
insert into list_id(id_inc,the_year) values(new_id_year,year(new.invoice_date));
end if;
end;
第二个参数根据表list_id确定id_inc的最大值,表list_id的对应值等于当前发票的年份(作为参数给出):
所以我可以有一个触发器,两个函数和一个表来控制invoicenumber的递增希望这有帮助!这对我有用 在DB表中,可以使用以下代码重置计数器:
ALTER TABLE tablename AUTO_INCREMENT = 1
重复您的意思是我需要每年执行该SQL查询吗?@user2862056是的,您必须每年运行该查询,或者有其他选项,如cron作业或事件调度器。
create function get_nr_invoices_year(invoice_year int) returns int
begin
declare nr_invoices_year int(11);
select count(*) into nr_invoices_year from lista_id where the_year = invoice_year;
return nr_invoices_year;
end;
create function get_max_id(year_invoice int) returns int
begin
declare max_id_year int(11);
select max(id_inc) into max_id_year from invoices.lista_id where the_year =year_invoice;
return max_id_year;
end;
ALTER TABLE tablename AUTO_INCREMENT = 1