I'm a bit confused about this. Must a foreign key always reference a primaryy key? What if there's two foreign key on the same table that refer to the same primary key?
Thanks
"What if there's two foreign key on the same table that refer to the same primary key?
"
Any number of child tables can reference a parent table. There are certain situations in which it is possible for a child table to have more than one foreign key on the same parent.
For instance, any form of sporting contest has opponents of the same type - player, team, etc. So a match will have two instances of that entity, hance the child table will have two columns with foreign keys referencing the same primary key.
create table player (
player_id number not null primary key
, name varchar2(30) not null unique
);
create table match (
match_id number not null primary key
, player_1 number not null
, player_2 number not null
, match_played date not null
, result varchar2(10)
, constraint match_player1_fk foreign key (player_1) references player
, constraint match_player2_fk foreign key (player_2) references player
);
A foreign key can reference a unique constraint rather than a primary key. However this is not standard practice. It is the convention to use unique keys to enforce candidate keys - business keys - and these are not always suitable for use as foreign keys.
For instance in my example, PLAYER.NAME is a unique key: every player must have a distinct name. However, it would not be appropriate to use NAME as the foreign key on MATCH because people can change their name. It is more convenient to use the synthetic primary key, PLAYER_ID because that will not change over the lifetime of the PLAYER record.
A set of columns comprising a foreign key in one table must refer to an equivalent set of columns in a table with either a Primary Key or Unique Key constraint.
You certainly can have 2 or more FKs in the same table that refer to the same PK or UK. This models a relationship where a child record is related to more than one parent record - e.g. a record representing a biological child might have a FK to the record for their father as well as for their mother.
Note that a unique index is not sufficient for this purpose; a unique constraint is required, otherwise you will get "ORA-02270: no matching unique or primary key for this column-list".
Related
I get the following error when I try to add a foreign key to my purchaseorderheader table :
ORA-02270: "no matching unique or primary key for this column-list"
I made sure the table and column names are the same but I still get the error, any ideas?
You have a composite primary key that is composed of two columns but, in your foreign key, you are only referencing one of the columns of the composite primary key and not the entire primary key.
You either need to:
change the primary key to be only on the single column purchaseorderid;
keep the existing primary key and additionally create a UNIQUE key on only the purchaseorderid column of the first table; or
include both columns of the primary key in the foreign key.
Whichever option you choose, you should make sure that it implements the business logic that you are trying to capture in the table; if it does not then you should revisit the database-design and find a design that does capture your business logic.
I'm using pl sql developer 12.what i want is to add the make the primary key sequential using oracle 12' windows and forms , not by scripting. I can't find out how?.
aslo, how can i make one to one relation between 2 tables (user,role), in user table i added role_id as foreign key; but the relation seem one to many!!!
In Oracle 12c and above, you define that column as identity columns:
CREATE TABLE mytable (
id NUMBER GENERATED ALWAYS AS IDENTITY,
-- other columns ...
);
A #Mureinik already said, in Oracle 12 and higher versions you can define your primary key column as NUMBER GENERATED ALWAYS AS IDENTITY to get a unique sequential value.
To enforce a one-to-one relationship in the database you'd want to have your ROLE_ID column defined as a primary key on one table, and as either a UNIQUE or PRIMARY key on the second table, with a foreign key relationship between the tables. In other words:
CREATE TABLE T1 (ROLE_ID NUMBER
CONSTRAINT PK_T1
PRIMARY KEY
...);
CREATE TABLE T2 (ID_T2 NUMBER
PRIMARY KEY,
ROLE_ID NUMBER
CONSTRAINT T2_U1
UNIQUE
CONSTRAINT T2_FK1
REFERENCES T1(ROLE_ID)
ON DELETE CASCADE,
...);
After the above ROLE_ID will be the primary key on T1, a unique key on T2, and T2.ROLE_ID will be a foreign key to T1.
Best of luck.
what I have done lastly is that i downloaded sql developer 64 w, and from it; I connected with the database then I made the column sequential
table1:
tid(primary key) // no foreign keys here
table2:
sid(primary key) // no foreign keys here too
table3:
Tid
Sid
iid(primary key)
foreign key(Tid,Sid) references table1(tid).table2(sid)
In table3 i want to make a composite foreign key or composite foreign key constraint but failed . there are many questions related to this .But none of them seems helpful to me . How can i do that ? Is it valid ? Then what is the syntax of making composite foreign key from two different tables primary key
It's not possible to have a single foreign key referencing fields on different tables, and it makes no sense at all. A foreign key of two or more fields implies that the combination of values of the fields must be match on a single record of the referenced table, and this can't be done if the referenced fields are on different tables.
What you can do is to create two distinct foreing keys to the two tables, as following:
CREATE TABLE table3(
iid NUMBER,
Tid NUMBER,
Sid NUMBER,
CONSTRAINT pk PRIMARY KEY (iid) USING INDEX TABLESPACE idx,
CONSTRAINT fk001 FOREIGN KEY (tid) REFERENCES table1(tid),
CONSTRAINT fk002 FOREIGN KEY (sid) REFERENCES table2(sid)
);
Not able to create /find the logic to apply FK on a column in child table referencing a column from composite PK of parent table.
create table product(prod_id number,
prod_name varchar2(20),
price number,
constraint PK12 primary key(prod_id,prod_name));
Table created.
create table purchase(prod_id number,
purchase_price number,
constraint FK12 foreign key(prod_id) references product(prod_id));
create table purchase(prod_id number,
purchase_price number,
constraint FK12 foreign key(prod_id) references product(prod_id))
ERROR at line 1:
ORA-02270: no matching unique or primary key for this column-list
Kinldy suggest how i can incorporate this logic.
Thanks.
You can't.
As the error says there's no matching primary key for that column list; you must have one. You have three options:
Remove PROD_NAME from the primary key of PRODUCT. On the face of it this seems like the logical solution, if this is not required in order to make the primary key unique.
Add PROD_NAME to the PURCHASE table.
Create a unique index on PURCHASE.PROD_ID. This seems excessive if it would be a primary key candidate anyway.
I suspect that this is not unique to Oracle. Considering you have a composite primary key in the referenced table, that implies that only one of the columns comprising the composite key is not enough to uniquely identify the record in that table. Therefore, it's impossible to reference only a single column of the primary key in a foreign key relationship that's one-to-many (e.g. one record in the referenced table can have many records in the referencing table--the one with the FK). However, if the relationship to be established is many-to-many, this may be possible.
HTH.
I'm a bit confused over when I should use a primary or foreign key. I have two tables, and in both of them, some of the columns reference columns that are primary keys in other tables.
Here they are:
CREATE TABLE roles (
movie_id NUMBER(10,0) NOT NULL REFERENCES movies(movie_id),
actor_id NUMBER(10,0) NOT NULL REFERENCES actors(actor_id),
movie_description VARCHAR2(50),
salary NUMBER(10),
CONSTRAINT pk_roles PRIMARY KEY (movie_id, actor_id)
);
CREATE TABLE profits (
movie_id NUMBER(10,0) NOT NULL,
gross_profit NUMBER(9) NOT NULL,
net_profit NUMBER(9) NOT NULL,
CONSTRAINT fk_profits FOREIGN KEY (movie_id) REFERENCES movies(movie_id) ON DELETE CASCADE
);
In the first table I have made a composite primary key from teh two columns that reference columns in other tables. Those columns happen to be primary keys in their respective tables.
In the second table, I've made a foreign key again referencing a primary key in anther table. But what is best practice? Should the key in the first table also be a foreign key since it references primary keys in other tables?
Primary key constraints and unique constraints prevent duplicate rows. Duplicate rows not only waste space, they make it harder to get meaningful answers from your database.
Foreign key constraints restrict values to those that exist in another table. The target of a foreign key constraint is commonly a primary key, but it could be any column(s) that have a unique constraint.
Every table should have a primary key constraint. If the column(s) that make up the primary key also require a foreign key constraint, add the foreign key constraint as well.
Your table "roles" is fine, as far as implementing primary key constraints and foreign key constraints. But "profits" needs a primary key.
To your Question "Should the key in the first table also be a foreign key since it references primary keys in other tables?"
There is no simple answer, as it heavily depends on type of planned usage of data and database being used. If you need a simple answer, yes it is a good idea. Below is the longer version.
Pros :
It helps keep your data clean.
Based on how database is planned to be used and which database you are using, some databases, tend to optimize joins better, if Foreign keys are defined for joins upfront.
Cons :
If you plan to bulk load into your tables frequently, then FK constraints tend to slow down your loads, if that is the case, some databases allow you to define soft constraints, which are only used for query optimization purposes, but are not verified during loads.