Total Page Preview:   000000000546

What is foreign key in SQL in English and Hindi

 

Foreign key in SQL in English

In SQL, a foreign key is a column or a set of columns that establish a link between two tables. It creates a relationship between two tables by referencing the primary key of one table in another table. The foreign key enforces referential integrity and helps maintain the consistency of data between two tables.

Here's an example of creating a foreign key in SQL:
Let's say we have two tables, "Orders" and "Customers". The "Orders" table has a foreign key column called "CustomerID" that references the primary key column "CustomerID" in the "Customers" table.
In the above example, we have created two tables - Customers and Orders. The Customers table has a primary key column called CustomerID. The Orders table also has a primary key column called OrderID and a foreign key column called CustomerID. The foreign key column in the Orders table references the CustomerID column in the Customers table using the FOREIGN KEY keyword.
 
The above foreign key definition ensures that any value entered into the CustomerID column in the Orders table must exist in the Customers table. If a CustomerID value is deleted from the Customers table, then any corresponding rows in the Orders table with that CustomerID value will also be deleted, ensuring referential integrity.
 
Foreign keys are an important concept in SQL and help to maintain the integrity of the data across related tables. It is essential to define foreign keys correctly to ensure that the relationships between tables are enforced correctly.
 
 

Foreign key in SQL in Hindi

SQL mein foreign key ek column ya ek set of columns hota hai jo do tables ke beech ek link create karta hai. Foreign key ek aisa constraint hai jo ek table ke column(s) ko dusre table ke primary key column(s) se associate karta hai. Foreign key ke through hum do tables ke beech ek relation establish karte hain aur data consistency maintain karte hain.

Neeche ek example diya gaya hai jisme foreign key kaise create kiya jaata hai SQL Server mein:
Upar diye gaye example mein humne "Employees" aur "Departments" table ko create kiya hai. "Employees" table mein humne ek column "DepartmentID" create kiya hai jo "Departments" table ke "DepartmentID" column se refer karta hai. Humne FOREIGN KEY keyword ka use karke DepartmentID column ko Departments table ke DepartmentID column se link kiya hai.
 
Upar diye gaye example mein humne constraint bhi add kiya hai. CONSTRAINT keyword ke through humne FK_Department naam ka constraint create kiya hai jiska use humne DepartmentID column par kiya hai. Is constraint ke through humne Employees table ke DepartmentID column ko Departments table ke DepartmentID column se link kiya hai.
 
Foreign key ke through hum ek table se dusre table tak data ko access kar sakte hain. Agar hum kisi table ke column mein changes karte hain toh foreign key ensure karta hai ki related table mein bhi changes reflect ho jaayein. Foreign key constraint table mein data integrity ko maintain karta hai aur humein duplicate aur inconsistent data se bachata hai.

 

 

 

Thank You

About Author

Brijesh Kumar

Database Developer

I have more then 6 years Experience in Microsoft Technologies - SQL Server Database, ETL Azure Cloud - Azure SQL Database, CosmosDB, Azure Data Factory, PowerBI, Web Job, Azure Function, Azure Storage, Web Apps, Powershall and Database Migration On-Premise to Azure Cloud.
LinkedIn : https://www.linkedin.com



Comments


                           
                           

                           

                           

Facebook User: