I need to add a NOT NULL column to an existing (populated) table that will be a foreign key to another table. This brings about two problems:
When you add the column, its value cannot be null - using a default is not an option (unless it is removed later on) because the database logic is used in the server side validation when a user enters a new record i.e. when the application tries to add a record with this field having a null value, the database throws an error that the application catches and returns to the user, prompting them to correct it.
The column has a foreign key constraint, meaning its value MUST exist in the foreign table as well.
What is the best way to go about this?
You can add a not null column at the time of table creation or you can use it for an existing table. In the above table, we have declared Id as int type that does not take NULL value. If you insert NULL value, you will get an error. Here is the query to add a not null column in an existing table using alter command.
ALTER TABLE students ADD FOREIGN KEY (student_id) REFERENCES points(id); To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax: ALTER TABLE students ADD CONSTRAINT fk_student_id FOREIGN KEY (student_id) REFERENCES points(id);
It is possible to add a NOT NULL constraint to an existing table by using the ALTER TABLE statement. In this case, the column_name must not contain any NULL value before applying the NOT NULL constraint.
Create the column, but allow NULL. Populate the column with the correct data from the foreign key table. Alter the column add not null. Add the foreign key constraint.
About parts of your questions (ofc after years):
1.If you mean that the default value would be something so smart, and you wouldn`t need to change it in future, then your wish is wrong. why?
for two reasons:
2.It can be handle with the code i`ll show you.
Therefore base on this explanations you could have a column with default value which exist in Fkeys, but it won`t be something you need, and you should update it in future base on your usage. and for this you need:
FIRST:
CREATE FUNCTION SelectMinForeignKey()
RETURNS INT
AS
BEGIN
DECLARE @FirstID INT
SELECT @FirstID = MIN(ID) from DetailTableExample01
RETURN @FirstID
END
SECOND:
ALTER TABLE example1 ADD NoNullableCol INT NOT NULL DEFAULT [dbo].SelectMinForeignKey()
ALTER TABLE example1
ADD NoNullableCol2 INT NOT NULL DEFAULT [dbo].SelectMinForeignKey() ,
FOREIGN KEY(NoNullableCol2) REFERENCES DetailTableExample01(id);
ALTER TABLE dbo.example1 ADD
NoNullableCol INT NOT NULL DEFAULT [dbo].SelectMinForeignKey(),
CONSTRAINT FK_example1_DetailTableExample01
FOREIGN KEY (NoNullableCol)
REFERENCES dbo.DetailTableExample01 (ID)
ON UPDATE CASCADE
ON DELETE CASCADE;
ALTER TABLE dbo.example1 ADD
NoNullableCol INT NOT NULL DEFAULT [dbo].SelectMinForeignKey()
GO
ALTER TABLE dbo.example1 ADD
CONSTRAINT FK_example1_DetailTableExample01
FOREIGN KEY (NoNullableCol)
REFERENCES dbo.DetailTableExample01 (ID)
ON UPDATE CASCADE
ON DELETE CASCADE
NOTE: As you know table and column names are sample.
THIRD:
CREATE FUNCTION SelectMinForeignKey()
RETURNS INT
AS
BEGIN
DECLARE @FirstID INT
SELECT @FirstID = MIN(ID) from DetailTableExample01
RETURN @FirstID
END
GO
ALTER TABLE dbo.example1 ADD
NoNullableCol INT NOT NULL DEFAULT [dbo].SelectMinForeignKey(),
CONSTRAINT FK_example1_DetailTableExample01
FOREIGN KEY (NoNullableCol)
REFERENCES dbo.DetailTableExample01 (ID)
ON UPDATE CASCADE
ON DELETE CASCADE;
AND It`s Done!
Hope it solve your problem
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With