SQL Server Implementation Change table name, change column name, change constraint code

Source: Internet
Author: User

1. Modify the table name

Format: sp_rename tablename,newtablename

?
1 sp_rename tablename,newtablename

2. Modify the field name

Format: sp_rename ' Tablename.colname ', Newcolname, ' column '

?
1 sp_rename ‘tablename.colname‘,newcolname,‘column‘

3. Add a Field

Format: ALTER TABLE table_name add New_column data_type [interality_codition]

Example 1

?
1 ALTERTABLE student Addnationality varchar(20)

--Example 2 adds a column of type int, the default value is 0

?
12345 alter table student add studentName int default 0 --示例3 添加int类型的列,默认值为0,主键 alter table student add studentId int primary key default 0 --示例4 判断student中是否存在name字段且删除字段 if exists(select * from syscolumns where id=object_id(‘student‘) and name=‘name‘) beginalter table student DROP COLUMN nameend

4. Changing fields

Format: ALTER TABLE table_name ALTER COLUMN COLUMN_NAME

?
1 ALTERTABLE student ALTER COLUMN nameVARCHAR(200)

5. Delete a field

Format: ALTER TABLE table_name DROP COLUMN column_name

?
1 ALTERTABLE student DROP COLUMNnationality;

6. View field constraints

Format: SELECT * FROM information_schema.constraint_column_usage WHERE table_name = table_name

?
12 SELECTTABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME FROM information_schema.CONSTRAINT_COLUMN_USAGEWHERETABLE_NAME = ‘student‘

7. View field Default constraint expressions (that is, default values, etc.)

Format: SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = table_name

?
12 SELECTTABLE_NAME, COLUMN_NAME, COLUMN_DEFAULT FROM information_schema.COLUMNSWHERETABLE_NAME=‘student‘

8. View the field DEFAULT constraint name

Format: select name from sysobjects where OBJECT_ID (table_name) =parent_obj and xtype= ' D '

?
12 selectname from sysobjectswhere object_id(‘表?名?‘)=parent_obj andxtype=‘D‘

9. Delete field constraints

Format: ALTER TABLE tablename DROP constraint ConstraintName

?
1 ALTERTABLE student DROP CONSTRAINTPK__student__2F36BC5B772B9A0B

10. Adding field constraints

Format: ALTER TABLE tablename ADD constraint ConstraintName primary key (column_name)

--Example 1

?
12345678910111213141516171819202122232425 ALTER TABLE stuInfo ADD CONSTRAINT PK_stuNo PRIMARY KEY (stuNo) --示例2 添加主键约束(Primary Key)-- 存在主键约束PK_stuNO,则删除 IF EXISTS(SELECT * FROM sysobjects WHERE name=‘PK_stuNo‘ and xtype=‘PK‘)Alter TABLE stuInfoDrop Constraint PK_stuNoGo -- 重新添加主键约束PK_stuNO ALTER TABLE stuInfo ADD CONSTRAINT PK_stuNo PRIMARY KEY (stuNo)Go --示例3 添加 唯一UQ约束(Unique Constraint)-- 存在唯一约束UQ_stuNO,则删除 IF EXISTS(SELECT * FROM sysobjects WHERE name=‘UQ_stuID‘ and xtype=‘UQ‘)Alter TABLE stuInfoDrop Constraint UQ_stuIDGo -- 重新添加唯一约束UQ_stuID ALTER TABLE stuInfo ADD CONSTRAINT UQ_stuID UNIQUE (stuID) --示例4 添加默认DF约束(Default Constraint)-- 存在默认约束UQ_stuNO,则删除 IF EXISTS(SELECT * FROM sysobjects WHERE name=‘DF_stuAddress‘ and xtype=‘D‘)Alter TABLE stuInfo Drop Constraint DF_stuAddressGo -- 重新添加默认约束DF_stuAddress ALTER TABLE stuInfo ADD CONSTRAINT DF_stuAddress DEFAULT (‘地址不详‘) FOR stuAddress --示例5 检查CK约束(Check Constraint)-- 存在检查约束UQ_stuNO,则删除 IF EXISTS(SELECT * FROM sysobjects WHERE name=‘CK_stuAge‘ and xtype=‘C‘)Alter TABLE stuInfo Drop Cons

SQL Server Implementation Change table name, change column name, change constraint code

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.