SQL tutorials 18: SQL Foreign Key Constraint By Manish Sharma
Skills:
Database Integration80%
Key Takeaways
Defines a SQL foreign key constraint using CREATE TABLE and ALTER TABLE statements
Full Transcript
what's up guys I'm Manish from revellion rider.com and I'm back again with another SQL tutorial today's SQL tutorial is about foreign key in my previous video I have shown you what primary key is and how to define it if you haven't watched it then please find the link for the video on primary key in the description below okay let's check out the foreign key similar to primary key we can Define foreign key using either create table statement or Al table statement first we will see how to Define foreign key using Create table statement we can Define foreign key using Create table statement in two different ways first one is on column level and second one is on table level we will start with defining foreign key using Create table statement on column level we have to create two separate table that of parent and child table so our parent table is authors and child table is books our parent table author is just a simple table with two columns author ID and author name where author ID is a primary key column as I have already shown you different way of defining a primary key on table in my last video so I'm going to fast forward this if you haven't watched my primary key tutorial then I'm providing the link in the description below so please watch this video and now let's jump over SQL Developer let's create our parent table okay we have our parent table author we will be using author ID column of author table in our foreign key thus it's mandatory to Define this author ID column in parent table as either primary key or unique key please note that it's mandatory only the primary key column or the unique key column of parent table can participate in foreign key constraint avoiding this may result in error so we have our parent table author with a primary key column author ID now let's create the child table box child table is the core of foreign key constant let's create it we are defining foreign key on column level this is the main column over which we are going to put foreign key using author ID column from our parent table so this column must have same data type and size as of the author ID column of parent table author you can give whatever name you want to this column but as I said data type and size of this column must match with the parent table column author ID let me explain this to you in order to Define foreign key we first Define our column then we specify constant which is a keyword followed by user defined constant name and then references which is again a keyword and then name of parent table and the column of parent table in parenthesis which you want to use in foreign key so we have created our child table with the name of box now let's see the second way of defining foreign key using Create table on table level so for that we will be using our same parent table author for demonstrating I have to drop a child table box and recreate it okay let's drop our child table books for that just write drop table and the table name which is box execute there we go and now let's recreate our child table box just a second let me give a comment as I have already told you that we are going to put our foreign key on book author ID column the third column here so it must have the same data type and the size as of the column in our parent table which we are going to reference here which is our author ID column let's move ahead let me give some spaces so that you can read it clearly okay execute there we go as you can see I first Define all the column of the table and then at the end I Define my constant let's see the last statement of the table constant is the keyword followed by the user defined constant name and our constant foreign key which is again a keyword and then the child table column in the parenthesis over which you want to put foreign key and the keyword references followed by the name of parent table and the column of parent table in parenthesis that's it now let's see how to Define foreign key using alter table statement let's say we have a child table box with all three columns book ID book title and author book ID but we do not have any constant over the table yet so let's start by dropping our child table book and recreating it once again we have to execute this command now we don't have any child table P let's recreate it okay let me give some space so that you people can see it clearly remember we are yeah I was saying remember we are going to create a simple Book Table without any constant this means we are not going to emphasis or put any constant right now on this table execute that's it just a simple table without any constant now we have to put foreign key constant on third column author book ID oh sorry book author ID before emphasizing foreign key constant on the column make sure this column has same data type and size as that of column in parent table which we are going to reference in foreign key now let's alter the table for that just write this gdl command let's format it first okay let's me let me check alterable books add constant book foreign key just make it come here book author not od's [Music] ID same here references okay let's execute okay let's check whether we have this constant on our table box or not for that just go and click over the table name you can click here or here it doesn't matter just click over the table name and then press shift key and F4 key and SQL Developer will show you this Wizard and here just click constant and as you can see we have our foreign key constant B e a i f k and if you click here double click it sorry and you will get to know that this constant is on column position one and uh every detail about this constant you can get from here it means we have a foreign key constant on our table books here so guys that's it for today for more indepth knowledge on foreign key you can read this article just right here on my website you can find the link in the description below also please share this video tutorial with your friends and peer on your social networking if you haven't subscribed already please do so and get latest update on SQL tutorial in case of any Cy feel free to come and take care see you soon with another interesting SQL tutorial this is Manish bye-bye
Original Description
SQL Tutorial 18: SQL foreign key constraint.
Topic covered in this tutorial
1. Defining SQL foreign key using create table at column level
2. Defining SQL foreign key using create table at table level
1. Defining SQL foreign key using Alter Table statement
Links
Website article: http://www.rebellionrider.com/sql-foreign-key.htm
Primary key tutorial: http://www.rebellionrider.com/sql-foreign-key.htm
Create Table Tutorial: http://www.rebellionrider.com/sql-create-table.html
Tool used in this tutorial is SQL Developer.
This tutorial series is part of SQL expert exam certification training. if you are preparing for SQL certification you can use my tutorials. This SQL Tutorial is a part of free training.
Copy Cloud referral link || Use this link to join copy cloud and get 20GB of free storage
https://copy.com?r=j7eYO7
Contacts
E-Mail
RebellionRiderYT@gmail.com
Twitter
https://twitter.com/rebellionrider
Instagram
http://instagram.com/rebellionrider
Facebook.
https://www.facebook.com/imthebhardwaj
Linkedin.
http://in.linkedin.com/in/mannbhardwaj/
Thanks for linking, commenting, sharing and watching more of our videos
This is Manish from RebellionRider.com
Foreign key is an Input/output data constraint which is also known as referential integrity constraint.
Foreign key represent a link or say a relationship between columns of tables.
Similar to primary key constraint Foreign Key constraint is also of two types.
Simple Foreign key constraint and
Composite Foreign key constraint.
Constraint which involves only one column in foreign key in child table and one column in reference key in parent table is called Simple Foreign Key. While the constraint which involves more than one column in foreign key in child table and more than one column in reference key in the parent table is called Composite Foreign Key.
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Manish Sharma · Manish Sharma · 23 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
▶
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Oracle Database tutorials 1: How to install Oracle Database 11g on windows 7
Manish Sharma
Oracle Database tutorials 2:How To install SQL Developer on windows 7
Manish Sharma
Oracle Database tutorials 3:How to enable Line numbers in SQL Developer.
Manish Sharma
Oracle Database tutorials 4: database connectivity using SQL developer and command prompt
Manish Sharma
Oracle Database tutorials 5: how to Fetch Data using SELECT - SQL statement by Manish Sharma
Manish Sharma
Oracle Database11g tutorials 6 | | How to use Concatenation operator, character String
Manish Sharma
Oracle Database11g tutorials 7 | |SQL DISTINCT keyword || SQL tutorials
Manish Sharma
Canon EOS 600D 2 lens kit/Canon rebell EOS T3i 2 lens kit Unboxing
Manish Sharma
First look: ORACLE CERTIFIED ASSOCIATE (OCA) CERTIFICATE - ORACLE DATABASE ADMINISTRATOR
Manish Sharma
Oracle Database11g tutorials 8 || SQL DISTINCT with multiple columns |SQL Distinct with Two columns
Manish Sharma
Oracle Database11g tutorials 9 || What is archive log mode and how to enable archive log mode
Manish Sharma
Oracle Database11g tutorials 10 || SQL Single Row Function (SQL Functions )
Manish Sharma
Oracle Database11g tutorials 11: SQL case manipulation function in Oracle Database
Manish Sharma
how to add channel trailer and section on your youtube channel 2014
Manish Sharma
Oracle Database11g tutorials 12 || SQL Concat Function - SQL character manipulation function
Manish Sharma
Oracle Database11g tutorials 13 || SQL substr function / SQL substring function
Manish Sharma
Oracle Database11g tutorials 14 : How to CREATE TABLE using sql developer and command prompt
Manish Sharma
SQL tutorials 15 || How To CREATE TABLE using enterprise manager 11g
Manish Sharma
Oracle Database11g tutorials 16: How to uninstall oracle 11g from windows 7 64 bit
Manish Sharma
ORACLE CERTIFIED PROFESSIONAL(OCP) CERTIFICATE First look - ORACLE DATABASE ADMINISTRATOR
Manish Sharma
Plantronics audio 655 USB headset with Mic Unboxing and Review and Plantronics audio 655 Mic test
Manish Sharma
SQL tutorials 17: SQL Primary Key constraint, Drop primary Key
Manish Sharma
SQL tutorials 18: SQL Foreign Key Constraint By Manish Sharma
Manish Sharma
SQL tutorial 19: ON DELETE SET NULL clause of Foreign Key By Manish Sharma (RebellionRider)
Manish Sharma
SQL tutorials 20: On Delete Cascade Foreign Key By Manish Sharma (RebellionRider)
Manish Sharma
SQL tutorial 21: How To Rename Table in SQL using ALTER TABLE statement By Manish Sharma
Manish Sharma
SQL tutorial 22: How to Add / Delete column from an existing table using alter table
Manish Sharma
SQL tutorial 23: Rename and Modify Column Using Alter Table By Manish Sharma (RebellionRider)
Manish Sharma
SQL tutorial 24 :SQLJoins- Natural Join With ON and USING clause By Manish/Rebellionrider
Manish Sharma
Oracle Database11g tutorials 25: How to install Oracle Database 11g Express Edition R2 on Windows 7
Manish Sharma
SQL tutorial 26: Introduction to SQL Joins in Oracle Database
Manish Sharma
Vidcon 2014 YouTube Fan Funding: How to enable fan funding on YouTube Channel
Manish Sharma
SQL tutorial 27: Right Outer Join in SQL by Manish Sharma for RebellionRider
Manish Sharma
SQL tutorial 28: Left Outer Join By Manish Sharma / RebellionRider
Manish Sharma
SQL tutorial 29: Full Outer Join with example By Manish Sharma/ RebellionRider
Manish Sharma
SQL tutorial 30: Inner Join In SQL by Manish Sharma/RebellionRider
Manish Sharma
SQL tutorial 31 : SQL Cross Join In Oracle Database By Manish Sharma from RebellionRider
Manish Sharma
SQL tutorial 32: How To Insert Data into a Table Using SQL Developer
Manish Sharma
SQL tutorial 33:How To Insert Data into a Table Using SQL INSERT INTO dml statement
Manish Sharma
SQL tutorial 34: How to copy /Insert data into a table from another table using INSERT INTO SELECT
Manish Sharma
SQL tutorial 35: DELETE and TRUNCATE how to delete data from a table
Manish Sharma
SQL tutorial 36: how to create database using database configuration assistant DBCA
Manish Sharma
SQL tutorial 37: How to create NEW USER account using Create User statement in Oracle database
Manish Sharma
SQL tutorial 38: How to create user using SQL Developer in Oracle database
Manish Sharma
SQL tutorial 39: How to create user in oracle using Enterprise Manager
Manish Sharma
SQL tutorial 40: DBA Trick, How to drop a user when it is connected to the database
Manish Sharma
Motorola Moto G 2nd Generation / G2 Unboxing and Review
Manish Sharma
SQL tutorial 41: How to UNLOCK USER in oracle Database
Manish Sharma
SQL tutorial 42: How to Unlock user using SQL Developer By Manish Sharma RebellionRider
Manish Sharma
SQL tutorial 43: How to create an EXTERNAL USER in oracle database By Manish Sharma RebellionRider
Manish Sharma
SQL tutorial 44: How to import data from Microsoft Excel to Oracle Database using SQL Developer
Manish Sharma
SQL tutorial 45: Introduction to user Privileges in Oracle Database By Manish Sharma RebellionRider
Manish Sharma
SQL tutorial 46: What are System Privileges & How To Grant them using Data Control Language
Manish Sharma
SQL tutorial 47: How to Grant Object Privileges With Grant Option in Oracle Database
Manish Sharma
SQL tutorial 48: How to create Roles in Oracle Database
Manish Sharma
SQL tutorial 49: CASE - Simple Case Expression in Oracle Database (1/2)
Manish Sharma
SQL tutorial 50: CASE - Searched Case Expression In Oracle (2/2)
Manish Sharma
SQL tutorial 51: DECODE function in Oracle Database By Manish Sharma (RebellionRider)
Manish Sharma
Oracle Database Tutorial 52 : Data Pump expdp - How to Export full database using expdp
Manish Sharma
Oracle Database Tutorial 53 : Data pump expdp - How to Export tablespace in Oracle Database
Manish Sharma
More on: Database Integration
View skill →Related Reads
📰
📰
📰
📰
What Happens When 6 AI Agents Collaborate on Your Code
Dev.to AI
MCP Design Patterns: 6 Architectures for Your AI Tools
Dev.to AI
The Last JSONL Line Is Not a State Machine
Dev.to · Agent Island
Devlog: How Our AI Agents Built the WebApp Guard & Manifest Architect in Record Time
Dev.to · Denis
🎓
Tutor Explanation
DeepCamp AI