Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
SQL FOREIGN KEY

SQL FOREIGN KEY: Understanding and Implementing in Relational Databases

Posted on March 16, 2025March 16, 2025 By Rishabh saini No Comments on SQL FOREIGN KEY: Understanding and Implementing in Relational Databases

SQL FOREIGN KEY

A foreign key is a crucial element in relational databases, allowing the establishment of a link between two tables. In simple terms, a foreign key in one table points to the primary key of another table, ensuring referential integrity.

Complete Python Course with Advance topics:-Click Here
SQL Tutorial :-Click Here

Let’s take an example to understand it better:

We have two tables: Employees and Salaries.

Employees Table:

Emp_IdLastNameFirstNameCity
101SharmaRohanMumbai
102VermaAnjaliDelhi
103MehtaKunalBangalore

Salaries Table:

Sal_IdAmountEmp_Id
20150000102
20260000103
20355000101

The Emp_Id column in the “Employees” table is cited in this instance by the Emp_Id column in the “Salaries” table.

The primary key in the “Employees” table is the Emp_Id column.

The “Salaries” table’s Emp_Id column is a Foreign Key.

Why Use Foreign Keys?

A foreign key constraint prevents actions that would destroy links between tables. It also ensures that invalid data cannot be inserted into the foreign key column.

SQL FOREIGN KEY Constraint on CREATE TABLE

To define a foreign key while creating the “Salaries” table, use the following SQL syntax:

MySQL:

CREATE TABLE Salaries
(  
Sal_Id INT NOT NULL,  
Amount INT NOT NULL,  
Emp_Id INT,  
PRIMARY KEY (Sal_Id),  
FOREIGN KEY (Emp_Id) REFERENCES Employees(Emp_Id)  
);

SQL Server / Oracle / MS Access:

CREATE TABLE Salaries
(  
Sal_Id INT NOT NULL PRIMARY KEY,  
Amount INT NOT NULL,  
Emp_Id INT FOREIGN KEY REFERENCES Employees(Emp_Id)  
);

SQL FOREIGN KEY Constraint Using ALTER TABLE

If the “Salaries” table is already created and you need to add a foreign key constraint later, use this syntax:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Salaries  
ADD CONSTRAINT fk_EmpSalaries  
FOREIGN KEY (Emp_Id)  
REFERENCES Employees (Emp_Id);

Dropping a FOREIGN KEY Constraint

To remove a foreign key constraint, use the following syntax:

MySQL:

ALTER TABLE Salaries  
DROP FOREIGN KEY fk_EmpSalaries;

SQL Server / Oracle / MS Access:

ALTER TABLE Salaries  
DROP CONSTRAINT fk_EmpSalaries;

Differences Between Primary Key and Foreign Key in SQL

FeaturePrimary KeyForeign Key
Can be NULLNoYes
UniquenessAlways uniqueCan be duplicated
Identifies recordsUniquely identifies a recordReferences primary key in another table
Quantity per tableOnly one primary keyMultiple foreign keys allowed
IndexingCreates a clustered indexDoes not automatically create an index

Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE

Foreign keys are essential for ensuring data consistency and establishing strong relationships between tables in a relational database. By implementing FOREIGN KEY constraints properly, you can maintain referential integrity and avoid orphaned records in your database.

For more SQL tutorials, stay updated with UpdateGadh!


sql foreign key example
sql foreign key w3schools
primary key in sql
primary key and foreign key in sql with examples
foreign key query in sql
foreign key syntax
foreign key in mysql
sql foreign key references two tables

Post Views: 506
SQL Tutorial Tags:foreign key, foreign keys, foreign keys rdbms, foreign keys vs primary keys, foriegn key, foriegn keys, how to use foreign keys, primary key, primary keys, primary keys versus foreign keys, primary keys vs foreign keys, what is a foreign key, what is a foriegn key, what is a primary key, what's the difference foreign keys and primary keys, what's the difference primary and foreign key, what's the difference primary key and foreign key

Post navigation

Previous Post: Job Portal Using PHP MySQL – Complete Project
Next Post: Flight Booking System in Django

More Related Articles

sql order by clause with descending order SQL ORDER BY Clause with Descending Order SQL Tutorial
SQL DELETE JOIN SQL DELETE JOIN: A Comprehensive Guide SQL Tutorial
SQL INSERT STATEMENT SQL INSERT STATEMENT SQL Tutorial

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may also like

  1. Understanding the SQL DELETE Statement and Its Variants
  2. SQL Temporary Tables: A Handy Tool for Developers
  3. SQL SELECT TOP Clause: A Comprehensive Guide
  4. SQL ORDER BY LIMIT – Retrieve Specific Rows from a Database
  5. SQL FULL JOIN – A Complete Guide
  6. IN vs EXISTS in SQL: A Complete Guide

Most Viewed Posts

  1. Top Large Language Models in 2025
  2. Online Shopping System using PHP, MySQL with Free Source Code
  3. login form in php and mysql , Step-by-Step with Free Source Code
  4. Flipkart Clone using PHP And MYSQL Free Source Code
  5. News Portal Project in PHP and MySql Free Source Code
  6. User Login & Registration System Using PHP and MySQL Free Code
  7. Top 10 Final Year Project Ideas in Python
  8. Online Bike Rental Management System Using PHP and MySQL
  9. E learning Website in php with Free source code
  10. E-Commerce Website Project in Java Servlets (JSP)
  • AI
  • ASP.NET
  • Blockchain
  • ChatCPT
  • code Snippets
  • Collage Projects
  • Data Science Project
  • Data Science Tutorial
  • DBMS Tutorial
  • Deep Learning Tutorial
  • Final Year Projects
  • Free Projects
  • How to
  • html
  • Interview Question
  • Java Notes
  • Java Project
  • Java Script Notes
  • JAVASCRIPT
  • Javascript Project
  • JSP JAVA(J2EE)
  • Machine Learning Project
  • Machine Learning Tutorial
  • MySQL Tutorial
  • Node.js Tutorial
  • PHP Project
  • Portfolio
  • Python
  • Python Interview Question
  • Python Projects
  • PythonFreeProject
  • React Free Project
  • React Projects
  • Spring boot
  • SQL Tutorial
  • TOP 10
  • Uncategorized
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code
  • Online Job Portal System in JSP Servlet MySQL

Most Viewed Posts

  • Top Large Language Models in 2025 (8,612)
  • Online Shopping System using PHP, MySQL with Free Source Code (5,210)
  • login form in php and mysql , Step-by-Step with Free Source Code (4,862)

Copyright © 2026 UpdateGadh.

Powered by PressBook Green WordPress theme