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.