How would you add a foreign key constraint on the dept_no column in - ProProfs Discuss
Advertisement

How would you add a foreign key constraint on the dept_no column in the EMP table, referring to the ID column in the DEPT table?

Asked by Karthicksugadev, Last updated: Apr 08, 2024

+ Answer
Request
Question menu
Vote up Vote down

5 Answers

B. Denton

B. Denton

Who wants to travel all the world and capture all the moment in his camera.

B. Denton
B. Denton, Traveler, journalism, Greater Manchester

Answered Sep 30, 2020

In order to do this, you would need to use the ALTER TABLE command. Make sure that you will use this with the ADD clause that will come from the EMP table.

These are the steps that you have to take to do that:

* You need to alter table one.
* Add two_ID integer.
* The next thing that you have to do is to alter table one.
* Now you can add the foreign key.
* It can be (two_id) References two (id)

Remember that if you would need to identify the foreign key in a table, you just need to look for the table that comes with the primary key

upvote downvote
Reply 

C. Hughes

C. Hughes

Learning new things along with my music

C. Hughes
C. Hughes, Musician, MA, Bradford

Answered Sep 25, 2020

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.

upvote downvote
Reply 

E. Reyes

E. Reyes

Building buildings and building intelligence

E. Reyes
E. Reyes, Builder, graduate, Birmingham

Answered Sep 23, 2020

A foreign key is utilized to link two tables together. It is a field in one chart that refers to the PRIMARY KEY in an additional table.

• It is utilized to prevent actions that would obliterate links between tables.

• You may need to create foreign key constraints, but you may not be certain of how you will do that.

• The main table should have a primary key column with a distinctive constraint.

• Create a row that will be used to reference a non -existent primary key.

• Make table EMP (empno NUMBER 4), ename VARCHAR2 (35) deptno NUMBER (7,2). NOT NULL CONSTRAINT em_deptro_fk REFERENCES dept (deptno) FOREIGN KEY constraint on the DEPTNO column of the EMP table.

upvote downvote
Reply 

F. Manasseh

F. Manasseh

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
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.

upvote downvote
Reply 

karthicksugadev

karthicksugadev

karthicksugadev
Karthicksugadev

Answered May 02, 2018

Use the ALTER TABLE command with the ADD clause on the EMP table.
upvote downvote
Reply 

Advertisement
Advertisement
Search for Google images Google Image Icon
Select a recommended image
Upload from your computer Loader
Image Preview
Search for Google images Google Image Icon
Select a recommended image
Upload from your computer Loader
Image Preview
Search for Google images Google Image Icon
Select a recommended image
Upload from your computer Loader

Email Sent
We have sent an email to your address "" with instructions to reset your password.