了解MySQL CREATE TABLE语法

时间:2011-07-03 03:42:49

标签: mysql

我有以下Django模型:

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    full_name = models.CharField(max_length=100)
    network = models.ForeignKey(Network)
    positions = models.ManyToManyField(Position, through ='Timestamp', blank=True)
    featured_video = models.ForeignKey(VideoInfo, blank=True, null=True)

SHOW CREATE TABLE userprofile_userprofile,我得到以下MySQL输出:

| userprofile_userprofile | CREATE TABLE `userprofile_userprofile` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `full_name` varchar(100) NOT NULL,
  `network_id` int(11) NOT NULL,
  `featured_video_id` int(11) DEFAULT NULL,
  `bio` longtext NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_id` (`user_id`),
  KEY `userprofile_userprofile_fb2a452b` (`network_id`),
  KEY `userprofile_userprofile_58bbe944` (`featured_video_id`),
  CONSTRAINT `featured_video_id_refs_id_b7c2ab56` FOREIGN KEY (`featured_video_id`) REFERENCES `userprofile_videoinfo` (`id`),
  CONSTRAINT `network_id_refs_id_f5c27879` FOREIGN KEY (`network_id`) REFERENCES `network_network` (`id`),
  CONSTRAINT `user_id_refs_id_da7416c6` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=31034 DEFAULT CHARSET=latin1 |

最后七行在这做什么?他们有什么意义?

  • unique key
  • key
  • constraint
  • engine=InnoDB
  • auto-increment=31034

1 个答案:

答案 0 :(得分:2)

  • 键正在创建索引
  • 唯一键是创建一个索引,为所提到的列强制使用唯一值。
  • constraint ...外键正在创建外键引用,限制只插入已存在的值的值 引用的表。
  • AUTO_INCREMENT 设置起始auto_increment 列值。