user3195396
user3195396

Reputation: 254

How do you prevent and able to give error message if a data has already existed in SQL server?

I have a table called Event and has 2 columns - eventID (Primary key, auto incremental, used for relationships, int data type) and an eventName(nvarchar(50) data type). How do I prevent duplication of data/value in my eventName column? I would like to return a message to the user that an event has already occurred through my asp.net page

Upvotes: 0

Views: 33

Answers (1)

Adriaan Stander
Adriaan Stander

Reputation: 166396

You can use a UNIQUE constraint on eventName

UNIQUE Constraints

You can use UNIQUE constraints to make sure that no duplicate values are entered in specific columns that do not participate in a primary key. Although both a UNIQUE constraint and a PRIMARY KEY constraint enforce uniqueness, use a UNIQUE constraint instead of a PRIMARY KEY constraint when you want to enforce the uniqueness of a column, or combination of columns, that is not the primary key.

Multiple UNIQUE constraints can be defined on a table, whereas only one PRIMARY KEY constraint can be defined on a table.

Also have a look at Creating and Modifying UNIQUE Constraints and SQL UNIQUE Constraint

Upvotes: 2

Related Questions