Primary key versus key

你说的曾经没有我的故事 提交于 2019-12-20 18:27:10

问题


When creating a mysql dump containing the structure of my database, one of the tables shows the following:

CREATE TABLE `completedTransactions` (
  `paymentId` int(10) unsigned NOT NULL,
  `timestamp` int(15) unsigned NOT NULL,
  `actionTaken` varchar(25) NOT NULL,
  `response` varchar(255) NOT NULL,
  `responseCode` int(5) NOT NULL,

  PRIMARY KEY  (`paymentId`,`timestamp`),
  KEY `paymentId` (`paymentId`),

The primary key is what I was expecting, but I'm unsure what the last line is about?

KEY `paymentId` (`paymentId`),

Is this related to an index?


回答1:


Yes, the KEY keyword is just an alias for the INDEX keyword.

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
  ...
  {INDEX|KEY} [index_name] [index_type] (index_col_name,...)
      [index_option] ...

Source: MySQL Documentation: CREATE TABLE




回答2:


KEY is not unique, PRIMARY KEY and UNIQUE KEY are uniques.




回答3:


Quoting the documentation of CREATE TABLE :

KEY is normally a synonym for INDEX.
The key attribute PRIMARY KEY can also be specified as just KEY when given in a column definition. This was implemented for compatibility with other database systems.



来源:https://stackoverflow.com/questions/2199285/primary-key-versus-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!