MySQL Composite primary key self-growing field

Source: Internet
Author: User
Tags mysql manual

The ID of Google analytics is generally like: ua-18xxxxx-1, ua-18xxxxx-4, we can divide it into two segments: field 1:ua-18xxxxx refers to the ID of the account Field 2:1, 4 appended to field 1, respectively, refers to two sites under the account name. There was a time to struggle with such an ID how to maintain, until recently checked to the MySQL manual only found that the original MySQL is very good support for this kind of automatic growth field of composite primary key, so field 2 is the storage engine automatically calculated. Calculation method: MAX (auto_increment_column) + 1 WHERE prefix=given-prefix [1].

Not much to say, look directly at SQL:

mysql> create table  ' _test '   (    ->  ' C1 '  varchar (20)  NOT NULL DEFAULT  ', --  primary key one:varchar    ->  ' C2 '  int (one)  NOT NULL AUTO_INCREMENT, --  primary key two: auto-increment sequence     ->  ' C3 '  varchar (255)  ,    -> PRIMARY KEY  (' C1 ', ' C2 ')  --   Note the sequencing here     -> )  ENGINE=MyISAM DEFAULT CHARSET=latin1; query ok, 0 rows affected  (0.04 sec) Mysql>mysql> insert into   ' _test '   (' C1 ', ' C3 ')  VALUES  (' AAA ', ' Robin home '),    ->  ( ' AAA ', ' Robin blog '),    ->  (' AAA ', ' robin resume '),     ->  (' BBB ', ' Lily home '),    ->  (' BBB ', ' lily blog '); Query ok, 5 rows affected  (0.00 sec) records: 5  duplicates: 0  warnings: 0mysql>mysql > SELECT * FROM  ' _test '; +-----+----+--------------+| c1  | c2  | c3           |+-----+----+--------------+|  aaa |  1 | robin home   | |  aaa |  2 | robin blog   | |  aaa |  3 | robin resume | |  bbb |  1 | lily home    | |  bbb |  2 | lily blog    |+-----+----+--------------+5  rows in set  (0.00 sec) mysql>

It is worth noting that this feature of MySQL only applies to the MyISAM and BDB engines.


MySQL Composite primary key self-growing field

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.