Hello, I am creating a data structure in SQLServer and I have the following situation:
- I have a list of contact types (people, public, commercial, etc ...)
- This list can be changed by the user, but it can only have 1 element as default.
The table structure looks like this:
create table TipoContato (
id int not null identity(1,1),
descricao nvarchar(100) not null,
padrao bit not null constraint [DF_TipoContato.padrao] default 0
);
create unique index [UX_TipoContato.descricao]
on TipoContato(descricao);
Is there any way I can force SQL to only accept one record as 1 for the default column? The intent is that, if passed in a INSERT
or UPDATE
the default as 1, one must force all elements to be 0 beyond that reported.
Note: I am aware that I can do this via TRIGGER
, but I do not see it as the best way to do it ... Is there a smarter way to solve the case?