我试图通过读取模式和SQL文件来提高对SQL语法的理解。以下代码段取自github上的非常大的原始文件。
我的问题:在CREATE TABLE块id
的唯一键和索引部分中,后跟一个下划线和一个数字。 下划线数字是什么意思?
我可以想到几种可能性,但是根据数据没有任何意义(特别是在引用UNIQUE KEY时)。
KEY `id_2` (`id`)
id_2
与其他语言中的id[1]
类似,以便该行读取
同样,与UNIQUE KEY `id_3` (`id`)
比较时,这些似乎都没有意义。
CREATE TABLE IF NOT EXISTS `t_asv` (
`id` int(8) unsigned zerofill NOT NULL,
`b` int(11) NOT NULL,
`c` int(11) NOT NULL,
`v` int(11) NOT NULL,
`t` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_3` (`id`),
KEY `id` (`id`),
KEY `id_2` (`id`),
KEY `id_4` (`id`),
KEY `id_5` (`id`),
KEY `id_6` (`id`),
KEY `id_7` (`id`),
KEY `id_8` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `t_asv`
--
INSERT INTO `t_asv` (`id`, `b`, `c`, `v`, `t`) VALUES
(01001001, 1, 1, 1, 'In the beginning God created the heavens and the earth.'),
(01001002, 1, 1, 2, 'And the earth was waste and void; and darkness was upon the face of the deep: and the Spirit of God moved upon the face of the waters.'),
(01001003, 1, 1, 3, 'And God said, Let there be light: and there was light.')
答案 0 :(得分:2)
表中的key
定义会创建索引。密钥后面的名称是索引的名称。所以:
key id_1 (id)
创建一个名为" id_1"的索引。关键。 " _1"什么都没有意义。它是索引名称的一部分。
这相当于:
create index id_1 on t_asv(id);
您可以多次执行此操作,如您的示例所示。据我所知,绝对没有理由让多个索引具有相同的键 - 除非你想在创建表时演示错误的事情。