Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
Foreign Key with a Null Value in PostgreSQL
Next article icon

How to Create a Table With Multiple Foreign Keys in SQL?

Last Updated : 26 Sep, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

When a non-prime attribute column in one table references the primary key and has the same column as the column of the table which is prime attribute is called a foreign key. It lays the relation between the two tables which majorly helps in the normalization of the tables. A table can have multiple foreign keys based on the requirement.

In this article let us see how to create a table with multiple foreign keys in MSSQL.

 Syntax:

column_name(non_prime) data_type REFERENCES table_name(column_name(prime)

Step 1: Creating a Database

We use the below command to create a database named GeeksforGeeks:

Query:

CREATE DATABASE GeeksforGeeks

Step 2: Using the Database

To use the GeeksforGeeks database use the below command:

Query:

USE GeeksforGeeks

Step 3: Creating 3 tables. The table student_details contains two foreign keys that reference the tables student_branch_details and student_address.

Query:

CREATE TABLE student_details(    stu_id VARCHAR(8) NOT NULL PRIMARY KEY,    stu_name VARCHAR(20),    stu_branch VARCHAR(20) FOREIGN KEY REFERENCES student_branch_details(stu_branch),    stu_pin_code VARCHAR(6) FOREIGN KEY REFERENCES student_address(stu_pin_code)    );  CREATE TABLE student_branch_details(    stu_branch VARCHAR(20) PRIMARY KEY,    subjects INT,    credits INT  );  CREATE TABLE student_address(    stu_pin_code VARCHAR(6) PRIMARY KEY,    stu_state VARCHAR(20),    student_city VARCHAR(20)  );

Output:

The number and type of keys can be checked in the tables section of object explorer on the left side of the UI.

Step 4: Inserting data into the Table  

Inserting rows into student_branch_details and student_address tables using the following SQL query:

Query:

INSERT INTO student_branch_details VALUES    ('E.C.E',46,170),    ('E.E.E',47,178),    ('C.S.E',44,160)    INSERT INTO student_address VALUES    ('555555', 'xyz','abc'),    ('666666', 'yyy','aaa'),    ('777777','zzz','bbb'),    ('888888','www','ccc'),    ('999999','vvv','ddd')

Inserting rows into student_details

Query:

INSERT INTO student_details VALUES  ('1940001','PRATHAM','E.C.E','555555'),  ('1940002','ASHOK','C.S.E','666666'),  ('1940003','PAVAN KUMAR','C.S.E','777777'),  ('1940004','SANTHOSH','E.C.E','888888'),  ('1940005','THAMAN','E.C.E','999999'),  ('1940006','HARSH','E.E.E','888888')

Step 5: Verifying the inserted data 

Viewing the tables student_details,student_branch_details,student_address after inserting rows by using the following SQL query:

Query:

SELECT * FROM student_details  SELECT * FROM student_branch_details  SELECT * FROM student_address

Output:


Next Article
Foreign Key with a Null Value in PostgreSQL

L

lokeshpotta20
Improve
Article Tags :
  • SQL
  • Blogathon
  • Blogathon-2021
  • SQL-Server

Similar Reads

  • How to Create a Table With a Foreign Key in SQL?
    A foreign key is a column or a set of columns in one table that references the primary key of another table. Foreign keys are used to establish and enforce a link between the data in two tables, ensuring referential integrity in the relational database system. In this article, we will explain how to
    6 min read
  • SQL Query to Create Table With a Primary Key
    A primary key is essential in SQL databases to uniquely identify each row in a table. It ensures data integrity by containing only unique and non-NULL values. A table can have only one primary key, which can consist of a single column or multiple columns, called a composite key. In this article, we
    5 min read
  • How to add a foreign key using ALTER in MySQL
    In this article, we will discuss the overview of foreign keys and will discuss how to add a foreign key using ALTER in MySQL step by step. Let's discuss it one by one. Foreign key : If an attribute is a primary key in one table but was not used as a primary key in another table then the attribute wh
    3 min read
  • How to List all Foreign Keys Referencing a Given Table in SQL Server?
    SQL Server is a Relational Database Management System(RDBMS) that allows users to create and manage databases efficiently. In SQL Server, understanding the table's relationship is very important for database design and maintenance. Foreign keys play an important role in establishing relations betwee
    5 min read
  • Foreign Key with a Null Value in PostgreSQL
    PostgreSQL supports foreign keys to ensure referential integrity between tables. Nullable foreign keys add flexibility by allowing child table rows to either reference a parent table or have a NULL value, meaning no association. While this is useful in certain cases, managing nullable foreign keys c
    6 min read
  • How to Delete a Foreign Key Constraint in SQL
    Foreign key constraints are important for maintaining referential integrity in a relational database as they define relationships between tables. However, there are times when we may need to identify which foreign key constraints reference a specific table in SQL Server. This could be necessary for
    4 min read
  • How to Retrieve Data from Multiple Tables in SQL?
    In SQL, retrieving data from multiple tables is a common requirement in database operations. Efficiently combining data from different tables allows developers to create complex queries and extract valuable insights from interconnected datasets. In this article, we will explore multiple approaches t
    5 min read
  • How to Temporarily Disable a Foreign Key Constraint in MySQL?
    MySQL is a popular open-source relational database management system (RDBMS) that is uniquely used to construct expandable and high-productivity databases. MySQL, which was created by MySQL AB and later acquired by its current owner Oracle Corporation, was originally introduced in 1995. MySQL is rep
    4 min read
  • How to Left Join Multiple Tables in SQL
    Left Join is one of the Keywords used while writing queries in SQL. In SQL we normally use Join for the purpose of forming a new table by taking out common data like rows or records or tuples from both the tables which are having matching records in general. Here when it comes to Left Join in SQL it
    3 min read
  • Foreign Key with a Null Value in MySQL
    In databases, foreign keys are like links connecting two tables. They make sure that data in one table matches data in another. But a common question is whether a foreign key can be NULL. In this article, We will explain what NULL values mean for foreign keys, how they work with the help of examples
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences