아마도 당신은 이미 해결책을 찾았고, 7 개월이 지난 이후로 나는 도움이되지 않을 수도 있습니다.
주제로 돌아가서 두 개의 열 (invoicetype, invoicenumber)이있는 구성된 기본 키가 있음을 발견했습니다. 따라서 invoicetype, invoicenumber 쌍의 중복이 없어야합니다. 매년 auto_increment를 재설정하면 'A', 두 행에 1, 2013 년에 인보이스 한 번, 2014 년에 인보이스가 중복 될 수 있습니다. 따라서 기본 키를 제거하여 위반을 방지 할 수 있습니다 기본 키 제약 조건의 대신 모든 행을 고유하게 만들기 위해 auto_incremented 열이있는 기본 키 (알맞은 테이블 있음)를 정의 할 수 있습니다. 그런 다음 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';
가 그럼 난을 list_id로 다른 테이블을 정의하는 것입니다 :
위의 표는 INVOICENUMBER
의 값을 설정하는 데 사용할 수 있습니다 CREATE TABLE `list_id` (
`id` int unsigned NOT NULL auto_increment,
`id_inc` int unsigned NOT NULL, -- number of invoice
`the_year` date NOT NULL, -- year corresponding to the number of invoice
PRIMARY KEY (id)
) COMMENT='' ENGINE='MyISAM';
인보이스 테이블에 삽입 된 현재 행의 경우 (invoicedate의 첫 번째 인보이스 인 경우 1, 인보이스 발행 연도에 상응하는 최대 id_inc 값) pl 우리 하나, 그렇지 않으면). 행은 송장 테이블에을 삽입하기 전에 유형의 트리거를 사용하여 완료됩니다. 따라서 새로운 인보이스를 삽입하기 전에 invoicenumber의 값을 결정해야합니다. 새 송장의 연도와 같은 값을 가진 열이있는 열 list_id의 테이블에 레코드가 없으면 1이됩니다. 이 경우 list_id 테이블에 값 (1,2014) (id_inc, year)이있는 새 레코드를 삽입 할 수 있습니다. 새 청구서의 연도와 동일한 값을 가진 열이있는 list_id 테이블에 레코드가있는 경우 id_inc의 최대 값에 1을 더한 값이됩니다. 이 경우 list_id 테이블에 값 (7,2014) (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;
트리거에는 두 가지 기능이 있습니다.
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;
두번째는 풀어서 값을 갖는 테이블 list_id로부터 id_inc의 최대 값을 결정한다 : 첫 번째는 (파라미터로 주어진) 현재 인보이스 년과 the_year 동일 갖는 list_id로 테이블의 행의 수를 결정 (매개 변수로 주어진) 현재 송장의 올해와 the_year 동등의 :
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;
그래서 하나의 트리거, 두 가지 기능 및 INVOICENUMBER의 점진을 제어하는 하나 개의 테이블을 가질 수 있습니다.
희망이 도움이됩니다. 그것은 나를 위해 일했다!
복제 http://stackoverflow.com/questions/8923114/how-to-reset-auto-increment-in-mysql –