Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are keys used for in MySQL? [duplicate]

Possible Duplicate:
mySQL's KEY keyword?

Like

PRIMARY KEY (ID),
KEY name (name),
KEY desc (desc),

etc.

what are they useful for?

like image 336
Elle Avatar asked Dec 17 '22 10:12

Elle


1 Answers

Keys are used to enforce referential integrity in your database.

A primary key is, as its name suggests, the primary identification of a given row in your table. That is, each row's primary key will uniquely identify that row.

A unique key is a key that enforces uniqueness on that set of columns. It is similar to a primary key in that it will also uniquely identify a row in a table. However, there is the added benefit of allowing NULL in some of those combinations. There can only be 1 primary key, but you can have many unique keys.

A foreign key is used to enforce a relationship between 2 tables (think parent/child table). That way, a child table can not have a value of X in its parent column unless X actually appears in the parent table. This prevents orphaned records from appearing.

like image 158
Derek Kromm Avatar answered Dec 25 '22 23:12

Derek Kromm