AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.
SQL AUTO INCREMENT |
|
SQL > Advanced SQL >
AUTO_INCREMENT
AUTO_INCREMENT is used in MySQL to create a numerical primary key value for each additional row of data. SQL AUTO_INCREMENT automatically generates a unique sequential integer for each new row inserted, making it ideal for primary key columns. By default it starts at 1 and increments by 1, but both values can be customized.
SyntaxThe syntax for AUTO_INCREMENT is as follows: ExampleAssume we want to create a table that consists of a primary key, last name, and first name. We run the following CREATE TABLE statement: Upon creation, there is no data in this table. We insert the first value: Now the table contains the following data: Table USER_TABLE
We then insert the second value: Now the table has the following values: Table USER_TABLE
Notice when we insert the first row, Userid is set to 1. When we insert the second row, Userid increases by 1 and becomes 2. By default, AUTO_INCREMENT starts with 1 and increases by 1. To change the default starting value, we can use the ALTER TABLE command as follows: where [New Number] is the starting value we want to use. The AUTO INCREMENT interval value is controlled by the MySQL Server variable auto_increment_increment and applies globally. To change this to a number different from the default of 1, use the following command in MySQL: mysql>
where [interval number] is the interval value we want to use. So, if we want to set the interval to be 5, we would issue the following command: mysql>
Frequently Asked Questions
|
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.