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.
possible duplicate of When to use a Unique constraints vs unique indices on Oracle?
– mgibsonbr
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
– Maniero
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.
– ceinmart
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
– Maniero
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
@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...
– ceinmart
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.
– ceinmart
To complement: How to Manipulate Duplicate Questions(en)
– Maniero
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.
– Maniero
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.
– Lucas Castro
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).
– ceinmart
@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.
– Diego C Nascimento
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...)
– ceinmart