Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between SERIAL and AUTO_INCREMENT in mysql

Tags:

mysql

People also ask

What is AUTO_INCREMENT in MySQL?

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.

What is serial MySQL?

SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE . SERIAL DEFAULT VALUE in the definition of an integer column is an alias for NOT NULL AUTO_INCREMENT UNIQUE .

What is AUTO_INCREMENT?

The auto increment in SQL is a feature that is applied to a field so that it can automatically generate and provide a unique value to every record that you enter into an SQL table. This field is often used as the PRIMARY KEY column, where you need to provide a unique value for every record you add.

What is the difference between sequence and auto increment in SQL?

The difference between auto-increment columns in SQL Server and sequences in Oracle is that: In SQL Server, you mark a column as an auto-increment column and SQL Server automatically generates new values for the column when you insert a new row.


As per the docs

SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE.

So, be careful when creating a reference to a SERIAL PK, since that reference column has to be of this exact type.


AUTO_INCREMENT is an attribute of a specific column of any numeric type (int or float), both signed and unsigned. When rows are inserted it automatically assigns sequential numbers, so you don't have to (e.g. by using LAST_INSERT_ID()). See http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

SERIAL is an alias that combines column type casting (BIGINT specifically), AUTO_INCREMENT, UNSIGNED and other attributes for a specific column (see quote from docs below). See https://dev.mysql.com/doc/refman/8.0/en/numeric-type-syntax.html

SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE.

SERIAL DEFAULT VALUE in the definition of an integer column is an alias for NOT NULL AUTO_INCREMENT UNIQUE.


From mysql doc

SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE.

SERIAL DEFAULT VALUE in the definition of an integer column is an alias for NOT NULL AUTO_INCREMENT UNIQUE.

If no value is specified for the AUTO_INCREMENT column, MySQL assigned sequence numbers automatically. You can also explicitly assign NULL or 0 to the column to generate sequence numbers. MySQL doesn't automatically decrease the autoincrement value when you delete a row. Reasons are:

  • Danger of broken data integrity (imagine multiple users perform deletes or inserts...doubled entries may occur or worse)
  • Errors may occur when you use master slave replication or transactions