How is the references keyword used when creating a table?
Let\'s say I want to create two tables person and hobby and I want t
Reference keyword is used actually to know where the foreign key has come. That means which is the table name and what is the name of this in that table.
I say this is correct :
CREATE TABLE person (person_id INT NOT NULL,
PRIMARY KEY (person_id));
CREATE TABLE hobby (hobby_id INT NOT NULL, person_id INT NOT NULL,
PRIMARY KEY(hobby_id),
FOREIGN KEY(person_id) REFERENCES person(person_id));
Then, look at this line :
FOREIGN KEY(person_id) REFERENCES person(person_id));
Here person_id is the foreign key and it has come from person table and in that table it's name is person_id...
That's it.