CREATE TABLE [dbo].[usr_users_branches_departments]
(
[usrbdep_user_code] [dbo].[ais_code_medium] NOT NULL,
[usrbdep_branch_id] [int] NOT NULL,
[usrbdep_department_code] [dbo].[ais_code_medium] NOT NULL
) ON [PRIMARY]
ALTER TABLE [dbo].[usr_users_branches_departments] ADD
CONSTRAINT [FK_usr_users_branches_departments_usr_users] FOREIGN KEY ([usrbdep_user_code]) REFERENCES [dbo].[usr_users] ([usr_user_code]) ON DELETE CASCADE ON UPDATE CASCADE

ALTER TABLE [dbo].[usr_users_branches_departments] ADD 
CONSTRAINT [PK_usr_users_branches_departments] PRIMARY KEY CLUSTERED  ([usrbdep_user_code], [usrbdep_branch_id], [usrbdep_department_code]) ON [PRIMARY]
ALTER TABLE [dbo].[usr_users_branches_departments] ADD
CONSTRAINT [FK_usr_users_branches_departments_act_branches_departments] FOREIGN KEY ([usrbdep_branch_id], [usrbdep_department_code]) REFERENCES [dbo].[act_branches_departments] ([brdep_branch_id], [brdep_department_code])

GO
