Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Adding Column Delete Data

I am wondering if I add a column to my table using:

alter table Jobs
add Complete boolean

Will this erase all of the data that is currently in my table? I am using SQL Server Management Studio 2012

like image 966
Chase Ernst Avatar asked Sep 22 '26 15:09

Chase Ernst


2 Answers

Not at all. This adds a column whose value is NULL, but it has no direct effect on the data already in the table.

As a note: boolean is not a data type in SQL Server, so that particular statement will not work. You can use bit, tinyint, smallint, or int instead. Or a char(1) to store 'T' and 'F'.

And a second note: If your table is very large, then adding a column can be take a while, because all the data may need to be rewritten. Although the values are NULL, each page in SQL Server contains a bit for the NULL values in all the columns. If this steps over a byte boundary, then data needs to be rearranged on the page -- and perhaps new pages allocated for the data. In any case, each page needs to be modified to include the NULL values. For this reason, it is recommended to add columns to empty tables, if possible.

like image 197
Gordon Linoff Avatar answered Sep 24 '26 07:09

Gordon Linoff


No it will not erase your data. It will add a column and all the existing data will be assigned null values in this column.

like image 38
jakubiszon Avatar answered Sep 24 '26 06:09

jakubiszon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!