alternative
  • Home (current)
  • About
  • Tutorial
    Technologies
    C#
    Deep Learning
    Statistics for AIML
    Natural Language Processing
    Machine Learning
    SQL -Structured Query Language
    Python
    Ethical Hacking
    Placement Preparation
    Quantitative Aptitude
    View All Tutorial
  • Quiz
    C#
    SQL -Structured Query Language
    Quantitative Aptitude
    Java
    View All Quiz Course
  • Q & A
    C#
    Quantitative Aptitude
    Java
    View All Q & A course
  • Programs
  • Articles
    Identity And Access Management
    Artificial Intelligence & Machine Learning Project
    How to publish your local website on github pages with a custom domain name?
    How to download and install Xampp on Window Operating System ?
    How To Download And Install MySql Workbench
    How to install Pycharm ?
    How to install Python ?
    How to download and install Visual Studio IDE taking an example of C# (C Sharp)
    View All Post
  • Tools
    Program Compiler
    Sql Compiler
    Replace Multiple Text
    Meta Data From Multiple Url
  • Contact
  • User
    Login
    Register

SQL -Structured Query Language - Sql Key & Constraint - Foreign Key Tutorial

Foreign Key Constraint is used, when we want to make the relationship between two tables. A column with Foreign Key in one table is used to relate Column in another table.

The main purpose of the foreign key is that table2 which has a column with a foreign key, will only able to insert data if the table2 foreign key value is already in the table1 column (column that is linked with foreign key).

It makes the database more secure. Suppose some hacker can insert invalid or not specific data, other than that we want in the database from the frontend, which may create a very big problem for us.

For example, in the case of gender - male or female, but hackers try to enter invalid data (i.e other than male and female). Therefore, it can cause a very big problem in the future.

 

The syntax for Foreign key-

CREATE TABLE table2
(
    table2_columnname datatype,

    ....[other column with its datatype],

    FOREIGN KEY (table2_columnname ) REFERENCES table1(table1_columnname)
);

 

Example for Foreign key-

 

 table 1-

CREATE TABLE  user_salary
(
id int primary key,

user_id int unique,

salary int
);

 

table 2-

This table user_detail contains the foreign key, which is in a relationship with the user_salary table.

Without the user_salary table, the user_detail table will not be created. So first we have to create a user_salary table.

CREATE TABLE user_detail
(
    user_id int primary key,

    name varchar(50) not null,

    email varchar(50) unique,

    dateofbirth date,

    FOREIGN KEY (user_id) REFERENCES user_salary(user_id)
);


 

In this, we define user_detail - user_id with foreign key constraint in relationship with user_id of a user_salary table.  With using joins we can retrieve many details from both the table simultaneously.

With constraint name-

CONSTRAINT FK_detailsalary 
FOREIGN KEY (user_id) REFERENCES user_salary(user_id)

It is best practice to add a constraint name for any constraint. just add CONSTRAINT constraint_name before FOREIGN KEY.

By default constraint name is already created. But you can write your own constraint name.

 

Syntax For Adding Foreign Key With Alter Command-

ALTER TABLE user_detail
ADD FOREIGN KEY (user_id) REFERENCES user_salary(user_id);

With Constraint-

ALTER TABLE user_detail
CONSTRAINT FK_detailsalary 
ADD FOREIGN KEY (user_id) REFERENCES user_salary(user_id);

This syntax will add a foreign key after table creation. If it gives an error make sure that records having a foreign key, should match their value with the record in the reference table.

 

Syntax For Deleting Foreign Key With Alter Command-

ALTER TABLE table_name
DROP FOREIGN KEY foreign_key_constraint_name;

For example- 

ALTER TABLE user_detail
DROP FOREIGN KEY FK_detailsalary;

This syntax will delete foreign keys with its constraint name.

In Mysql Workbench-

  • A table containing a foreign key will not be created without its reference table. First, we have to create a reference table.

 

 

 

  • Data with foreign key value will not be inserted in the table if the foreign key value is not present in the reference table. Which makes it more secure.

 

 

 

  • Users will not able to delete or update reference table records if their foreign key value is present in any other table.

 

SQL -Structured Query Language

SQL -Structured Query Language

  • Introduction
  • Overview
    • How To Download And Install MySql Workbench
    • SQL Syntax
  • SQL Database
    • Create Database
    • Drop Database
    • Use / Select Database
  • SQL Table
    • Create Table
    • Alter - Rename Table
    • Alter - Add Column
    • Alter - Modify Column
    • Alter - Rename Column
    • Alter - Drop Column
    • Copy Table
    • Drop Table
  • SQL Insert
    • Insert Record
  • Sql Update
    • Update Record
  • Sql Select
    • Select Record
    • Select - Count
    • Select - Average
    • Select - Sum
    • Select - Min Max
    • Select - Distinct
    • Select - Limit
    • Select - First
    • Select - Last
  • Sql Clause
    • Like Clause
    • And Operator
    • Or Operator
    • Between & Not Between Operator
    • As or Alias
    • In Clause
    • Group By Clause
    • Having Clause
  • Sql Order By
    • Ascending
    • Descending
    • Random
    • Ascending Descending Multiple Column
  • SQL Joins
    • Joins and its type
    • Inner Join
    • Left Join
    • Right Join
    • Full Join
    • Cross Join
  • SQL View
    • Create View
    • Update View
    • Delete View
  • SQL Delete
    • Delete Table
    • Truncate Table
  • Sql Key & Constraint
    • Primary Key
    • Foreign Key
    • Unique Key
    • Not Null Constraint
    • Check Constraint
    • Default Constraint
  • Normalization
    • Overview
    • 1 NF
    • 2 NF
    • 3 NF
    • Boyce Codd normal form ( BCNF )

About Fresherbell

Best learning portal that provides you great learning experience of various technologies with modern compilation tools and technique

Important Links

Don't hesitate to give us a call or send us a contact form message

Terms & Conditions
Privacy Policy
Contact Us

Social Media

© Untitled. All rights reserved. Demo Images: Unsplash. Design: HTML5 UP.

Toggle