1
Hello, I am creating a data structure in Sqlserver and I have the following situation:
- I have a list of contact types (person, public, commercial, etc...)
- This list can be changed by the user but, obligatorily, 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 that I can force SQL to accept only a record as 1 for the default column? The intention is that if it is passed in a INSERT
or UPDATE
standard as 1, one must force all elements to be 0 beyond the informed.
Note: I have science that I can do this via TRIGGER
, but I don’t see how the best possible way to do it... there’s a smarter way to solve the case?
What’s wrong with using the TRIGGER?
– João Martins
the problem is that in addition to not being directly attached to the table but to an external procedural structure, it makes it difficult to maintain the generation code of the tables...
– LeandroLuk