Adding a foreign key constraint referencing to primary key columns - sql

I am applying a foreign key constraint to a table(datareal.official). Should I always refer to the same columns in the primary key of the other tabel (datareal.officialcarriage). Or could I theoretically also use difference in the reference table?
ALTER TABLE datareal.official
ADD CONSTRAINT FK_carriage FOREIGN KEY
(target,goal)
REFERENCES datareal.officialcarriage (target,goal)
ON DELETE CASCADE
ON UPDATE CASCADE

Related

Defining foreign key for a composite primary key in SQL Server

I have two tables: Ticket and TicketRelation. I am trying to relate 2 Tickets using the TicketRelation table.
I want to add a cascade constraint to both foreign keys.
I am using SQL Server Management Studio to add the constraints but I get this error:
Introducing FOREIGN KEY constraint 'FK_destiny_ticket' on table 'ticketRelation' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
I also tried to do this with a trigger, but I am not sure how to define it.
Here I provide the tables definitions:
This is the base entity TICKET
CREATE TABLE ticket
(
ticket_id BIGINT,
some_data VARCHAR (50),
CONSTRAINT PK_ticket PRIMARY KEY (ticket_id)
);
and the relational table TICKETRELATION
CREATE TABLE ticketRelation
(
origin_ticket BIGINT,
destiny_ticket BIGINT,
relation_data VARCHAR (50),
CONSTRAINT PK_ticket_relation PRIMARY KEY (origin_ticket, destiny_ticket),
CONSTRAINT FK_origin_ticket FOREIGN KEY (origin_ticket)
REFERENCES ticket(ticket_id),
CONSTRAINT FK_destiny_ticket FOREIGN KEY (destiny_ticket)
REFERENCES ticket(ticket_id),
)
I want to add an ON DELETE CASCADE constraint to both foreign keys:
CONSTRAINT FK_origin_ticket FOREIGN KEY (origin_ticket)
REFERENCES ticket(ticket_id)
ON DELETE CASCADE, -- I want to add this --
CONSTRAINT FK_destiny_ticket FOREIGN KEY (origin_ticket)
REFERENCES ticket(ticket_id)
ON DELETE CASCADE, -- and this --

Super Foreign Key or Composite Key

ALTER TABLE `SWIMMER`
ADD CONSTRAINT `fk5` FOREIGN KEY (`CoachID`, `Country`) REFERENCES `TEAMMEMBER` (`MemberID`, `Country`),
ADD CONSTRAINT `fk1` FOREIGN KEY (`MemberID`, `Country`) REFERENCES `TEAMMEMBER` (`MemberID`, `Country`);
I'm using this SQL query and unfortunately it runs but the designer mode in phpmyadmin isn't showing up the relation between the TeamMember table and the Swimmer table which is a supertype and subtype relation in this database. Please Let me know if you can do this kind of sql query or you need to make some other connection.
ALTER TABLE `SWIMMER`
ADD CONSTRAINT `fk5` FOREIGN KEY (`CoachID`, `Country`) REFERENCES `TEAMMEMBER` (`MemberID`, `Country`),
ADD CONSTRAINT `fk1` FOREIGN KEY (`MemberID`, `Country`) REFERENCES `TEAMMEMBER` (`MemberID`, `Country`);

Constraint primary key

In SQL developer, when PRIMARY KEY is added like this:
PRIMARY KEY("ID")
In CONSTRAINT is generated with a name like "SYS006321". I need to change this name.
I tried to ADD:
CONSTRAINT TABLE_NAME_PK PRIMARY ("ID")
but I can't because can be only one PRIMARY KEY. This PRIMARY KEY is used in FOREIGN KEYs in others tables. So if i want to drop this PRIMARY KEY and after that add CONSTRAINT. I have to drop with cascade, so i lose references in FOREIGN KEY. What should I do?
If this is Oracle you can rename a constraint like this:
ALTER TABLE yourTable RENAME CONSTRAINT SYS006321 TO yourNewName;
First drop the foreign keys that point to the Primary Key.
Then drop the Primary Key.
Then re-create the primary key with the name you want.
Then re-create the foreign keys.

Correct syntax for foreign key constraint for multiple foreign keys

As per this example what is the correct syntax for a foreign key constraint for multiple foreign keys that all reference the same primary key from the referenced table?
ALTER TABLE team
ADD CONSTRAINT fk_team_players
FOREIGN KEY (player_1, player_2, player_3, player_4, player_5, player_6, player_7, player_8)
REFERENCES player (player_id)
You have to do them separately:
ALTER TABLE team
ADD CONSTRAINT fk_team_players1
FOREIGN KEY (player_1)
REFERENCES player (player_id)
ALTER TABLE team
ADD CONSTRAINT fk_team_players2
FOREIGN KEY (player_2)
REFERENCES player (player_id)
...
ALTER TABLE team
ADD CONSTRAINT fk_team_players8
FOREIGN KEY (player_8)
REFERENCES player (player_id)
you can also do it in one instruction as below
ALTER TABLE team
ADD CONSTRAINT fk_team_players1 FOREIGN KEY (player_1) REFERENCES player (player_id),
ADD CONSTRAINT fk_team_players2 FOREIGN KEY (player_2) REFERENCES player (player_id),
...
ADD CONSTRAINT fk_team_players8 FOREIGN KEY (player_8) REFERENCES player (player_id),
I recommend you to change table structure to keep players not in columns but in other table in rows.

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.