如何防止重复记录插入到我的表中? [重复]

时间:2022-08-04 22:13:41

This question already has an answer here:

这个问题在这里已有答案:

+-----+--------------+--------+-----------+
| id  |     a_no     | status | req_date  |
+-----+--------------+--------+-----------+
|   1 | 6019-6039120 |    0   |2015-03-01 |  
|   2 | 6019-6039120 |    0   |2015-03-01 |
|   4 | 6019-6039120 |    0   |2015-03-02 |
|   5 | 6019-6039121 |    0   |2015-03-02 |
|   6 | 6019-6039134 |    0   |2015-03-02 |
|   7 | 6019-6039134 |    0   |2015-03-02 |
|   8 | 6019-6039120 |    0   |2015-03-03 |
|   9 | 6019-6039129 |    0   |2015-03-03 |
|  10 | 6019-6039145 |    0   |2015-03-04 |
|  11 | 6019-6039167 |    0   |2015-03-04 |
+-----+--------------+--------+-----------+

This is my table structure, as you can see id=1 and id=2 have a same data. How to prevent the same data to insert into my table?

这是我的表结构,因为您可以看到id = 1和id = 2具有相同的数据。如何防止相同的数据插入我的表?

1 个解决方案

#1


Add a unique constraint/index:

添加唯一约束/索引:

create unique index idx_table_3 on table(a_no, status, req_date);

This will prevent duplicates in the table. An attempt to insert duplicates will result in an error.

这样可以防止表中的重复。尝试插入重复项将导致错误。

#1


Add a unique constraint/index:

添加唯一约束/索引:

create unique index idx_table_3 on table(a_no, status, req_date);

This will prevent duplicates in the table. An attempt to insert duplicates will result in an error.

这样可以防止表中的重复。尝试插入重复项将导致错误。