Super Foreign Key or Composite Key - sql

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`);

Related

Adding a foreign key constraint referencing to primary key columns

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

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 --

How to add foreign key to an existing column in SQL Server 2012

I am trying to add foreign key to my existing column using below query
ALTER TABLE Sub_Category_Master
ADD FOREIGN KEY (Category_ID) REFERENCES Category_Master(Category_ID)
but I'm getting an error
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK__Sub_Categ__Categ__5812160E". The conflict occurred in database "shaadikarbefikar_new", table "shaadikarbefikar.Category_Master", column 'Category_ID'.
Well, the error clearly tells you that Category_ID in your Sub_Category_Master table contains some values that are not present in Category_Master (column Category_ID). But that's exactly the point of having a foreign key constraint - making sure your child table (Sub_Category_Master) only uses defined values from its parent table.
Therefore, you must fix those "voodoo" values first, before you're able to establish this foreign key relationship. I would also strongly recommend to explicitly name that constraint yourself, to avoid those system-generated, but not really very useful constraint names like FK__Sub_Categ__Categ__5812160E:
ALTER TABLE Sub_Category_Master
ADD CONSTRAINT FK_SubCategoryMaster_CategoryMaster
FOREIGN KEY (Category_ID) REFERENCES Category_Master(Category_ID)
ALTER TABLE Sub_Category_Master
ADD CONSTRAINT FKSub_Category_Master_Category_ID FOREIGN KEY (Category_ID)
REFERENCES Category_Master(Category_ID);
CREATE TABLE Orders
(
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
CONSTRAINT FK_PersonOrder
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);

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.