Learning MySQL - FOREIGN KEY CONSTRAINTS

Published: 02 June 2019
on channel: Steve Griffith - Prof3ssorSt3v3
44,679
697

This tutorial covers what Foreign Keys are, what Constraints are, how to create foreign keys, constraints and how to implement actions based on the constraints.

MySQL Playlist:

MySQL Foreign Key Constraint Reference:

CODE SAMPLE:
ALTER TABLE characters DROP FOREIGN KEY `fk_character_race`;
DROP TABLE IF EXISTS races;
DROP TABLE IF EXISTS characters;

CREATE TABLE races (
race_id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
race_name VARCHAR(30) NOT NULL
)ENGINE=INNODB;

CREATE TABLE characters(
character_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY Key,
character_name VARCHAR(50) NOT NULL,
race_id TINYINT UNSIGNED NOT NULL,
INDEX `idx_race`(race_id),
CONSTRAINT `fk_character_race`
FOREIGN KEY (race_id)
REFERENCES races(race_id) ON UPDATE CASCADE ON DELETE RESTRICT
)ENGINE=INNODB;


Watch video Learning MySQL - FOREIGN KEY CONSTRAINTS online without registration, duration hours minute second in high quality. This video was added by user Steve Griffith - Prof3ssorSt3v3 02 June 2019, don't forget to share it with your friends and acquaintances, it has been viewed on our site 44,679 once and liked it 697 people.