When we delete data in a database table of SQLSever, we want the ID to start from 1 again, instead of following the command required by the last ID _ MySQL

Source: Internet
Author: User
1. if the data is important, back up the data first. 2. delete the table data SQL: DeleteFrom (#39; table name #39;) For example: DeleteFromabcd 3. execute the new SQL statement: dbcccheckident (#39; table name #39;, reseed, 0) for example: dbcccheckident (#39; abcd... 1. if the data is important, back up the data first.

II. delete table data

SQL: Delete From ('Table name ')

For example, Delete From abcd

III. execute new statements

SQL: dbcc checkident ('Table name', reseed, 0)

For example, dbcc checkident ('abcd', reseed, 0)

Note:

{

Dbcc checkident ('Table _ name', RESEED, new_reseed_value)

Check that the current identity value of the specified table meets the requirements.

Reseed, fill; then seeding, that is, re-specifying the ID value.

}

After this operation, the newly inserted data ID starts from 1.

Supplement:

The truncate table function is the same as the DELETE statement without the WHERE clause: Both DELETE all rows in the TABLE. However, truncate table is faster than DELETE and uses less system and transaction log resources.

The DELETE statement deletes a row at a time and records one row in the transaction log. Truncate table deletes data by releasing the data pages used to store TABLE data, and only records the release of pages in transaction logs.

Truncate table deletes all rows in the TABLE, but the TABLE structure, its columns, constraints, and indexes remain unchanged. The count value used by the new row ID is reset to the seed of the column. To retain the ID count value, use DELETE instead. To delete TABLE definitions and data, use the drop table statement.

For tables referenced by the foreign key constraint, the truncate table cannot be used, but the DELETE statement without the WHERE clause should be used. Because the truncate table is not recorded in the log, it cannot activate the trigger.

The truncate table cannot be used in the index View.

Update statistics must be used to maintain the index information when adding data to a TABLE that uses truncate table to delete data.

If a ROLLBACK statement exists, the DELETE operation is revoked, but the TRUNCATE statement is not.

Truncate table name [DROP/reuse storage]

Drop storage: explicitly specifies the space to release data tables and indexes.

Reuse storage: explicitly specifying that the data table and index space are not released

The truncate command restores the auto-increment field to start from 1, or you can try clearing table_a, cancel auto-increment, save, and add auto-increment again, this is also the method for restoring auto-increment segments to 1.

There are two ways to restore the table id to 1:

1. delete from your table name (the preceding method)

Dbcc checkident (your table name, reseed, 0)

2. if you want to save your data, introduce the third method: (unused)

By QINYI

Use phpmyadmin to export the database. you will find it in it.

Edit the SQL file, change the auto-increment ID, and then import it.

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.