Question

I have a schema on my db where there are some tables. I have to create a table into this schema and i have to connect it with the others already present on the schema.I make an example:

Tables already present:

SCHOOL(IdSchool,NumStud,IdCountry);
SHOP(IdShop,IdCountry);

New table:

Country(IdCountry,....);

I want to know if there is an automatic mode to connect them (it means not to set the foreign key manually).

Was it helpful?

Solution

I want to know if there is an automatic mode to connect them (it means not to set the foreign key manually).

No.

How is the DBMS to know that Country.IdCountry and SCHOOL.IdCountry are given the same name with the intention to be connected, instead of just accidentally?

You'll have to use ALTER TABLE ... ADD FOREIGN KEY (...) REFERENCES ...1 to explicitly create the foreign key in the existing table.


1 Or ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY (...) REFERENCES ....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top