CREATE TABLE中的KEY和PRIMARY KEY()

时间:2014-07-07 13:05:09

标签: mysql sql indexing primary-key create-table

试图了解钥匙的特殊性。文档说KEY = INDEX和PRIMARY KEY - 一种不同类型的密钥。还行吧。 但是有代码:

$db = mysqli_connect ('localhost', 'root',''); 
mysqli_query ($db, 'CREATE DATABASE IF NOT EXISTS publications;'); 
mysqli_query ($db, 'USE publications;'); 
// try to create a field with the usual non-unique index that is not a primary key: 
mysqli_query ($db, 'CREATE TABLE IF NOT EXISTS classics1 (author INT KEY)'); 
// output the indexes on the table: 
$res = mysqli_query ($db, 'SHOW INDEX IN classics1'); 

为什么作者字段包含主键?我没有在请求中写一些东西(明显创建主键),如下所示:

$res = mysqli_query ($db, 'CREATE TABLE IF NOT EXISTS classics1 (author INT PRIMARY KEY)'); 

顺便说一下,如果在查询中添加一行:

mysqli_query ($db, 'ALTER TABLE classics1 ADD KEY (author)'); 

...然后添加正常索引。为什么在CREATE TABLE()KEY中成为PRIMARY KEY?

0 个答案:

没有答案