如何在MySQL中保持列的值唯一? [英] How do I to keep the values of a column unique in MySQL?

查看:148
本文介绍了如何在MySQL中保持列的值唯一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Perl中进行文件解析,然后插入到MySQL数据库的表中.例如,我有以下字段:

I do file parsing in Perl and insert into a table in a MySQL database. For example, I have the following fields:

S.No ,PCID,USERNAME, TIME   INFORMATION.
1     203  JANE      22:08  updation
2     203  JANE      22:09  deletion
3     203  JANE      22:10  insertion

在此表中,我希望PCID是唯一的,USERNAME是唯一的. S.No是唯一的,因为我将其设置为自动编号并且它是主键.现在我的问题是:

In this table I wanted to have the PCID to be unique, USERNAME to be unique. S.No is unique, since I have set it to autonumbering and it's the primary key. Now my question is:

如果我添加PCIDUSERNAME作为复合主键,我仍然在表中找到重复项.没有变化.相同的o/p.如何删除重复项?我应该在Perl中编写代码以在插入之前检查重复项吗?

If I add PCID and USERNAME as composite primary key, I still find duplicates in the table. There is no change. The same o/p. What should be done to remove the duplicate? Should I code in Perl to check for duplicates before insertion?

请指导并提供帮助.预先感谢.

Please guide and provide assistance. Thanks in advance.

推荐答案

您希望S.No保持主键,并且PCID + USERNAME保持唯一,因此与Hammerite所说的相近:

You want the S.No to remain the primary key and PCID + USERNAME to be unique, so close to what Hammerite said:

ALTER TABLE MyTable
    ADD PRIMARY KEY (`S.No`),
    ADD UNIQUE KEY `PCID_USER_uk` (`PCID`, `USERNAME`);

这篇关于如何在MySQL中保持列的值唯一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆