开发者

MySQL constraint for numeration column

开发者 https://www.devze.com 2023-02-06 16:00 出处:网络
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 uni

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消