Add/Modify/Resize/Change/Delete/Drop/Re Name MS SQL Server Table Column Using T-SQL
Add/Modify/Resize/Change/Delete/Drop/Re Name MS SQL Server Table Column Using T-SQL
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
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.
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 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.
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*)