I have solution where i need that id in one table starts every month from 1. I'm thinking of using month name as primary key together with id auto incerement. Am I wrong way? Does someone have any idea?
Example with MYISAM table:
CREATE TABLE table1(
yearmonth_id INT(11) NOT NULL, -- combination of year and month
id INT(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (yearmonth_id, id)
)
ENGINE = MYISAM;
INSERT INTO table1 VALUES(201110, NULL);
INSERT INTO table1 VALUES(201110, NULL);
INSERT INTO table1 VALUES(201110, NULL);
INSERT INTO table1 VALUES(201112, NULL);
INSERT INTO table1 VALUES(201112, NULL);
INSERT INTO table1 VALUES(201112, NULL);
INSERT INTO table1 VALUES(201201, NULL);
INSERT INTO table1 VALUES(201201, NULL);
INSERT INTO table1 VALUES(201201, NULL);
SELECT * FROM table1;
+--------------+----+
| yearmonth_id | id |
+--------------+----+
| 201110 | 1 |
| 201110 | 2 |
| 201110 | 3 |
| 201112 | 1 |
| 201112 | 2 |
| 201112 | 3 |
| 201201 | 1 |
| 201201 | 2 |
| 201201 | 3 |
+--------------+----+
Am I wrong way?
Exactly.
A comment from the N.B. should be an answer. Quoting it here:
Why do everyone want to tamper with auto_increment instead of creating their own triggers and implementing the logic their way.. just don't tamper with the primary key, it's a bad idea from multiple perspectives. Create a trigger to increment number in some other column and let primary key do what it does.
Every newbie web-programmer should engrave it on their table and read aloud for the 3 times a day.
The thing with primary keys is that they need to be simple, normally. the reason most systems use numbers is because then in code, you just pass the number around to represent the piece of data, with out necessarily requesting all the data, if you dont need it.
You can have a multi-column primary key with one column being a month-year string and another being a numerical auto-increment, then the auto-increment will automatically re-use numbers when the month changes. This has the advantage of being tied directly into the table definition, which makes moving the system easier.
Source: enter link description here