SQL Server Modify the default constraint

Source: Internet
Author: User

Modify the default constraint using the SSMS database management tool

1, connect the database, select Data table-Right click-"Select Design."

2, in the Table Designer window-"Select the data column to modify-" in the column properties found in the default value binding-"to modify.

3. Click Save (or Ctrl+s)-"Close Table Designer-" Refresh table-"Reopen Table Designer to view.

To modify the default constraint by using a T-SQL script

The default constraint modification rule must first delete the existing default constraint, and then recreate it with the new definition to modify the DEFAULT constraint using Transact-SQL.

Grammar:

--Using the database
Use database name;
Go
--Determine if the constraint you want to create already exists
if exists (select * from sysobjects where name= constraint name)
ALTER TABLE name DROP CONSTRAINT constraint name;
Go
--Add a default value constraint
ALTER TABLE name ADD CONSTRAINT constraint name default constraint value for column name;
Go

Example:

--Using the database
Use TESTSS;
Go
--Determine if the constraint you want to create already exists
if exists (select * from sysobjects where name= ' default1 ')
ALTER TABLE test1 drop constraint default1;
Go
--Add a default value constraint
ALTER TABLE TEST1 add constraint default2 default.
Go

Summarize

1. There can be only one default constraint per field.
2. If the default constraint is set to a value greater than the length allowed by the field, the allowable length of the field is truncated.
3. Cannot be added to a field with an identity attribute or timestamp.
4, if the data type of the field is a user-defined type, and the default value is bound on this data type, the default value is not allowed to be used again.

SQL Server Modify the default constraint

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.