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
精彩评论