In-depth analysis of SQL auto-growth settings and Deletion

Source: Internet
Author: User

Identity () indicates that the value of this field increases by 1 every time a statement is inserted,
Syntax
IDENTITY [(seed, increment)]
Parameters
Seed
The value (indicating the seed) used by the first row loaded to the table ).
Increment
Increment value, which is added to the ID value (increment) of the previous loaded row ).
Both seed and increment must be specified, or neither of them is specified. If neither of them is specified, the default value is (1, 1 ).
For example:
Create table WorkRoom
(WorkRoomID int not null identity (1, 1) primary key,
WorkRoomName Varchar (50) null,
TeacherName Varchar (10) null,
WorkRoomManager Varchar (10) null,
WorkRoomPlan Varchar (max) null,
Purl Varchar (50) null,
Remarks Varchar (max) null,
)
Modify s to create and delete tags:
ALTER TABLE TABLENAME DROP COLUMN ID
Alter table tablename add id int identity (1, 1) not null
 
How to remove the column self-growth (identity) using SQL statements)
==========================================================
How to remove the column self-growth (identity) using SQL statements and cancel the identity command:
** You cannot use alter to change the existing auto-increment field to a non-auto-increment field.
For example, if alter table a alter id int is used, the auto-increment attribute is not removed.
You can do this by modifying the system table (this method may have unpredictable results, with caution ...)
Sp_configure 'Allow updates', 1
GO
Reconfigure with override
GO
Update syscolumns set colstat = colstat & 0x0000
Where id = o B j e c t_id ('table name') and name = 'field name'
GO
Sp_configure 'Allow updates', 0
---------------------------------
-- Discount method
Alter table a add xxx int
Update a set xxx = id
Alter table a drop column id
Exec sp_rename 'xxx', 'id', 'column'
==========================================================
The first method can be removed, but it may cause problems in the future. We recommend that you use the second method. The statements I use are also posted now. It may be helpful for friends who need them.
To delete the auto-increment attribute of The userid field in the zx_user table, follow these steps:
// Create a temporary field
Alter table zx_user add useridtmp int null
// Assign a value
Update zx_user set useridtmp = userid
// Delete the primary key
Alter table zx_user drop pk_zx_user
// Delete a field
Alter table zx_user drop column userid
// Add a field
Alter table zx_user add userid int null
// Restore the value back
Update zx_user set userid = useridtmp
// Delete temporary Fields
Alter table zx_user drop column useridtmp
// Modify the new field.
Alter table zx_user alter column userid int not null
// Primary key. Multiple fields are used as primary keys separated by commas (,), for example, primary key (userid, xxxxxx)
Alter table zx_user add primary key (userid)
==========================================================
When deleting a primary key, you do not know your primary key name. Please use the following method to query:
Select * from syso B j e c ts where xtype = 'pk

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.