請問下面倆個個sql陳述句那個正確,為什么?
1,mysql> create table a1(id int primary key auto_increment,name text);
2,mysql> create table a2(id int auto_increment,name text);
當我的想要自動增長的欄位添加主鍵約束之后,則可以編譯成功
mysql> create table a1(id int primary key auto_increment,name text);
Query OK, 0 rows affected (0.01 sec)
當該欄位沒有添加主鍵約束,直接自增長則會出現錯誤
mysql> create table a2(id int auto_increment,name text);
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key(想要自動增長必須給他一個鍵(key))
uj5u.com熱心網友回復:
第一個正確。如果想讓自增長的列不是主鍵,可以設定隨意一個鍵就可以。create table tab_01(
id int auto_increment not null,
test varchar(10),
key(id)
)
uj5u.com熱心網友回復:
第一條創建陳述句是對的uj5u.com熱心網友回復:
結帖率:0%
當您的問題得到解答后請及時結貼.
http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html
8、如何給分和結貼?
http://bbs.csdn.net/help#post_dispose
uj5u.com熱心網友回復:
這應該是為了能讓自增列不重復,且速度快,才 要求強制使用一個key,說白了就是要求用索引uj5u.com熱心網友回復:
欄位使用auto_increment來設定自增時,這個欄位必須創建索引,如果沒有創建索引,創建資料表則就會報錯。但是在MySQL中,primary key即主鍵約束,會默認創建一個唯一索引,默認名為primary.所以,auto_increment與primary ke一起使用是,不需要再創建索引就能成功的創建資料表了。如果要想使,mysql> create table a2(id int auto_increment,name text);能夠成功創建資料表,添加索引即可,在下面我添加一個普通索引:create table a2(id int auto_increment,name text,index(id));這樣添加后就能夠成功創建資料表了//因為欄位id中使用了auto_increment屬性,所以要創建這個欄位的索引。至于索引的型別,在這就不作講解了
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/80884.html
標籤:MySQL
上一篇:MHA部署問題
下一篇:各位大神求幫助
