sql 将自动递增主键添加到现有表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 将自动递增主键添加到现有表相关的知识,希望对你有一定的参考价值。

/* Add new auto-incrementing field */
ALTER TABLE dbo.YourTable
ADD ID INT IDENTITY;

/* Make it the primary key */
ALTER TABLE dbo.YourTable
ADD CONSTRAINT PK_YourTable
PRIMARY KEY(ID);

/*
http://dev.mysql.com/doc/refman/5.5/en/alter-table-examples.html
Add new auto-incrementing field and
make it the primary key
MySQL version 5.5.42-cll 
*/
	
ALTER TABLE YourTable 
ADD NewFieldName INT UNSIGNED NOT NULL AUTO_INCREMENT FIRST,
ADD PRIMARY KEY (NewFieldName);
  
/* move column to the first position
http://stackoverflow.com/questions/29838981/how-to-move-mysql-table-column-to-first-position
WHEN MOVING ID COLUMN, SOMETHING BREAKS AND IDS TRY TO DUPLICATE RESULTING IN 
THE RECORD NOT BEING SAVED GORDON NOV 2015
*/
ALTER TABLE YourTable MODIFY COLUMN YourFieldName int(10) FIRST 
   
/* move column to anything other than the first position
http://stackoverflow.com/questions/6805426/how-to-move-columns-in-a-mysql-table
*/
ALTER TABLE YourTable MODIFY `column_you_want_to_move` DATATYPE AFTER `anothercolumn`

以上是关于sql 将自动递增主键添加到现有表的主要内容,如果未能解决你的问题,请参考以下文章

将自动递增的主键插入 Access 表

Oracle中如何将自动增量添加到现有表中

sqlserver字段如何自动更新

向oracle中的现有表添加自动增量主键[重复]

sql 使用自动递增主键创建表

怎么用SQL语句CREATE TABLE的主键为“自动增加”