The image below is of a database I am learning to make from a tutorial. The author of the tutorial, as you can see from the arrow connecting the two tables, is trying to explain the relational nature of them. He added the subject_id
to the pages table so that, whenever we're talking about a page, we know which subject it is a part of. Subject is the parent, page is the child.
My question is, is there something special about this syntax subject_id
that allows that relationship to be made. For example, the name of the subjects table is plural subjects
, so I don't see how subject_id makes a connection from pages
to subjects
. Could subject_id have been开发者_开发知识库 named anything, and, if so, then how does the database know the relationship has been made?
The names are totally irrelevant to MySQL; they are chosen to help humans understand what's going on. The way you tell MySQL about the relationship is to specify a foreign key constraint as part of a CREATE TABLE or ALTER TABLE statement.
It'a made by adding a foreign key to your pages table that refers to your subject table. Basically a foreign key tells the table that subject_id
is actually a record in subjects
and they are linked
Have a look at these links
http://en.wikipedia.org/wiki/Foreign_key
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
精彩评论