Back to Browse

Understanding SQL Foreign Keys: Building Relationships Between Tables

86 views
Jul 24, 2024
6:11

Learn how to build and manage parent-child relationships in SQL using Foreign Keys. In this video, we create tables for doctors and medical records, establish relationships between them, and explore key SQL commands for inserting, updating, and retrieving data. Perfect for database beginners and those looking to understand relational databases better." drop table if exists Doctors; create table Doctors ( id varchar(15) primary key, first_name varchar(50) not null , last_name varchar(50) not null, specilization varchar(200), gender bigint, dob date, salary int, hospital_id int ); ALTER TABLE DOCTORS alter column gender varchar(10); insert into Doctors values ('D1', 'Ragav', 'Jain', 'Cardiology', 'Male', convert(date,'01-01-1980',105), 250000, 4); insert into Doctors values ('D2', 'Priyanka', 'Verma', 'Physician', 'female', convert(date,'01-01-1995',105), 350000, 3); insert into Doctors values ('D3', 'Sheetal', 'Shetty', 'Neurology', 'F', convert(date,'01-01-1980',105), 200000, 3); insert into Doctors values ('D4', 'David', 'Smith', 'Psyatry', 'M', convert(date,'01-01-1970',105), 150000, 1); insert into Doctors values ('D5', 'James', 'Dias', 'Oncology', 'm', convert(date,'01-01-1985',105), 150000, 2); insert into Doctors values ('D6', 'Ali', 'Abdal', 'Dermatology', 'M', convert(date,'01-01-1990',105), 250000, 5); drop table if exists Medical_Records; create table Medical_Records ( id int , problem varchar(200), date_of_examination date, patient_id varchar(15), doctor_id varchar(15) references doctors(id) ); insert into Medical_Records values (1, 'Food Poisoning', convert(date,'10-01-2023',105), 'P1', 'D2'); insert into Medical_Records values (2, 'Fever and Flu', convert(date,'11-01-2023',105), 'P6', 'D6'); insert into Medical_Records values (3, 'Back Spasm', convert(date,'15-01-2023',105), 'P7', 'D6'); insert into Medical_Records values (4, 'Headache', convert(date,'20-01-2023',105), 'P0', 'D7'); insert into Doctors values ('D7', 'Vijay', 'Abdal', 'Dermatology', 'M', convert(date,'01-01-1990',105), 250000, 4); select * from Doctors; select * from Medical_Records; SQL for Beginners: A Complete Introduction to SQL Basics! https://youtu.be/iZPGAnNZm6U Master SQL Commands: DDL, Data Types & Constraints Explained! https://youtu.be/6if1q_7CGLI Mastering SQL Constraints: Primary Key, Unique Key, Not Null, and CHECK Explained! https://youtu.be/KdGUpf_OTPA Understanding SQL Foreign Keys: Building Relationships Between Tables https://youtu.be/mA_pE0Kg9K4 SQL Identity Column: Auto-Generating Unique Values for Your Tables https://youtu.be/PZVDNekwrNc SQL DML & DDL Commands Explained | Insert, Update, Delete, Truncate, Drop https://youtu.be/DSe2Gw6Z-z0 Master Basic SQL Queries & Operators | Part 1: Fetch, Filter & Count https://youtu.be/HgKW590CVSI SQL Functions and Queries: Aggregate, CAST, REPLACE, ROUND, GETDATE https://youtu.be/w1Vvj4IFYsE Mastering SQL INNER JOIN: Essential Queries & Examples https://youtu.be/NzFpLo1J5oI SQL Group By Explained with Examples | Master Aggregate Queries https://youtu.be/9nrVo0h7Z8o Mastering SQL: Group By, Having, CASE, Order By, Join, Top, and Limit https://youtu.be/sQv2y1CTJIU Introduction to SQL Normalization: 1NF (First Normal Form) - Part 1 https://youtu.be/r_dHyFqK19g Understanding SQL Normalization: 2NF (Second Normal Form) - Part 2 https://youtu.be/2ZF3NVxYIcE Mastering SQL Normalization: 3NF (Third Normal Form) Explained - Part 3 https://youtu.be/ne-0qFcP3ao Master SQL Subqueries: Scalar, Multi-Row, and Correlated Subqueries Explained https://youtu.be/twAnX7-rAAk SQL Tutorial: Remove Duplicate Data Efficiently | Common Interview Question https://youtu.be/M2lwOXk4ZfA SQL Joins Explained: Master INNER, LEFT, RIGHT, FULL, CROSS & SELF Joins with Examples https://youtu.be/0y-u7w7s5ig Master SQL Joins: Self Joins, Outer Joins, and More! https://youtu.be/xfBlQ2x7oSg SQL Queries Using CTE: Profitability & Monthly Sales Differences https://youtu.be/ttKIVVx3JXQ Calculate User Popularity Percentage with SQL CTEs https://youtu.be/_YzwhLdc5z8 SQL Window Functions Explained: ROW_NUMBER, RANK & DENSE_RANK https://youtu.be/wpLNF5hnPxg SQL Window Functions Explained: LEAD & LAG with Real Examples! https://youtu.be/wfubFMAhEds Master SQL Window Functions: FIRST_VALUE, LAST_VALUE, and More! https://youtu.be/FvV0gFIxdA8 Easily Delete Duplicate Rows in SQL Server with ROW_NUMBER() https://youtu.be/p2ZJu3tu91Q Understanding Recursive CTEs in SQL: A Simple Guide https://youtu.be/9H79g5Hv3hE Master SQL PIVOT: Transform Rows into Columns https://youtu.be/iyDJQFY8LAA

Download

0 formats

No download links available.

Understanding SQL Foreign Keys: Building Relationships Between Tables | NatokHD