Jump to content
Larry Ullman's Book Forums

The "Key" In The Tables - Purpose And Formatting


Recommended Posts

On page 169, it lists the SQL for the various tables.

 

In the Carts table, for example, there is a primary key, but there is also a "Key" for product type and a "Key" for user id

 

Some of the code looks like this

 

PRIMARY KEY (`id`),
KEY `product_type` (`product_type`, `product_id`),
KEY `user_session_id`(`user_session_id`)

 

Questions:

1) what is the purpose of the KEY? Please explain by comparing to PRIMARY KEY and also UNIQUE KEY, which is found in other tables.

2) Why is the input formatted that way for KEY. i.e. it has a name in quotes `product_type` and then it has more data after in parentheses?

Link to comment
Share on other sites

KEY is jut a synonym for INDEX. So there are general indexes being created there. UNIQUE KEY is just a more specific type of index. PRIMARY KEY is jut a more specific type of UNIQUE index.

 

The syntax for creating any index in MySQL is:

INDEX_TYPE name (columns)

With the name being optional. The backticks are just the optimal way to reference column names and other database elements (to avoid potential conflicts with MySQL reserved words), although I don't always use them myself.

Link to comment
Share on other sites

 Share

×
×
  • Create New...