Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases

Gregory Ledger
Gregory Ledger
6,116 Points

Foreign Key Constraints

I'm referring to the teacher's notes in Database Keys video.

Given the following code:

CREATE TABLE Orders ( O_Id int NOT NULL, OrderNo int NOT NULL, P_Id int, PRIMARY KEY (O_Id), CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) )

what is "fk_PerOrders", Is it a name of something, does it appear in the resulting database?

Can you have foreign keys without a constraint even tho you are referencing it?

1 Answer

Steven Parker
Steven Parker
231,007 Points

:point_right: The term "fk_PerOrders" is the name of the constraint.

Naming a constraint is optional, but it makes it easy to reference it later for things like enable/disable/drop.

And a foreign key is itself a type of constraint, whether or not the syntax used to create it actually contains the word "constraint".