Use SQL statements to add an auto-incrementing primary key or delete a primary key or an SQL primary key to a table.

Source: Internet
Author: User
Tags sql primary key

Use SQL statements to add an auto-incrementing primary key or delete a primary key or an SQL primary key to a table.

When I first met this requirement, I searched the internet and found that I couldn't do it. I had to delete the primary key column and add it again or create a temporary table before importing the data, in fact, it can be directly modified in MYSQL.

Modify the ID field to an auto-increment primary key:

Alter table 'test' change 'id' int (11) unsigned not null AUTO_INCREMENT, add primary key ('id ');

Modify the ID field to auto-increment non-primary key:

Alter table 'test' change 'id' id' int (11) unsigned not null AUTO_INCREMENT, drop primary key;

Modify the ID field to a common field:

Alter table 'test' change 'id' id' int (11) unsigned not null;




Use an SQL statement to copy all rows in the table and insert them to the end of the table (primary key auto-increment). Use an SQL statement to delete duplicate rows.

Example Table: student (id, name, sex. age), id Primary Key auto-Increment
Copy the records in the table and insert them to the end of the table.
Insert into student select name, sex, age from student;
In principle, all columns except the auto-incrementing column must be written. Note that the order of the query fields corresponds to the table, or the following:
Insert into student (sex, name, age) select sex, name, age from student;
The query field corresponds to the inserted Field

Delete duplicate rows
Delete from student where id in (select. id from student a, student B where. name = B. name and. sex = B. sex and. age = B. age and. id <> B. id)
The query statement indicates the id of all duplicate records.
Select. id from student a, student B where. name = B. name and. sex = B. sex and. age = B. age and. id <> B. id
In principle, all fields except the primary creation must be equal.
And a. id <> B. id. If this parameter is not added, the data in the table is duplicated.
Because both a and B are student tables, and they are similar to themselves,
And a. id <> B. id is compared with other records

Running, SQL server2000 Database, no problem
What database do you use?

The primary key of an SQL table is an auto-incrementing sequence. How can I delete the id?

Give you a trigger. When you delete one, the id decreases.

Create trigger delid
On Table Name
For delete
As
Begin
If @ rowcount = 1
Update table name set id = id-1 where id> (select id from deleted)
End

Only one row can be deleted. If multiple rows are deleted, follow up.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.