I have situation when a I have to store complex data number in the database. Something like that 21/2011
where 21
is document number, but 2011
is document year. So I need some constraint to handle uniqueness becaus开发者_如何学Pythone there is document with number 21/2010
and 21/2012
.
How can I create such constraint when numbering starts each from 1 and there is uniqueness check for complex number value?
CREATE TABLE documents
(
year INT NOT NULL,
no INT AUTO_INCREMENT,
PRIMARY KEY (year, no)
) ENGINE=MyISAM;
INSERT
INTO documents(year)
VALUES (2010),
(2010),
(2011);
SELECT *
FROM documents;
Unfortunately, the AUTO_INCREMENT
part will only work for MyISAM
tables, however, uniqueness check will work in InnoDB
as well, though you will have to provide the numbers explicitly.
What is the problem, you can simply create an unique key over these both columns.
An other way would be to store "21/2010" as single varchar column.
精彩评论