4

here are two declaration in sql

create table bookAdoption ( courseId int, sem int, isbn int, PRIMARY KEY(courseId, sem, isbn), FOREIGN KEY(courseId) REFERENCES course(courseId), FOREIGN KEY(sem) REFERENCES enroll(sem) ); 

and the other one

CREATE TABLE bookAdoption ( courseId INT REFERENCES course(courseId) , sem INT REFERENCES enroll(sem), isbn INT REFERENCES text1(isbn), PRIMARY KEY(courseId, sem, isbn) ); 

what is the between using only references keyword

and using both references and foreign keyword both together screenshot

12
  • 2
    According to the grammar, only using references keyword like that is invalid syntax Commented Feb 3, 2013 at 13:17
  • @Esailija both works in mysql Commented Feb 3, 2013 at 13:19
  • @tiger When I run the second example verbatim, I get the expected syntax error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTREFERENCES course(courseId) , sem INT REFERENCES enroll(sem), isbn ' at line 3 Commented Feb 3, 2013 at 13:19
  • @Esailija spelling mistake it should be references instead of intreferences now try Commented Feb 3, 2013 at 13:22
  • 1
    @Esailija: it is ignored (which is documented in the manual) No foreign key constraint will be created and you will be able to insert values that don't exist in the referenced table. See here: sqlfiddle.com/#!2/725388/2 Commented Feb 3, 2013 at 13:31

3 Answers 3

5

You can't use the REFERENCES without a foreign key constraint, as quoted from the MySQL Documentation, FOREIGN KEY Constraints:

Furthermore, InnoDB does not recognize or support “inline REFERENCES specifications” (as defined in the SQL standard) where the references are defined as part of the column specification. InnoDB accepts REFERENCES clauses only when specified as part of a separate FOREIGN KEY specification. For other storage engines, MySQL Server parses and ignores foreign key specifications.

Sign up to request clarification or add additional context in comments.

Comments

1

A FOREIGN KEY can only reference a column that is UNIQUE.

In practice we ussualy add foreign key constraints to primary keys, and we say that the FK references the PK. But it is possible to add such a constraint with any UNIQUE column(s).

In order for this to work SQL syntax requires you to:

  • declare a foreign key
  • tell it what to reference

Comments

-1
What's the difference between foreign key constraint and referencing a column? 

The difference is simple - column referencing is parsed but ignored.

See fiddle - when column referencing is used then neither index nor constraint / foreign key are created.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.