Friday, December 8, 2017

SQL SERVER - How create table with referential integrity (foreign key)

Field LOGS.USER_IDENT is linked to primary key of table USERS. So, in this field are enabled only values from USERS.IDENT. When is USERS.IDENT changed, cascade rule change its value in LOGS table too.
create table [logs]
( 
  id int not null primary key identity,
  [date] datetime default getdate(),
  ...
  [user_ident] [dbo].[USER]    
    foreign key references [users]( ident )     
    on update cascade
);
Table USERS:
create table [users]
( 
  ident [dbo].[USER] not null primary key,
  [name] [dbo].[USER_NAME],
  [active] bit not null default 1
...
);

No comments:

Post a Comment