我想要保持数据库记录的不重复,现在有两种方案,一种是给这一个(或多个)字段添加唯一性约束,一种是程序先去查询数据库是否存在给个字段值的记录,再决定是否插入?请问这两种方式该如何抉择?有更好的方式么?
3 回答
噜噜哒
TA贡献1784条经验 获得超7个赞
选择唯一性约束比较好:
简化应用逻辑。
建立唯一性约束的时候,系统一般会自动创建一个索引,在确保唯一的同时增加查询性能。(PostgreSQL 文档)
Adding a unique constraint will automatically create a unique B-tree
index on the column or group of columns listed in the constraint.
慕少森
TA贡献2019条经验 获得超9个赞
添加回答
举报
0/150
提交
取消
