The FOREIGN KEY constraint specifies which column to use as the foreign key, which is typically a primary key to another table.
You can apply rules or constraints to your database table when you create it with CREATE TABLE, or later on with ALTER TABLE.
Example:
CREATE TABLE Director (
DirectorID SMALLINT UNSIGNED AUTO_INCREMENT,
FirstName VARCHAR(50),
LastName VARCHAR(50),
PRIMARY KEY (DirectorID)
);
CREATE TABLE Movie (
MovieID SMALLINT UNSIGNED AUTO_INCREMENT,
DirectorID SMALLINT UNSIGNED,
Title VARCHAR(100),
PRIMARY KEY (MovieID),
FOREIGN KEY (DirectorID) REFERENCES Director(DirectorID)
);
Subscribe to Appficial for more programming videos coming soon. Also, don't forget to click LIKE and comment on the video if it helped you out!
Continue this lesson in the app
Install CourseHive on Android or iOS to keep learning while you move.