Add/Modify/Resize/Change/Delete/Drop/Re Name MS SQL Server Table Column Using T-SQL

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

Add/Modify/Resize/Change/Delete/Drop/Re name MS SQL Server Table Column using T SQL

As a software developer or Sql Server Administrator most often we need to change a column datatype or length or rename even delete or drop. So Add/Modify/Resize/Change/Delete/Drop/Rename operations is a very common tas in our daily lives. !ets start an e"ample# !et you start inserting data into a ta$le $ut getting the ERROR: String or binary data would be truncated. %he reason is you are trying to insert data which e"ceeds any of your e"isting column length. So in this scenario you have to increase the column size programatically. Another common pro$lem is Insert Error: Column name or number of supplied values does not match table definition. %he pro$lem is you are trying to insert n no of columns data into a ta$le whereas ta$le has n&' columns. So in this scenario you need to add a column using %& Sql. ( now i will descri$e how we can do this. Focus rea: '. Add a new column to a ta$le ). Modify or Resize an e"isting column *. Rename an e"isting column +. Drop/Delete/Remove an e"isting column ,. Rename a %a$le -. Drop/Delete/Remove an e"isting %a$le

dd a new column to a table:


%he A!%.R %A/!. statement include an ADD clause0 followed $y the new column specification li e datatype 1 length.
ALTER TABLE Articles ADD Notes VARCHAR(MAX) ALTER TABLE Sales ADD PartnerComission DECIMAL(1 !") ALTER TABLE LineItem ADD SecStat#s INT N$T N%LL

Note: 2f you have a ta$le with data then you can3t add a not nulla$le column. %he solution is first add a null column 1 then update this column data 1 then change allownull to false. 2f your ta$le has no data then you can easily do what you want.

!odify or Resi"e an e#isting column:


Almost same as adding a new column e"cept ADD will $e changed to A!%.R C(!4M5. ( now we will change the allow null constraint of previous partnercomission column to false.
ALTER TABLE Sales ALTER C$L%MN PartnerComission DECIMAL(1 !") N$T N%LL&

Note: %o change allow null property of a column to 6alse all rows must have contains the value for this column otherwise you will get 7ERROR: $Sales$ table% &nable to modify table. Cannot insert the value '&(( into column $)able'ame$* table $dbo.Sales$+ column does not allow nulls. I'SER) fails. )he statement has been terminated. 7 5ow we want to reduce the column size of 5otes column to '888. So our query should $e#
ALTER TABLE Articles ALTER C$L%MN Notes VARCHAR(1''')&

Note: 9eep in mind that if the ta$le Articles contains length of any 5otes column data greater than '888 then you will get the ERROR: String or binary data would be truncated. %he another change is you may want to change the datataype of an e"isting column. %hen must eep in mind that the new datatype must $e cmpati$le with e"isting datatype otherwise you may loose data or get the ERROR: Conversion failed when converting the ,revious -ata)ype value $... $ to data type 'ew -ata)ype.

Rename an e#isting column:


EXEC s()rename *o+,name - .Ta+leName/$l0Col#mnName.! *ne1name - .Ne1Col#mnName.! *o+,t2(e - .C$L%MN.

6or more details on sp:rename you can read MS2SD5 Article.

-rop.-elete.Remove an e#isting column:


/y using the DR(; C(!4M5 clause0 one can remove an e"isting column. %his permanently deletes the column and all of its contents or data. 6or e"ample0 the following command drops the 5otes column from the Articles ta$le.
ALTER TABLE Articles DR$P C$L%MN Notes

Rename a )able:
%he $uiltin 7sp:rename7 procedure can $e used to change or rename the name of an e"isting ta$le. 2n this case0 the e"isting name and new name must $e provided.
EXEC s()rename .$l0Ta+leName.! .Ne1Ta+leName.

6or more details on sp:rename you can read MS2SD5 Article.

-rop.-elete.Remove an e#isting )able:


2f you need to permanently delete or remove an e"isting ta$le then you can use DR(; %A/!. statement which will permanently delete your ta$le as well as data. 9eep in mind that if the ta$le under a relationship then you can not delete master ta$le without deleting the chaild ta$le first. %he synta" for delete command is given $elow#
DR$P TABLE Ta+leName

ALTER TABLE amar ADD CONSTRAINT pk_fff PRIMARY KEY (e_id) ALTER TABLE amar alter !l"m# e_id i#t #!t #"ll alter ta$le amar add !#%trai#t k &e k(%alar' $et(ee# )**** a#d +****) alter ta$le amar add !#%trai#t df defa"lt(),-.+)f!r p&_#! alter ta$le amar alter !l"m# amar /ar &ar(0*)

You might also like