问题
I just want to make sure that I understand PRIMARY and FOREIGN key relation before searching for some finished answers on the internet.
Let's say this:
We have table CITY(ID-PK,CODE,NAME) and we have table PERSON(ID-PK,NAME,LASTNAME,CITY_ID-FK)
I'm confused, if in this case user needs to enter a foreign key in the table person? If not how know which city needs to be applied to the user? If user needs to enter a foreign key why to have it then because in that way we are leaving a lot of space for manipulation from the user side (etc. wrong ID input...)
How to connect those two tables? What would be a recognizable reference to connect? Or in this example user needs to input a foreign key?
I'm using ORACLE database. I would appreciate any explanation and example.
回答1:
I'm not entirely sure what you mean by 'wrong ID input', but I'll assume you mean an ID that that isn't valid, rather than just a mistake (like saying someone is in a different city to where they really are).
The foreign key constraint means that the value they enter in the person table for city_id has to exist as a primary key in the city table. They cannot put any old value in as a city_id, only valid values. And the corresponding city row cannot then be deleted without either removing/changing the reference in the person table (e.g. updating to a different valid value), or - unlikely in this case - cascading the delete so that any person records for the city are deleted.
So lets say you create your tables as:
create table city (id number primary key, code varchar2(2), name varchar2(30));
create table person (id number, name varchar2(30), last_name varchar2(30),
city_id number not null references city(id));
You have three records in your city table:
insert into city (id, name) values (1, 'New York');
insert into city (id, name) values (2, 'London');
insert into city (id, name) values (3, 'Paris');
Then you can add a person who lives in New York by including the ID of that city:
insert into person (id, name, last_name, city_id)
values (1, 'Michael', 'Bloomberg', 1);
(SQL Fiddle)
You aren't denormalising the data in the matching city record, so if it New York decided to change its name back to New Amsterdam, say, that would be a single update to the city record and you wouldn't have to touch any person records for people in that city.
If you tried to delete the city record for New York, you'd get an error (ORA-02292) saying that a child record existed. You could update the person record to have a city_id of 2 or 3, and would then be able to delete New York. The idea is that you can't do this by accident and leave orphaned data behind - a person_id pointing to a city that no longer exists.
If you tried to create a person record with a city_id value that doesn't match a city.id value:
insert into person (id, name, last_name, city_id)
values (2, 'Elvis', 'Presley', 4);
... then you'd get an error (ORA-02291) that the parent key - that is, a matching id value in the city tables - doesn't exist.
You can read more about foreign keys in the database concepts guide.
回答2:
With whis code you could add a constraint on the Person table to have the foreign key of the City table
alter table PERSON
add constraint CONSTR_PERSON_CITY
foreign key (CITY_ID-FK)
references CITY (ID-PK);
回答3:
Keep in mind if Person table City_ID has a value/s that do not exist in City table, trying to create a constraint will give you an error. Took me a while to figure out why.
来源:https://stackoverflow.com/questions/17137949/how-to-insert-foreign-key-in-a-table