且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使数据库插入ID,而不是创建重复的记录.

更新时间:2023-02-02 23:20:52

了解您的问题很困难.
好的,解决您的问题的方法是:
1.您需要放置必填字段验证器.
2.放入业务逻辑以检查重复记录
一个.您可以使用代码逻辑在插入
之前检查重复的记录 b.如果某些东西在字段级别是唯一的,则还要在数据库中放置一个UNIQUE键约束
3.进行以ProjectID而不是name为参数的SQL查询.
It was little difficult understanding what your problem was.
Okay so, resolution to your issue is:
1. You need to put required field validators.
2. Put business logic to check duplicate record
a. you can have a code logic to check duplicate records before inserting
b. if something is field level unique, also put a UNIQUE key constraint in your DB
3. Make your SQL query that takes ProjectID and not name as a parameter.


OP太长了!
您需要正确规范化数据库.
使用代理键(例如,您的自动递增ID字段)会导致您需要做额外的工作以确保唯一性(以及其他问题).参见:

http://en.wikipedia.org/wiki/Surrogate_key#Disadvantages_of_Surrogate_Keys [
The OP is too long!
You need to normalise you database properly.
Using Surrogate keys (such as your auto-incrementing ID Field) has the effect that you need to do extra work to ensure uniquness (amongst other problems). See:

http://en.wikipedia.org/wiki/Surrogate_key#Disadvantages_of_Surrogate_Keys[^]

If the project information relies on the ProjectName, use that as the primary key. If it relies upon the ProjectName and Reference, use these etc. If you apply this consistently across the tables, you''ll find your task easier. If you use surrogate keys, to do a good job you''ll end up re-implementing the referential integrity stuff (like Sandeep''s answer) SQL servers already do really well for free.


使用保存"按钮运行的代码是什么?您要运行插入查询而不是更新查询吗?
What code are you running with the Save button? Are you running an Insert query instead of an Update query?