foreign key doubt in MYSQL - sql

can we define a primary key in a table as a foreign key in that table . I mean,
PRIMARY KEY(ssn),
FOREIGN KEY (ssn) REFERENCES Cust(cust_ssn)
And If we have a table that has some parameters that refers to some other table parameters and to some other 3rd table too. Then Do we need to define those parameters as foreign key referencing to both the tables or Just only one.

Yes. Any field or combination of fields can be a foreign key.

Related

how to create a table with two primary keys where one is foreign key in one table and the other on another table?

I have a table with two primary keys. municipality tables has two entities: municipality_code and municipality_names both primary keys. there is one table postcode which has municipality_code as a foreign key. on the other hand there is another table which has municipality_name as a foreign key. How can I connect them?
You can only have one primary key. But a UNIQUE NOT NULL constraint is just as good as a primary key and can serve as target of a foreign key constraint.
While that solves your immediare problem, I'd suggest that you change the data model so that you always use the same column as target of foreign keys. It makes your data model more understandable.

How to have more than one foreign key?

I'm having trouble adding more than 1 foreign key to my tables, I am getting the error:
"Error report -
ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view"
I tried making the table without the third foreign key and it worked so I think its something to do with the last key but not really sure. (I put spaces in between the SQL to make it easier to read.)
CREATE TABLE ORDER(
ORDERID CHAR(4) NOT NULL,
HOUSETYPE CHAR(2),
CHAIR CHAR(2),
PERSON CHAR(2),
PAYDAY DATE,
MENUPRICE NUMBER(4,2),
CONSTRAINT CONFERENCESESSION_PK PRIMARY KEY(ORDERID),
CONSTRAINT CONFERENCESESSION_FK1 FOREIGN KEY(HOUSETYPE) REFERENCES
HOUSE(HOUSETYPE),
CONSTRAINT CONFERENCESESSION_FK2 FOREIGN KEY(PAYDAY) REFERENCES PERSON(PAYDAY),
CONSTRAINT CONFERENCESESSION_FK3 FOREIGN KEY(MENUPRICE) REFERENCES
MENU(MENUPRICE)
);
A foreign key must reference another table primary key, my guess is that some of you foreign keys don't reference the primary key of the other table.
A FOREIGN KEY is a key used to link two tables together.
A FOREIGN KEY is a field (or collection of fields) in one table that
refers to the PRIMARY KEY in another table.
The table containing the foreign key is called the child table, and
the table containing the candidate key is called the referenced or
parent table.
https://www.w3schools.com/sql/sql_foreignkey.asp
A foreign key must reference another table primary key so make sure you are doing that with the right name of the primary key. For example:
CONSTRAINT CONFERENCESESSION_FK2 FOREIGN KEY(PAYDAY) REFERENCES PERSON(PAYDAY)
Make sure that in table PERSON you have PRIMARY KEY with name PAYDAY.

Can a foreign key reference another foreign key

Is it possible to have a foreign key that references another foreign key in a different table, or can it only reference primary and unique keys?
A foreign key can reference any field defined as unique. If that unique field is itself defined as a foreign key, it makes no difference. A foreign key is just to enforce referential integrity. Making a field a foreign key doesn't change the field itself in any way. If it is a unique field, it can also be the target of another FK.
For example:
create table Table1(
PK int identity primary key,
...
);
create table Table2( -- 1-1 relationship with Table1
PKFK int primary key,
...,
constraint FK_Table2_1 foreign key( PKFK ) references Table1( PK )
);
create table Table3( -- relates to Table2
PKFKFK int primary key,
...,
constraint FK_Table3_2 foreign key( PKFKFK ) references Table2( PKFK )
);
I know of no DBMS where this is not the case. And I agree with Horse, there is nothing wrong with the practice.
Is it possible to have a foreign key that references another foreign key in a different table
Yes. In fact contrary to accepted answer, the referenced FK column doesn't even have to be unique! - at least in MySQL. see https://www.db-fiddle.com/f/6RUEP43vYVkyK2sxQQpBfj/0 for a demo of the same.
which brings up the question that if the FK is not unique in the parent table, then who is the parent row? The purpose of FKs is to establish parent-child relationship.

I need to set as foreign key which refers to one of the composite primary key in other table

I have 3 tables with composite primary keys, I need to set as foreign key which refers to one of the primary key in other table.
ie,
table 1: merchant(mId,mName,addid,..) pk(mId ,addId) .
table 2: address(addId,name..) pk(addId)
table 3: store(storeId,addId,mId,storename,..) pk(storeId,addId,mId)
mId in table store is a foreign key to primary key mId of merchants table,
addId in tbl store is foreign key to primary key in table Address.
How is this relationship set in sql server management studio express using gui?
Your constraints could work like this, i.e. in a cascading style:
alter table store add constraint FK_store_merchant foreign key (mId, addId) references merchant (mId,addId)
alter table merchant add constraint FK_merchant_address foreign key (addId) references address (addId)
Not sure how you do this in the GUI, but just run this from a query window.

can we have a foreign key which is not a primary key in any other table?

it is written in every book that foreign keys are actually primary key in some other table but can we have a foreign key which is not primary key in any other table
Yes - you can have a foreign key that references a unique index in another table.
CREATE UNIQUE INDEX UX01_YourTable ON dbo.YourTable(SomeUniqueColumn)
ALTER TABLE dbo.YourChildTable
ADD CONSTRAINT FK_ChildTable_Table
FOREIGN KEY(YourFKColumn) REFERENCES dbo.YourTable(SomeUniqueColumn)
By definition a foreign key must reference a candidate key of some table. It doesn't necessarily have to be the primary key.
As a matter of detail the constraint called a FOREIGN KEY in SQL isn't exactly equivalent to the textbook definition of a foreign key in the relational model. SQL's FOREIGN KEY constraint differs because:
it can reference any set of columns subject to a uniqueness constraint even if they are not candidate keys (superkeys or nullable columns for example).
it may include nulls, in which case the constraint is not enforced
its syntax depends on column order, so a fk constraint on (A,B) referencing (A,B) is different to a constraint on (B,A) referencing (A,B).
Yes , There can be a foreign key which is unique key in other table as Unique key is subset of primary key but not the exact primary key.
So that's possible that foreign key is unique key in aother table.
General standard answer is no. It is only possible if foreign key refers any column uniquely in other table. That means foreign key must be the candidate key in other table and primary key is also a candidate key the table.