MySQL: FOREIGN KEYS are easy (kind of)

preview_player
Показать описание
#MySQL #course #tutorial

00:00:00 intro
00:00:50 CREATE TABLE customers
00:01:17 INSERT INTO customers
00:01:58 CREATE TABLE transactions w/ FOREIGN KEY
00:02:20 Add FOREIGN KEY to a new table
00:03:34 DROP FOREIGN KEY
00:04:07 ADD a named FOREIGN KEY to an existing table
00:05:28 re-populating our transactions table
00:05:45 AUTO_INCREMENT transactions
00:06:05 inserting new rows into transactions
00:07:21 attempting to delete a referenced primary key
00:07:53 conclusion

CREATE TABLE customers (
customer_id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50),
last_name VARCHAR(50)
);
INSERT INTO customers (first_name, last_name)
VALUES ("Fred", "Fish"),
("Larry", "Lobster"),
("Bubble", "Bass");
SELECT * FROM customers;

-- Add a named foreign key constraint to a new table
CREATE TABLE transactions (
transaction_id INT PRIMARY KEY AUTO_INCREMENT,
amount DECIMAL(5, 2),
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
ALTER TABLE transactions
AUTO_INCREMENT = 1000;

-- Add a named foreign key constraint to an existing table
ALTER TABLE customers
ADD CONSTRAINT fk_customer_id
FOREIGN KEY (customer_id) REFERENCES customers(customer_id);
Рекомендации по теме
Комментарии
Автор

CREATE TABLE customers (
customer_id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50),
last_name VARCHAR(50)
);
INSERT INTO customers (first_name, last_name)
VALUES ("Fred", "Fish"),
("Larry", "Lobster"),
("Bubble", "Bass");
SELECT * FROM customers;

-- Add a named foreign key constraint to a new table
CREATE TABLE transactions (
transaction_id INT PRIMARY KEY AUTO_INCREMENT,
amount DECIMAL(5, 2),
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
ALTER TABLE transactions
AUTO_INCREMENT = 1000;

-- Add a named foreign key constraint to an existing table
ALTER TABLE customers
ADD CONSTRAINT fk_customer_id
FOREIGN KEY (customer_id) REFERENCES customers(customer_id);

BroCodez
Автор

Weeks of classes condensed into just a few videos, thank you for making this!

red_uwu
Автор

This has saved my life! Managed to get my module done with weeks to spare thanks to you :)

pdalmao
Автор

bro why u always have the BEST tutorials out there and as much as i search the whole yt i always end up warching YOUR vids?😂 keep up the good work man all of us IT students appreciate it. you are far better than our professors

maraki_zrg
Автор

You are the best! You make a difference on this world!

fgbeast
Автор

Litarly covered this in class yesterday tnx Bro! Your timing is as always great

TheLastRaven
Автор

I am preparing for an interview. The explanation was simple and included good examples.

mehmetoz
Автор

I rarely need these kinds of videos but i still click and give a like just cuz ur a good guy and teacher

bekirs
Автор

so clean and neet and explained perfectly

gopinath
Автор

@brocode I am late to the party. This is hands down the best video I have watched to explain PK and FK. Thank you

babajideamure
Автор

Bro your tutorials are better than my sql teacher, you probly saved my test and also made me appreciate sql way more, much thanks!

ArielLegoAndMore
Автор

I am not IT student. I am lucky I found your channel :)

markvilla
Автор

unique key is easy, auto_increment is awesome and foreign_keys are easy (kind of), HUh!! Bro made it easy.

sominramchiary
Автор

thank you so much, help me to understand

fahriekurniadi
Автор

Very Nice Explanation, Sir.Needs More.

mithunchandrasaha
Автор

i like ur videos short and unique style of teaching, very easy to get, specially my main language is not english

celestepozzi
Автор

I really like all your videos!
And I really want you will upload about Spring Framework. 😇

bexacode
Автор

"im going to delete some of our customers" made me laugh xd. this tutorial is very cool, good work bro!

SchSlendy
Автор

Thank you, you made this super easy to understand! One question, would the foreign key ever be a primary key as well in the transactions table?

bartsworkshop
Автор

nice video, easy to understand. May Allah increase your knowledge and be blessed with knowledge

loebys