开发者

Trying to add Primary Key to MYSQL

Here is my code:

ALTER TABLE `$table` ADD PRIMARY KEY `id` INT( 11 ) NOT NULL AUTO_INCREMENT FIRST 

It keeps giving me errors about the syntax on adding a primary key. What will make it go through?

Also, I am trying开发者_Go百科 to add the new column id to the table.


Try this syntax:

ALTER TABLE `$table` 
  ADD `id` INT( 11 ) NOT NULL AUTO_INCREMENT FIRST,
  ADD PRIMARY KEY(`id`);


The primary question has been solved, however one very important point. Avoid using $variable inside your SQL queries. Read about SQL Injection


 ALTER TABLE `$table`
    ADD `id` INT(11) NOT NULL AUTO_INCREMENT,
    ADD PRIMARY KEY (`id`);

Also see the MySQL documentation on ALTER TABLE

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜