Тёмный

MySQL: FOREIGN KEYS are easy (kind of) 

Bro Code
Подписаться 1,9 млн
Просмотров 187 тыс.
50% 1

#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);

Опубликовано:

 

16 июн 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 52   
@BroCodez
@BroCodez Год назад
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);
@ahmedhussein6359
@ahmedhussein6359 9 месяцев назад
two typo errors: -- Add a named foreign key constraint to a new table //TYPO ERROR 1: should be unnamed 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 //TYPO ERROR 2: should be transactions ADD CONSTRAINT fk_customer_id FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
@red_uwu
@red_uwu Год назад
Weeks of classes condensed into just a few videos, thank you for making this!
@pdalmao
@pdalmao Год назад
This has saved my life! Managed to get my module done with weeks to spare thanks to you :)
@TheLastRaven
@TheLastRaven Год назад
Litarly covered this in class yesterday tnx Bro! Your timing is as always great
@fgbeast5805
@fgbeast5805 Год назад
You are the best! You make a difference on this world!
@maraki_zrg
@maraki_zrg Год назад
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
@vishalplayzz2580
@vishalplayzz2580 Год назад
true
@xazizayari
@xazizayari 10 дней назад
True I share that point I even learned English to understand Him 😂😂 if I stay At home watching his videos I see much better than going to school wasting time and make my life miserable
@mehmetoz886
@mehmetoz886 4 месяца назад
I am preparing for an interview. The explanation was simple and included good examples.
@bekirs4240
@bekirs4240 Год назад
I rarely need these kinds of videos but i still click and give a like just cuz ur a good guy and teacher
@markvilla7796
@markvilla7796 8 месяцев назад
I am not IT student. I am lucky I found your channel :)
@ArielLegoAndMore
@ArielLegoAndMore Год назад
Bro your tutorials are better than my sql teacher , you probly saved my test and also made me appreciate sql way more, much thanks!
@xazizayari
@xazizayari 10 дней назад
The point is that he Took A serious lesson to funny story So hilarious 😂😂
@babajideamure108
@babajideamure108 8 месяцев назад
@brocode I am late to the party. This is hands down the best video I have watched to explain PK and FK. Thank you
@mithunchandrasaha403
@mithunchandrasaha403 Год назад
Very Nice Explanation,Sir.Needs More.
@celestepozzi9204
@celestepozzi9204 10 месяцев назад
i like ur videos short and unique style of teaching, very easy to get, specially my main language is not english
@jubibi390
@jubibi390 7 месяцев назад
This saved me. I love you Bro.
@bexacode1150
@bexacode1150 Год назад
I really like all your videos! And I really want you will upload about Spring Framework. 😇
@bartsworkshop
@bartsworkshop Год назад
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?
@loebys
@loebys Год назад
nice video, easy to understand. May Allah increase your knowledge and be blessed with knowledge
@amroulouay6819
@amroulouay6819 11 месяцев назад
👍
@dev20114
@dev20114 11 месяцев назад
Thanks a ton Man!
@fahriekurniadi403
@fahriekurniadi403 Год назад
thank you so much, help me to understand
@mr_clean575
@mr_clean575 Год назад
Thank you so much!!!
@maaboii5431
@maaboii5431 Год назад
Really frustrated till now trying to learn how primary key work. I have no idea how to insert multiple same column data (the assigment the gave me) meanwhile primary key doesn't work if you add duplicate data, besides that problem i pretty much know well how to use mySQL, thanks to this video ofc
@tomh6010
@tomh6010 Год назад
Great video! Any plans on covering relational databasing? That's probably the next thing that stumps a lot of people imo Also huge respect for the fundraiser, it's a great cause :)
@mukhtarabebefe7596
@mukhtarabebefe7596 Год назад
Thank you
@samitee8336
@samitee8336 9 месяцев назад
The typing sound is so satisfying😅
@lorenne928
@lorenne928 8 месяцев назад
very great
@drawdownfx4415
@drawdownfx4415 Месяц назад
this is great thanks. To do joins between two tables do we need to specifiy primary and foreign keys? What if you have two tables with no constraints? would it still be possible to do join queries
@boogieman8827
@boogieman8827 Год назад
Can we put a foreign key in a constraint. Like table_1 has column_D.O.B as primary key and table_2 has column_joining_date and joining_date should be greater than D.O.B . How do we do that?
@enriquepasa
@enriquepasa Год назад
Cool video bro :) better than going through theoretical crap - straight to action
@meitangyoon7087
@meitangyoon7087 10 месяцев назад
Great lessons - as a beginner, it's short & sweet makes it easy to follow & understand. Seen other youtube lessons - difficult to follow for beginners. The comments pinned by you are very helful guide & notes. Great job, kudos to you and look forward for more.
@user-ky9sw7yc1m
@user-ky9sw7yc1m 10 месяцев назад
What if I have 3 tables and the 2 nd table has a dk with 3 rd table and vice-versa and it shows error while execution.
@NerdyGeecks
@NerdyGeecks 14 дней назад
hello, can someone explain on how to insert an existing foreign key into two tables like inserting user_id and two tables like transaction and reserve in phpmyadmin?
@luffy_viz
@luffy_viz 25 дней назад
I see last that line delete form table name and where name = ' vishwa ' ; but I can't delete then how to delete in 2 tables
@korosai
@korosai 3 месяца назад
why you use transaction_id as a primary key if we don’t pair it with a foreign key?
@ToughNevyHacSoc
@ToughNevyHacSoc Год назад
Hey Bro Code, Can you please please help me with creating a draw method in a tick-tak-toe game you created your-self, I really want that, and I've been trying for weeks, but I could not figure any way out. Please if you know how to create one, replay in my comment.
@xelozzen5312
@xelozzen5312 Год назад
why is mine key.. doesn't exist in table i write it exact like yours tho
@vishalmane3139
@vishalmane3139 Год назад
didnt work..column missing
@aatifmohammed7031
@aatifmohammed7031 Год назад
how to make foreign key on non primary columns?
@maxxcleiton
@maxxcleiton 16 дней назад
FOREIGN KEY is a PRIMARY KEY from another table. The definition by itself answers your question.
@luckycreative7418
@luckycreative7418 11 месяцев назад
I HAVE A QUESTION WHY YOU HAVE DROPPED THE TABLE? PLS CAN SOMEONE RESPOND!!!!
@freezeyourmind7011
@freezeyourmind7011 8 месяцев назад
I don't understand it too, he deleted table but still can link and use it
@ambi297
@ambi297 Месяц назад
in my sql the data is not taking it says cannot foreign key constraints unable to different tyoes alo what shell i do????
@aetyrunner
@aetyrunner 16 дней назад
Pk and FK must have same data type, like INT/SMALLINT/etc are all INT, but not like INT-DECIMAL or INT-VARCHAR Then if your PK is SMALLINT (one of most common cases I guess) your FK should be: column_id INT, FOREING KEY(column_id) REFERENCES table(column_id)
@amroulouay6819
@amroulouay6819 11 месяцев назад
Why the unique name should be fk 😂😂4:17
@f15srinivas82
@f15srinivas82 Месяц назад
i didnt see god . even i didnt see you bro
@dlamontw
@dlamontw 2 месяца назад
nice ok. i think i got it
@AZZAMNO1
@AZZAMNO1 5 месяцев назад
maybe this will be useful for someone in future i'll drop this here. ALTER TABLE student ADD COLUMN student_loginUsername VARCHAR(35), ADD FOREIGN KEY (student_loginUsername) REFERENCES login(login_username);
Далее
MySQL: JOINS are easy (INNER, LEFT, RIGHT)
5:04
Просмотров 323 тыс.
Primary & Foreign Keys
8:25
Просмотров 469 тыс.
Eddie Hall VS Neffati Brothers
00:11
Просмотров 1,7 млн
Мамооо 😂😂😂
00:21
Просмотров 95 тыс.
Add a foreign key with mySQL workbench
24:02
Просмотров 9 тыс.
6 SQL Joins you MUST know! (Animated + Practice)
9:47
Просмотров 104 тыс.
MySQL: TRIGGERS
16:50
Просмотров 101 тыс.
Python 101: Learn the 5 Must-Know Concepts
20:00
Просмотров 1 млн
MySQL: PRIMARY KEYS are easy
5:25
Просмотров 39 тыс.
Learn SQL Basics in Just 15 Minutes!
16:57
Просмотров 110 тыс.