Search code examples
sqlmysqlgetlatest

SQL - how to let sql auto set the next id


how can I let sql/mysql set the next id, that I have no problems with posts at the same time?


Solution

  • CREATE TABLE Persons
    (
    ID int NOT NULL AUTO_INCREMENT,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255)
    )
    

    Auto_Increment can be used as ID as there wont any duplicate value.
    When insert value for the table :

    INSERT INTO Persons (FirstName,LastName)
    VALUES ('Hello','World')
    

    The table will look like this.

    ID | FirstName | LastName
    1  |   Hello   |  World