InnoDB:
Is it possible to have the auto increment column of a composite primary key restart from 1 every time the 'date' column value changes?
as in:
but using auto_increment
Is it possible to have the auto increment column of a composite primary key restart from 1 every time the 'date' column value changes?
Code:
CREATE TABLE `odr_orders` (
`dte` DATE NOT NULL DEFAULT '2013-01-01',
`odrId` SMALLINT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`odrId`, `dte`)
)
COLLATE = 'utf8_bin'
ENGINE = InnoDB
AUTO_INCREMENT = 1;
Code:
INSERT INTO
odr_orders
(
dte
)
VALUES
(
DATE_FORMAT( NOW(), '%Y-%m-%d' )
)
Code:
SET @D:= DATE_FORMAT( NOW(), '%Y-%m-%d' );
SET @I:= (
SELECT
MAX( odrId ) + 1
FROM
odr_orders
WHERE
dte = @D
);
INSERT INTO
odr_orders
(
dte
, odrId
)
VALUES
(
@D
, @I
)