You can refer to the ID column in the DEPT table to add a foreign key constraint on the dept_no column in the EMP table by using the command of an altar table with the added clause on the emp table. The reason for this being correct is that the alter command can be used to create a constraint for emp.
The constraint for the foreign key would be made or created for the emp table and not for the DEPT table. For the DEPT table to be created successfully, it needs to have a primary key, and the emp table also needs to have a foreign key to be created successfully.
You use the modifying clause for different purposes, and the creation of foreign key constraints would not be for the DEPT table. The alter table command's modified clause is used for other purposes apart from being used on the emp table. The task can be accomplished such that you add foreign constraints after table creations.
I love to code. I believe everything is programmed in a certain way to make it work. From human brains to every single command in the machines.
F. Manasseh, Software Developer, B.E (Bachelor of Engineering), Tallahassee, Florida
Answered Sep 18, 2020
You may need to create SQL foreign key constraints but you are not sure how you are going to do that. The parent table should have a primary key column with a unique constraint. The main purpose of this is to make sure that you will not create a row that will be used to reference a non−existent primary key.
You can create a foreign key when you make the create table statement. Take note that you will see the error that it results in by getting rid of the comments that are placed in the code.
There may be some differences depending on how you would want to include it. You can refer to SQLite reference if you want more information.