mysql中SERIAL和AUTO_INCREMENT有什么區別

[英]What is the difference between SERIAL and AUTO_INCREMENT in mysql


I have come across two ways to increment the ids in mysql automatically.

我有兩種方法可以自動增加mysql中的id。

One is SERIAL and other is AUTOINCREMENT.

一個是SERIAL,另一個是AUTOINCREMENT。

So Suppose i want to create a table myfriends. I can create it in two ways like:

所以假設我想創建一個表格myfriends。我可以用兩種方式創建它:

1)

1)

mysql> create table myfriends(id int primary key auto_increment,frnd_name varchar(50) not null);

2)

2)

mysql> create table myfriends(id serial primary key,frnd_name varchar(50) not null);

What is difference between the two ?

這兩者有什么區別?

OR

要么

Do anyone way has advantages over other ?

有沒有什么比其他方式有優勢?

Please Help.

請幫忙。

3 个解决方案

#1


36  

As per the docs

按照文檔

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

SERIAL是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.

因此,在創建對SERIAL PK的引用時要小心,因為該引用列必須是這種類型。

#2


9  

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

AUTO_INCREMENT是任何數字類型(int或float)的特定列的屬性,有符號和無符號。插入行時,它會自動分配序號,因此您不必(例如,通過使用LAST_INSERT_ID())。請參閱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 http://dev.mysql.com/doc/refman/5.5/en/numeric-type-overview.html

SERIAL是一個別名,它將列類型轉換(特定於BIGINT),AUTO_INCREMENT,UNSIGNED和特定列的其他屬性組合在一起(請參閱下面的文檔引用)。請參閱http://dev.mysql.com/doc/refman/5.5/en/numeric-type-overview.html

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

SERIAL是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.

整數列定義中的SERIAL DEFAULT VALUE是NOT NULL AUTO_INCREMENT UNIQUE的別名。

#3


0  

From mysql doc

來自mysql doc

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

SERIAL是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.

整數列定義中的SERIAL DEFAULT VALUE是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:

如果沒有為AUTO_INCREMENT列指定值,MySQL會自動分配序列號。您還可以顯式為列分配NULL或0以生成序列號。刪除行時,MySQL不會自動降低自動增量值。原因是:

  • 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
  • 使用主從復制或事務時可能會發生錯誤

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/11/16/a3e2b6df1b8bf132623fcb1cfdc73653.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com