mysql组合唯一键 [英] mysql combined unique keys

查看:502
本文介绍了mysql组合唯一键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MySQL中是否有办法拥有两个唯一键并以某种方式连接它们?

Is there way in MySQL to have two unique keys and connect them somehow?

例如,如果我有下表,并且"title"和"store"是唯一键

for example if i have the following table and 'title'and 'store' are a unique keys

id | category | title       | price | store
1  | outdoors | fishing rod | 59.99 | wal-mart
2  | auto     | Penzoil Oil | 9.99  | target

,我尝试插入以下记录.该新记录将被忽略,因为标题为"fishing rod"并且商店为"wal-mart",并且存在具有该标题和商店的现有记录

and i try to insert the following record. This new record would be ignored because the title is "fishing rod" AND the store is 'wal-mart' and there is an existing record with that title and store

   | outdoors | fishing rod | 30.99 | wal-mart

但是如果我尝试插入以下记录,则将被接受,因为不存在标题为"fishing rod"和"target"为存储的记录

but if i attempted to insert the following record it would be accepted because there isn't a record that exists with the title of "fishing rod" and store of "target"

   | outdoors | fishing rod | 30.99 | target

仅MySQL有可能吗?

is this possible with just MySQL?

推荐答案

您可以在多个列上定义索引,例如:

You can define an index on multiple columns, e.g.:

CREATE UNIQUE INDEX arbitrary_index_name ON table_name (title, store);

这篇关于mysql组合唯一键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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