sql server - Using Composite Key Or Unique Constraint to prevent row duplicate -
my table , data looks this: data being mine archive.

i using row_number() over(partition values) single row. however, add constraint table prevent duplicate inserts.
because data many many business rule, figure can either use composite key comprising userid , 4 additional columns.
i looking @ following 3 options:
- should use constraint comprising of columns below
- use composite key comprising of columns
- add unique column (clustered index) , use constraint on rest of columns.
however, using composite still able insert duplicate rows.
what better approach here?
tl;dr primary key unique constraint should way go.
you should add new column serve primary key. identity column fine. like:
alter table table_1 add id int identity; alter table table_1 add constraint pk_table_1 primary key(id); the reason makes easier trace row individual entity can more perform updates , deletes. plus, it's useful 3rd normal form.
the primary key not have clustered index, btw. although clustered index created automatically default primary key in sql server if there not clustered index on table.
once have primary key, can use unique constraint other columns. that's assuming ok default system error messages. if need error messages more customized needs want use instead of trigger.
i avoid clustered, unique index columns. cause lot of fragmentation , disk io when inserting new records. non-clustered unique index ok, you've got constraint @ point.
Comments
Post a Comment