I have two tables, both already populated with data, and with the following columns:
Table1
jobType char
jobDesc varchar
New_table
jobID int
jobType char
I want to replace Table1.jobType with a jobID column, such that Table1 will now reference to New_table using jobID. Any existing data in Table1.jobType must be “translated” to the appropriate jobID.
The basic drill:
Start with the original job table
Create the new job type table (don’t forget to populate it)
Alter the table dbo.job to add a new job_type_id column
Update the job table, mapping the value of job_type_id based on the [old] job_type column’s value.
Alter the new column to make it non-nullable. This will fail until you’ve ensured that all rows have a non-null value for job_type_id:
Add the new foreign key constraint needed
The last and irrevocable step is to drop the old column. This will break any queries, stored procedures, etc. that reference this column…but you already did your homework in this department and have coordinated all necessary changes, right?