I would like to create a foreign key but the Primary table has 2 fields as it Primary Key. Is there a way to create a Foreign Key that links only on one field of the primary key.
Ex: table 1: id int , language char(2), description varchar(100) PK = ID + language
table 2 : id int, idlanguage int PK = id FK (idLanguage refers to id from table 1)
This cause an error because the foreign key does not include all part of the primary key.
Rufen
If your table1.id is unique, then create primary key only on that column, if not, then you should add laguage in your table2 column because there will be no way to distinguish between languages that have the same id.|||
I know that there will be no way to distinguish all records that have the same id, but that is what I want. When I delete a record from Table 1, I want to delete all record from table 2 that have this id (foreign from table 1).
|||You can implement the foreign key logic using triggers.
For eg. For Delete
CREATE TRIGGER trg
ON table1
FOR DELETE
AS
BEGIN
DELETE FROM table2
WHERE idlanguage in (SELECT id FROM deleted)
END
You can have similar trigger for insert and update
No comments:
Post a Comment