47 mysql
我有两种方法可以自动增加mysql中的id.
一个是SERIAL,另一个是AUTOINCREMENT.
所以假设我想创建一个表格myfriends.我可以用两种方式创建它:
1)
mysql> create table myfriends(id int primary key auto_increment,frnd_name varchar(50) not null);
Run Code Online (Sandbox Code Playgroud)
2)
mysql> create table myfriends(id serial primary key,frnd_name varchar(50) not null);
Run Code Online (Sandbox Code Playgroud)
这两者有什么区别?
要么
有没有什么方法比其他方式有优势?
请帮忙.
zam*_*uts 11
AUTO_INCREMENT是任何数字类型(int或float)的特定列的属性,有符号和无符号.插入行时,它会自动分配序号,因此您不必(例如使用LAST_INSERT_ID()).请参阅http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
SERIAL是一个别名,结合柱型铸造(BIGINT特异性), AUTO_INCREMENT,UNSIGNED和其它属性的特定列(见从下面的文档引用).请参阅http://dev.mysql.com/doc/refman/5.5/en/numeric-type-overview.html
SERIAL是BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE的别名.
整数列定义中的SERIAL DEFAULT VALUE是NOT NULL AUTO_INCREMENT UNIQUE的别名.