Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

Daily Counter with auto_increment

$
0
0
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?
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' )
        )

as in:
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
        )

but using auto_increment

Viewing all articles
Browse latest Browse all 13329

Trending Articles