2012-09-09 59 views
2

我有一個表中的兩個字段(標題,內容),我插入一些值...:設置AUTO_INCREMENT值,以舊錶項

+--------------------------------------+--------------------------+ 
| title        | content     | 
+--------------------------------------+--------------------------+ 
| hello word       | this is my first content | 
+--------------------------------------+--------------------------+ 
| smoke Smoking is bad for your health | But i love it   | 
+--------------------------------------+--------------------------+ 

如果我添加一個名爲ID的AUTO_INCREMENT領域,以前輸入值取ID = 0:

+---+--------------------------------------+--------------------------+ 
|ID | title        | content     | 
+---+--------------------------------------+--------------------------+ 
| 0 | hello word       | this is my first content | 
+---+--------------------------------------+--------------------------+ 
| 0 | smoke Smoking is bad for your health | But i love it   | 
+---+--------------------------------------+--------------------------+ 

,我需要這樣的:

+---+--------------------------------------+--------------------------+ 
|ID | title        | content     | 
+---+--------------------------------------+--------------------------+ 
| 1 | hello word       | this is my first content | 
+---+--------------------------------------+--------------------------+ 
| 2 | smoke Smoking is bad for your health | But i love it   | 
+---+--------------------------------------+--------------------------+ 

到正確的ID設置爲舊的值的唯一方法是對HP +的foreach?或者我忘記了與sql/mysql做更快,並自動正確的ID值?

感謝您的幫助。

+0

您是否在使用類似phpMyAdmin的內容將此字段ID添加到auto_increment中?或者你如何添加這個領域? – dbf

+0

@dbf我嘗試了2種方法(phpmyadmin和代碼)。我的代碼: ALTER TABLE'blog' ADD'id' INT(255)NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST –

回答

2

那麼,現在是我的時間來幫助你:P。

您將SQL代碼放入php頁面或phpmyadmin在線代碼編輯器中?有時phpmyadmin在線編輯失敗,嘗試在你的PHP頁面,它工作正常。

+0

其確實如此,謝謝! –

2
ALTER TABLE MyTable 
ADD ID INT IDENTITY 
+0

這是否會自動使它成爲PRIMARY KEY? :) – dbf

+2

要使它成爲主鍵,我會使用 ALTER TABLE MyTable ADD CONSTRAINT PRIMARY KEY(ID) 我希望這有助於! – Mandar

相關問題