在MySQL中,是否可以对一行有UNIQUE约束?
看来MySQL的UNIQUE约束是逐列的,我正在寻找一种方法来确保行在逐行的基础上是唯一的;猜测答案是通过连接每行的列来创建哈希,然后我希望是唯一的,但存储该行哈希的列上是唯一的。另外,除非我创建控件,否则行本身将始终是唯一的,因为行的 ID 是 SURROGATE_KEY;意味着它是一个整数,按最后一行整数的 ID +1 连续增长。
Appears that MySQL's UNIQUE constraint is on a columns by column basis, I'm looking for a way to make sure the row are UNIQUE on a row by row basis; guessing the answer is to create a hash from by concatenating the columns per row I want to be UNIQUE then but a UNIQUE on the column storing the hash for that row. Also, the rows themselves unless I create a control will always be UNIQUE, since the ID for the row is a SURROGATE_KEY; meaning it's an integer sequential growing by +1 of the ID of the last row's integer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建多列
UNIQUE
索引。You can create a multiple-column
UNIQUE
index.确保行完全唯一的主要方法是在每一列上使用
UNIQUE
约束,或在每一列上创建类似的UNIQUE
索引。如果您可以允许两列接受相同的输入,但您需要唯一区分它们,那么您没有理由需要除主键(基本上是 ID 列)以外的任何内容。您可以做的另一件事是切换到列存储数据库,例如 InfiniDB,它具有 MySQL 前端 - end 或 MonetDB,两者都是开源替代方案。The main ways to ensure a row is completely unique is to use the
UNIQUE
constraint on every column or create an analagousUNIQUE
index on every column. If you can allow two columns to accept the same input but you need to uniquely distinguish them, there's no reason why you would need anything more than a primary key (basically an ID column). The other thing you could do is switch to a column-store database such as InfiniDB, which has a MySQL front-end, or MonetDB, both of which are open source alternatives.