How to alter table add column with named default constraint and named foreign key constraint?
Solution 1:
Adding both constraints in one statement wasn't as easy as I thought it would be and there didn't seem to be many examples out there (at least I wasn't able to find any very easily), so I thought I'd share how I did it here and maybe someone can suggest a better way?
ALTER TABLE [table name] ADD
[New Column Name] [Column Type]
CONSTRAINT [constraint name] DEFAULT ([default value]) NOT NULL,
CONSTRAINT [constraint name] FOREIGN KEY ([New Column Name])
REFERENCES [Other Table] ([Foreign ID])
Example:
ALTER TABLE tableA ADD
myNewColumn BIGINT
CONSTRAINT myNamedConstraint_df default (1) NOT NULL,
CONSTRAINT myNamedConstraint_fk FOREIGN KEY (myNewColumn)
REFERENCES tableB (tableBPrimaryKeyID)