Difference in Index Unique and Unique Constraint usage in Mysql?

Asked

Viewed 17,226 times

20

I would like to know the behavior/differences between a Indice Unique and Unique Constraints to the Mysql ?

Many may think that it makes no difference in the database, but it does! And not only for the DBA/Administrator, but also for the developers because it can influence how a code is written.


As a reference of what may be so different and what I’m interested to know, below is a link of how it is in a bank Informix , but in the Mysql how it works?

Link, similar situation on other engine: Difference in Use Index Unique and Unique Constraint in Informix?

  • 1
  • We are discussing the validity of this type of question here: http://meta.answall.com/questions/289/o-que-fazer-com-questions-com-podem-ter-multiplas-answers

  • 2

    I do not consider this question duplicated because they are different objectives, where one explains punctually the use of the resource in a specific database and the other tends to compare the availability of these same resources between different databases , what will make all differs for any developer working with systems that support multi-database and Dbas that need to manage this whole variety.

  • 2

    Duplicate does not seem even, the question is whether it is broad or not. The staff is not understanding what the vote on the exact duplication

  • If you had specified a database engine that would be a question legitimate for our format; but as you leave it open for several different Engineering ends up being too broad. Regarding the marking of duplicate, I disagree, the question already asked answers only part of the current question.

  • @Talles, please explain to me, how to ask a specific behavior comparison question by putting only one database engine? If I question only one, I will have an incomplete answer just as I had an oracle answer that you say is duplicated. In the specific case of the other question/Oracle, as the answer has already been accepted, how do I get the information I need, which is not there in the answer? I can even ask for more information in the comment, but I have no faith that after accepting the question, someone else will add new information... I can even try, but...

  • Good, but before they close the question as duplicate... then I will edit it, make it specific as you want it to be... and I will create others for each engine.

  • 3

    To make it clear, the text in the comment above is to show that this question in no way is duplicated. Let’s reopen it.

  • Is there a problem to be solved? Or is the question just existential? I’d like to see people with problems solved, and help these people solve their problems.

  • I (author of the question) am waiting for a technical and objective answer to the question. That is the understanding of the operation of the question in Mysql. (but as Sopt is still closed, I particularly do not expect that all questions here have good and valid answers right away).

  • 1

    @ceinmart I particularly think that the citation of the other question polluted his, only the link would be enough although I still find it unnecessary, since his question is objective and valid. Probably the answer should contain the behavior about the different Engines storage.

  • Hmmm. OK @Diego, I agree... I will edit the question and leave only the link. (It’s just that when I wrote the first time it was all in one question...)

Show 8 more comments

3 answers

10

In Mysql is the same thing.

ALTER TABLE tabela ADD CONSTRAINT UNIQUE KEY is the same thing as CREATE UNIQUE INDEX

According to the documentation:

KEY and INDEX are synonymous.

KEY is normally a synonym for INDEX. The key attribute PRIMARY KEY can also be specified as just KEY when Given in a column Definition. This was implemented for Compatibility with other database systems.

The nomenclature for indexes is to use the same name as the first field, and if it already exists, add _2, _3 and so on.

In Mysql, the name of a PRIMARY KEY is PRIMARY. For other Indexes, if you do not assign a name, the index is Assigned the same name as the first Indexed column, with an optional suffix (_2, _3, ...) to make it Unique. You can see index Names for a table using SHOW INDEX FROM tbl_name.

To prove yourself, run the table structure dump, and you will see that both to index or Constraint, is the same thing.

But if you still want to get the final proof, compare the schema from the bank, and you’ll see there’s no difference.

6

KEY and INDEX are synonyms in Mysql.
They mean the same thing. In the database you would use indexes to improve data recovery speed.
An index is typically created in columns used in clauses JOIN, WHERE e ORDER BY.
Important : You may only have one primary key per table, but several unique restrictions.

There is a very important difference between a Unique Index (Mysql responding to a "uniqueness constraint") and a primary key in Mysql.
Take a look at this:
Create a table t with a indice unique in the columnas a,b (The combination of columns a,b should uniquely identify any tuple in the table, certain?)

CREATE TABLE t (
  a int,
  b int,
  c int,
  UNIQUE KEY a (a,b)
);

Now let’s enter data:

mysql> insert into t (a,b,c)values(1,2,3);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t (c)values(1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t (a,c)values(1,1);
Query OK, 1 row affected (0.01 sec)

mysql> insert into t (b,c)values(1,1);
Query OK, 1 row affected (0.01 sec)

mysql> insert into t (b,c)values(1,1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t (a,b,c)values(1,2,3);
ERROR 1062 (23000): Duplicate entry '1-2' for key 'a'
mysql> select * from t;
+------+------+------+
| a    | b    | c    |
+------+------+------+
|    1 |    2 |    3 |
| NULL | NULL |    1 |
|    1 | NULL |    1 |
| NULL |    1 |    1 |
| NULL |    1 |    1 |
+------+------+------+
5 rows in set (0.00 sec)

mysql>

A unique index creates a constraint in such a way that all values in the index must be distinct. An error occurs if you try to add a new line with a key value that matches an existing line. This restriction does not apply to NULL values, except for the storage engine BDB. For other engines, an index UNIQUE allows multiple NULL values for columns that can contain NULL.

Performance Indice Unique vs Unique Constraints: With MyISAM as the engine, there should not be a performance difference between the Indice Unique vs Unique Constraints.
The MyISAM does not treat them differently.
If you were using the engine InnoDB, however, there would be a difference, InnoDB stores data in primary key order.

0

The key: ensures the uniqueness of information in your table. (Generally unique keys may also have null records, so they may not be primary key.)

A Primary key: Can be used for Foreign key relationships with other tables. (Cannot contain null records but can be used autoincrement).

  • But that’s not what he asked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.