The difference between a key column and a non-key

Sorry for the newbie question. I defined the table as follows.

CREATE TABLE `class1` (
`name` varchar(20) NOT NULL,
`familyname` varchar(20) NOT NULL,
`id` int(11) DEFAULT NULL,
KEY `class1` (`name`,`familyname`));

Can you explain to me what the difference is here, I have a first and last name as a key.

I need to enter values ​​in a table and run queries, but I can not understand what this gives me? If I do not define the first and last name as keys, I get the same results.

+3
source share
3 answers

A key, otherwise known as an index, will not change the query results, but sometimes it can make the query faster.

If you are interested, you can read about the different types of keys here .

+1
source

KEY create table mysql INDEX "name", "familyname".

- , "name" "familyname" where select.

, :

CREATE TABLE `class1` (
`name` varchar(20) NOT NULL,
`familyname` varchar(20) NOT NULL,
`id` int(11) DEFAULT NULL,
PRIMARY KEY (`name`,`familyname`));

"" "".

+1

( ), , .

But there is a drawback to this. Adding unnecessary indexes will negatively impact your performance. So to summarize all this: indexes can speed up your database, but always check to see if you really need them before adding them.

0
source

Source: https://habr.com/ru/post/1777408/


All Articles