SQL Add column, delete column, modify column type

Source: Internet
Author: User

ALTER TABLE name add column name data class type
Example: ALTER TABLE Student add nickname char (20)
ALTER TABLE tableName (table name) add columnName (column name) varchar (30)

Modifying the type of a column

ALTER TABLE tbname--table name
Alter COLUMN COLNAME--column name
NvarChar (50)--Data type
Not NULL

ALTER TABLE s_part_no ALTER COLUMN rohs_grade varchar (NOT NULL)

ALTER TABLE s_part_no ALTER COLUMN Rohs_grade varchar (10)

Add columns

Alter table dbo. Wf_d_task_signer Add COMMENT NVARCHAR (400)

Delete Column

ALTER TABLE dbo. Wf_d_task_notification Drop Column task_id

ALTER TABLE dbo. Wf_d_task_notification Drop Column next_task_id

Determine if a column exists

SELECT * from syscolumns where id=object_id (' table name ') and name= ' column name '

SELECT * from syscolumns where id=object_id (' q_d_exception ') and name= ' Exception_type '

--Determine if the column exists and add if it does not exist

IF not EXISTS (SELECT 1 from sys.columns WHERE object_id = object_id (' q_d_exception ') and name = ' COMMENT ')

BEGIN

ALTER TABLE q_d_exception ADD COMMENT NVARCHAR ($) NULL;

END

Modify column names

Modify table name or field name with stored procedure sp_rename

exec sp_rename ' Q_d_inmr_header. [All_signed_date] ', ' signed_date ', ' column '

SQL Add column, delete column, modify column type

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.