
Setting Default Value to value of another column
Owen,
If I understand you correctly, you have an existing table. You wish to add a
new column to the table, with the NON NULL property, and initialize the
value to that of an existing column. However, I'm assuming, since it makes
no sense for two columns to always have the same value, that the values of
this new column will change over time.
Short answer: No way to do that.
Longer answer:
Option 1) Use ALTER TABLE ... to add the new column with the NON NULL
property, but give it a default value. Then use the UPDATE command to change
the actual values of the new column, and again use ALTER TABLE to drop the
default value of the new column.
Option 2) Use ALTER TABLE ... to add the new column with the NULL property.
Use the UPDATE column to change the actual values of the new column. Use the
ALTER TABLE to change the NULLability of the new column.
Personally, I'd recommend the second option ... I think it will execute
faster.
-------------------------------------------
BP Margolin
Please reply only to the newsgroups.
When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
can be cut and pasted into Query Analyzer is appreciated.
Quote:
> I've got a database table with some existing data. I'd like to add
another
> column, not allowing NULLs. When the column is added, I want its value to
> be equal to the value of another column (TypeID being the existing column,
> TypeID2 being the new column).
> Anyone know if there is a way in the management console (design table) to
do
> this? Or is there a "modify table" command to do it?
> TIA,
> Owen