Insert Sql Server

Asked

Viewed 65 times

2

I have a multi-column table. The unique identifier is the ID, but my question is the following:

I have a column version, I want to insert only a value without repetition. How can I implement this?

  • And if you created a default value for the column, so at the time of insert pass as NULL, and would automatically be filled by default, what do you think?

1 answer

2


You should make your column have a constraint of the kind unique.

In an editor like the SQL Server Management Studio, you should address the table contents and create a single index for the column.

Already saw code, you do something like this:

CREATE UNIQUE INDEX NomeDoIndice ON dbo.[Tabela com várias colunas]([Versão]);

Note that the above code assumes that the call table actually Tabela com várias colunas and that the column name really is Versão. You can use the real names of your table and column, and the index name can be any arbitrary name.

If you create a single index, every time you try to insert a record into the table with a value in the version column that already exists, SQL will trigger an error. The error message will explain that the table does not allow repetition in this column.

  • It worked. Thank you very much :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.