Use of unique SQL server constraints

Source: Internet
Author: User

The table structure is as follows:
Id int 4
Entryid int 4
Blogid int 4
At the time of insertion, you are not allowed to insert records with the same entryid and blogid. That is, the two records in the table cannot have the same entryid and blogid. entryid and blogid constitute the unique identifier of the record.
In the past, I used the SELECT statement to check whether the same record exists before insertion.
Now I use the unique constraints of SQL Server, which is simple, convenient, and efficient. The implementation method is as follows:
1. In the database relationship diagram, right-click the table that contains the constraints and select the "attribute" command from the shortcut menu.
-Or-
Open the table designer for a table that contains constraints, right-click the table designer, and select the "attribute" command from the shortcut menu.
2. Select the index/key tab.
3. Select the "new" command. The name assigned by the system appears in the index Name box.
4. Expand the column list under "column name" and select the column to which the constraint will be appended (Here we select the column entryid and blogid ).
5. Select the "create unique" check box.
6. Select the constraint option.
When a table or graph is saved, the unique constraint is created in the database.

Now we can insert it again, and the error "violates the unique key constraint..." is displayed ...".
InProgramThe duplicate record is inserted.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.