开发者

Understanding MySQL CREATE TABLE syntax [closed]

开发者 https://www.devze.com 2023-03-16 19:13 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that开发者_开发知识库 it can be reopened, visit the help center. Closed 11 years ago.

I have the the following Django model:

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)

From SHOW CREATE TABLE userprofile_userprofile, I get the following MySQL output:

| 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 |

What do the last seven lines doing here? What significance do they have?

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


  • key is creating an index
  • unique key is creating an index that enforces a unique value for the column mentioned.
  • constraint ... Foreign Key is creating a foreign key reference, limiting values inserted to only those that already exist in the referenced table.
  • auto_increment set the starting auto_increment column value.
0

精彩评论

暂无评论...
验证码 换一张
取 消