简体   繁体   English

MySQL复合主键列顺序不敏感

[英]MySQL Compound Primary Key Column-Order-Insensitivity

For compound keys in MySQL, does the order of the columns matter for insuring the uniqueness of the rows? 对于MySQL中的复合键,列的顺序对于确保行的唯一性重要吗?

Eg 例如

CREATE TABLE test (
    A  INT NOT NULL,
    B  INT NOT NULL,
    PRIMARY KEY (A, B)
);

Now, assuming I already have a row that contains the values A = 1, B = 2, will MySQL refuse to insert a row with A = 2, B = 1? 现在,假设我已经有一行包含值A = 1,B = 2,MySQL会拒绝插入A = 2,B = 1的行吗?

I ask this because I need a solution that uses a compound key & ignores the order of the values. 我问这个问题是因为我需要一个使用复合键并忽略值顺序的解决方案。

不,唯一性无关紧要,仅对排序和访问时间很重要

The ordering in an index matters. 索引中的顺序很重要。 As a reminder for why the order matters, consider what happens if the types are incompatible -- say date and varchar(255) . 为了提醒您顺序为何重要,请考虑如果类型不兼容(例如datevarchar(255)会发生什么。 The values are not interchangeable. 这些值不可互换。

If you want uniqueness for both values, then you are going to need to add a trigger. 如果您希望两个值都唯一,那么您将需要添加一个触发器。 The trigger can probably implement a much simpler condition, which is the requirement that A be less than B . 触发器可以实现更简单的条件,这是A小于B的要求。 This, in combination with the primary key will guarantee uniques across the two values. 结合主键,可以保证两个值的唯一性。 You can express this constraint in MySQL: 您可以在MySQL中表达此约束:

CONSTRAINT CHECK (A < B)

But, alas, MySQL will parse the code, but not execute the check. 但是,可惜,MySQL将解析代码,但不执行检查。 Instead, you can add before update and before insert triggers to put the smallest value in A . 相反,您可以在更新之前和插入触发器之前添加,以将最小值放入A Here is an example: 这是一个例子:

CREATE TRIGGER table_beforeinsert BEFORE INSERT ON table
     FOR EACH ROW 
     BEGIN
         declare xx int;
         if (NEW.A > NEW.B) then
             set xx := NEW.A;
             set NEW.A = NEW.B;
             set NEW.B = xx;
         endif;
     END

Haha, no! 哈哈不! The key is compound of the attributes. 关键是属性的组合。
Excuse me for being amused, it's just that you know that the values of 不好意思,只是您知道...的价值
your attributes are different A : 1 != 2 and B: 2 != 1. 您的属性是不同的A:1!= 2和B:2!= 1。
Just imagine the rows in the table. 想象一下表中的行。
A|B A | B
1|2 1 | 2
2|1 2 | 1

Imagine it as eg a string concatation: "1:2" whilst "2:1". 想象一下,例如字符串并置:“ 1:2”而“ 2:1”。
Obviously these strings are not equal. 显然,这些字符串不相等。
Internally the representation of a compound primary key will be less trivial, of course, 在内部,复合主键的表示将变得不那么琐碎,当然,
propably based on a hash function, stored into a temporary memory region for look ups 适当地基于哈希函数,存储在临时存储区中以进行查找
which is divided into 分为
range based pages. 基于范围的页面。

So what is important is not the ordering of the attributes in the key but the values for 因此,重要的不是键中属性的顺序,而是
the corresponding columns. 相应的列。
Actually the process of determining which attributes can be used best for a compound 实际上,确定哪些属性最适合用于化合物的过程
primary key is a very interesting one. 主键是一个非常有趣的键。
So you should understand the process clearly otherwise you may wonder why you experience 因此,您应该清楚地了解该过程,否则您可能会奇怪为什么会遇到
data inconsistency. 数据不一致。
Take as an example a person's table: 以一个人的桌子为例:
Name, FirstName, Birthdate, Street, City, Country, State, zip 名字,名字,生日,街道,城市,国家,州,邮编
Actually none of the attributes satisfies the condition of uniquely identifying a person. 实际上,这些属性都不满足唯一标识一个人的条件。
Neither does Name and Firstname. 名称和名字都没有。
Well, so Name and Firstname and Birthdate as well as City is not too bad, but take a look 好吧,所以名字和名字,生日以及城市还不错,但是看看
at how many people named Peter Smith live in London (may it be located in GB or in 伦敦有多少名叫彼得·史密斯的人居住(可能位于GB或
Ontario, Kanada). 安大略省,金田市)。
The process of determinating the candidate key is in fact the the process of defining functional dependencies between attributes. 确定候选键的过程实际上就是定义属性之间的功能依赖性的过程。
Basically the idea is: 基本上,这个想法是:
If I know a Persons lastname, do I know his first name. 如果我知道一个人的姓氏,我是否知道他的名字。
If I know a Persons lastname and his first name, do I know in which city he lives? 如果我知道一个人的姓氏和他的名字,我是否知道他住在哪个城市?
and so on. 等等。
This function dependencies are written as: 该函数依赖关系写为:
City, Country, State --> zip // meaning : when I know a persons city, country and state I know his Zip. 城市,国家/地区-> zip //含义:当我认识一个人时,我知道他的邮编。
(and the other way round, of course) The attributes that don't appear on the right side of the dependency list, must be part of every candiate key. (当然,反之亦然)未出现在依赖项列表右侧的属性必须是每个候选键的一部分。
If every attribute else is determined by this attributes, you are done. 如果其他每个属性都由该属性确定,则说明您完成了。 You got your 你有你的
compound primary key. 复合主键。
If not: 如果不:
Then the process of finding the optimal candiate key is just the steps of using first 然后,找到最佳候选密钥的过程只是首先使用的步骤
a key made of all attributes in the table. 由表中所有属性组成的键。
Then remove the attributes that depend. 然后删除依赖的属性。 List all candiate keys and repeat. 列出所有候选密钥并重复。
Then see similarities (which candidate keys are contained in other candidate keys, this is called reduction). 然后查看相似性(哪些候选关键字包含在其他候选关键字中,这称为简化)。
So you will get to the optimal compound primary key for your table. 因此,您将获得表的最佳复合主键。
As an example for the first case: 作为第一种情况的示例:
Attributes 属性
A Name, B LastName, C City, D State, E zip (internationally) A名,B名,C市,D州,E zip(国际)
E --> C // international zips have a form of eg GB-W11 2BQ E-> C //国际邮政编码的格式例如为GB-W11 2BQ
E --> D E-> D
So the first candidate would be: 因此,第一个候选人是:
A, B, E (as they don't appear on the right side). A,B,E(因为它们没有出现在右侧)。
Is every attribute else depending? 其他属性是否都取决于? Yes, there are only C and D left.And they depend on E 是的,只剩下C和D,它们取决于E
You're done. 你完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM