MySQL Set primary key auto Grow

Source: Internet
Author: User

Create a database and create a table.

SQL code
  1. mysql> Create database ssh2;
  2. Query OK, 1 row affected (0.04 sec)
  3.   
  4. mysql> use SSH2;
  5. Database changed
  6. mysql> Create table user(
  7. -id integer primary key,
  8. - firstname varchar(+) not null,
  9. - lastname varchar(+) not null,
  10. -Age integer
  11. );
  12. Query OK, 0 rows Affected (0.46 sec)


Add a self-increment function to the primary key:
Java code
    1. mysql> ALTER TABLE user modify ID integer auto_increment;
    2. Query OK, 1 row affected (0.28 sec)
    3. Records: 1 Duplicates: 0 Warnings: 0



In this way, the ID of the primary key in the user table above can be self-increment.









add default values and auto-increment to the primary key ID above.
Java code
  1. mysql> ALTER TABLE user modify ID integer auto_increment;
  2. Query OK, 0 rows affected (0.39 sec)
  3. Records: 0 Duplicates: 0 Warnings: 0
  4.   
  5. mysql> ALTER TABLE user Modify ID integer default ' 1 ';
  6. Query OK, 0 rows affected (0.16 sec)
  7. Records: 0 Duplicates: 0 Warnings: 0
  8.   
  9. mysql> ALTER TABLE user modify ID integer auto_increment;
  10. Query OK, 1 row affected (0.28 sec)
  11. Records: 1 Duplicates: 0 Warnings: 0






MySQL Get system time:
Java code
    1. mysql> ALTER TABLE user add createtime timestamp default current_timestamp;
    2. Query OK, 2 rows affected (0.17 sec)
    3. Records: 2 Duplicates: 0 Warnings: 0












MySQL Settings primary key cannot be empty and will grow automatically (there is no default value set here, but the default is 1, starting from 1 growth.) ) and get the system default date:


If you want to create a table of the same data structure from a table:

CREATE TABLE Newtbname SELECT * from tab where 0;

This new table has the same data structure as the original table but does not set the primary key auto-growth

ALTER TABLE Modify ID integer primary key auto_increment

This ensures that the primary key ID is automatically increased in the database




MySQL Set primary key auto Grow

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.