After SQL 2000 is cleared, let the table id start from 1 and perform other database operations.

Source: Internet
Author: User

Truncate table name (
Unrecoverable truncate identifiers will be reordered)
LinqUtil. db. executeCommand ("truncate table warehousing"); // warehousing is the TABLE name. This operation is more efficient and the counter is cleared. However, tables with foreign keys cannot be used, you can delete the foreign key and then use
LinqUtil. Db. ExecuteCommand ("delete ioinfo"); // The counters cannot be cleared because of this deletion.
LinqUtil. Db. ExecuteCommand ("dbcc checkident (ioinfo, reseed, 0)"); // clear the counter. ioinfo indicates the table name.
LinqUtil. Db. SubmitChanges ();
Run the command after clearing the table.
Dbcc checkident (Table Name, reseed, start value)
For example:
You need to clear the table Employee ID and start from 1.
Dbcc checkident (Employee, reseed, 0) -- you added a piece of data and added 1 from the start value.
Bytes -------------------------------------------------------------------------------------
Use SQL statements to clear data from all tables in the database
Recently, I found that the database is too large and has insufficient space. Therefore, I plan to clear all the data in the database, but there are a lot of tables, one by one, which is really troublesome, therefore, we want to use SQL statements to clear all data at a time. three methods are found for clearing. the database used is ms SQL SERVER.

1. Search all table names and construct an SQL statement. Copy codeThe Code is as follows: declare @ trun_name varchar (8000)
Set @ trun_name =''
Select @ trun_name = @ trun_name + 'truncate table' + [name] + ''from sysobjects where xtype = 'U' and status> 0
Exec (@ trun_name)

This method is applicable when there are not many tables. Otherwise, the number of tables exceeds the length of the string and cannot be completely cleared.

2. Use a cursor to clear all tables Copy codeThe Code is as follows: declare @ trun_name varchar (50)
Declare name_cursor cursor
Select 'truncate table' + name from sysobjects where xtype = 'U' and status> 0
Open name_cursor
Fetch next from name_cursor into @ trun_name
While @ FETCH_STATUS = 0
Begin
Exec (@ trun_name)
Print 'truncated table' + @ trun_name
Fetch next from name_cursor into @ trun_name
End
Close name_cursor
Deallocate name_cursor

This is self-built and can be called as a stored procedure. It can clear all the table data at a time, and it can also be selected to clear the table.

3. Use Microsoft's undisclosed stored procedures
Exec sp_msforeachtable "truncate table? "
This method can clear all tables at a time, but does not apply filter conditions.
-----------------------------------------------------------------------------
Clear table SQL statements
You can use delete to clear a table.
Delete from t table name
You can also use the truncate command
Truncate table name
Create table name [TABLE constraints]
(Column name 1 Data Type [default value 1, column constraint 1]
(Column name 2 Data Type [Default Value 2, column constraint 2]
...
Column name n data type [default value n, column constraint n]
[TABLESPACE name]
[STORAGE (STORAGE clause)]
[ENABLE constraint name]
[DISABLE constraint name]
? Insert data
Insert into table name [(column name 1,…)]
VALUES (value 1, value 2 ,..., Value n)
? Modify data
UPDATE table name SET column name 1 = expression 1, column name 2 = expression 2 ,...
WHERE condition;
? Delete data
You cannot delete existing data from a table.
Statement Syntax:
Delete from table name WHERE condition;
? Modify Table Structure
Add a new column to an existing table. Syntax:
Alter table name ADD (new column name data type (length ));
For example:
Alter table student add (department char (8 ));
B. Add the data types of existing columns.
For example:
Alter table student modify (NAME VARCHAR2 (25 ));
? Table Deletion
Delete an existing table. Syntax:
Drop table name;
For example:
Drop table emp;
? Query statement
The syntax of the SELECT command is:
SELECT [DISTINCT | ALL] {* | mode name.] {table name | view name |
Snapshot name]. *… | {Expression [column alias]…} } [, [Schema name.] {table name |
View name | }.*... | Expression [column alias]…
FROM [mode name.] {table name | view name | snapshot name} [@ database chain name] [table alias]
[, [Mode name.] {table name | view name | snapshot name} [@ database chain name]
[Table alias]…
[WHERE condition]
[Start with condition connect by condition]
[Group by expression [, expression]… [HAVING condition]
[UNION | union all | INTERSECT | MINUS] SELECT command
[Order by {expression | position} [ASC | DESC] [, {expression | position [ASC | DESC]}]…]

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.