0

I'm learning MySQL. I came across some multiple-choice questions. If I doubted what the answer was or was not convinced it's correct I started searching google, stackoverflow and the mysql site. However for some I still couldn't confirm what the correct answer was.

Question: all of the following statements are true about a unique index except which one?
A. it must be unique for all columns of a table
B. it should not be explicitly defined for tables
C. It must be created only for columns having PRIMARY KEY or UNIQUE constraints.
D. It cannot be created by itself
E. It cannot be ensured that the combination of all values contained in the index are unique

Proposed answer: E.

My thoughts:

A. I really have no clue what they mean by it. But in this question it should be a correct statement. Can someone make this clear?
B. defined for tables? a unique index is on a table column. And by default non unique values are possible so it should be explicitly defined
C. Looks correct. Primary key is unique as well so therefor an index should be built by MySQL (however not explicitly)
D. wut? another answer that doesn't make any sense to me (am I stupid? :S)
E. Combination of values? No then it can't be ensured that these combinations of the values of a column are unique. If it is combinations of the different columns that might be combined in a primary key: then it can be ensured otherwise there is no use for a UNIQUE index.

Mostly I'm able to have a preferred answer when in doubt, however here I have no clue:s

This one is really driving me nuts:s Is this just a language barrier or ...

If you have any remark about this question/answer or for any of my remarks I'm happy to hear it!

4

1 回答 1

0

这些答案没有意义:

A. 它必须对表的所有列都是唯一的

唯一索引通常定义在表的列子集上。如果以这种方式定义给定索引,则它仅适用于所有列。因此,这是错误的。

B. 不应为表显式定义

如果不应该使用该构造,为什么还要存在?唯一索引非常有用。有人可能会争辩说,它不是在“表格”上定义的,而是在“表格中的列”上定义的,但这对于这个问题来说似乎太微妙了。

C. 它必须只为具有 PRIMARY KEY 或 UNIQUE 约束的列创建。

这几乎是真的。您可能会争辩说,创建唯一索引等同于创建唯一约束。但是,它们是用于执行相同操作的不同句法结构。此外,不会为具有唯一约束的列创建唯一索引,因为数据库会为您执行此操作。

D. 它不能自己创造

我不知道这应该是什么意思。从某种意义上说,它不能“自行”创建,因为它需要在其上定义一个表。

E. 不能保证索引中包含的所有值的组合都是唯一的

这是真的,因为 NULL 值允许重复:

A UNIQUE index creates a constraint such that all values in the index
must be distinct. An error occurs if you try to add a new row with a
key value that matches an existing row. For all engines, a UNIQUE
index permits multiple NULL values for columns that can contain NULL.][1]

我会读到只有 (e) 是明确正确的。选项 (c) 和 (d) 接近,具体取决于解释。选项 (a) 和 (b) 在我看来似乎是错误的。

您可能想在其他地方寻找此类问题。

于 2013-08-18T16:19:28.537 回答