Primary Key and Foreign keyto 2 differnt table

i want to add a new table :

CREATE TABLE [dbo].[BlockWordsExcluders](
[BlockedWordID] [int] NOT NULL,
[SubAccountId] [int] NOT NULL,
[CreateDate] [datetime] NOT NULL,
CONSTRAINT [PK_BlockWordsExcluders] PRIMARY KEY CLUSTERED
(
[BlockedWordID] ASC,
[SubAccountId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[BlockWordsExcluders] ADD CONSTRAINT [DF_BlockWordsExcluders_CreateDate] DEFAULT (getdate()) FOR [CreateDate]
GO

[BlockedWordID] and [SubAccountId] are together becuase they shoul be entered only once.
i want to add Foreign key to the ciolumns
[BlockedWordID] and [SubAccountId]
so when a new row is added
this values must exist in other tables (where there each one of them exist)
what is the correct way to do it?

Something like:

ALTER TABLE dbo.BlockWordsExcluders
ADD CONSTRAINT FK_BlockWordsExcluders_BlockedWordID
FOREIGN KEY (BlockedWordID)
REFERENCES dbo.BlockedWord(BlockedWordID);
--ON DELETE CASCADE
--ON UPDATE CASCADE
GO
ALTER TABLE dbo.BlockWordsExcluders
ADD CONSTRAINT FK_BlockWordsExcluders_SubAccountId
FOREIGN KEY (SubAccountId)
REFERENCES dbo.SubAccount(SubAccountId);
--ON DELETE CASCADE
--ON UPDATE CASCADE
GO
1 Like