SQLite is a serverless architecture, which does not require any server or administrator to run or process queries. This database system is used to develop embedded software due to its lightweight, and low size. It is used in Desktop applications, mobile applications televisions, and so on.
Foreign Key in SQLite
- A Foreign Key is a column or set of columns in the database that refer to the primary key of another table.
- A Foreign Key is used to ensure referential integrity in the database. Referential Integrity ensures the relationship between the tables is valid.
- It means the value in the cell of one table must be the same as the value present in the cell of another table. The foreign key is the key through which we can interact with other tables. Through foreign keys, data retrieval is very fast.
- Foreign keys play a vital role in JOINS operations because in join operations there is the same column in two tables which we achieved through the FOREIGN KEY.
Syntax:
Let's understand the syntax with the help of an example. We will create two tables Employees and Department Table.
Employees TABLE
CREATE TABLE Employees
(
emp_ID varchar(255),
emp_name varchar(255),
emp_city varchar(255),
emp_deptID varchar(255) PRIMARY KEY
)
Department TABLE
CREATE TABLE Department
(
emp_deptID varchar(255) ,
emp_dept_name varchar(255)
FOREIGN KEY (emp_deptID) REFERENCES Employees(emp_deptID)
)
If you clearly saw, then there is a column called 'emp_deptID' which is common in both tables. This column acts as a PRIMARY KEY for the Employees table and acts as a FOREIGN KEY for the Department table.
The column acts as a PRIMARY KEY for those Tables, then the table is called Parent Table and the the same column acts as a FOREIGN KEY for the another table then that table is called Child Table.
FOREIGN KEY (emp_deptID) REFERENCES Employees(emp_deptID)
This above line specifies that the emp_deptID in the Department acts as a FOREIGN KEY and we took the referance of emp_deptID from the Employees table.
Example of FOREIGN KEY
Let's insert some entries into our tables.
Insert into Employees Table
INSERT INTO Employees VALUES(01, 'Vipul', 'Dehradun', 01);
INSERT INTO Employees VALUES(02, 'Nakul', 'Amritsar', 02);
Insert into Department Table
INSERT INTO Department VALUES(01,'Content');
INSERT INTO Department VALUES(02,'Finance');
Explanation:
In this data insertion example, we ensure that the emp_deptID values in the Department table must match valid emp_deptID values from the Employees table,for establishing a valid foreign key relationship.
Now, if you try to insert an order with a non-existent emp_deptID , SQLite will reject the operation, enforcing referential integrity.
FOREIGN KEY with JOINS
As we have discussed above, Foreign key is play a vital role in join operations. Because for joins operations we should have a common column in both table, so here Foreign Key came into picture and make our query very easy. Let's look with example from above tables.
SELECT Employees.emp_id, Employees.emp_name, Employees.emp_city,
Employees.emp_deptid, Department.emp_dept_name
FROM
Employees JOIN Department
ON (Employees.emp_deptid = Department.emp_deptid)
Output:
After JOIN OperationExplanation:
In the above query we perform the join operation on both table Employees and Department tables. Under the ON block, we have inserted the column(emp_deptid) with the same name for joining the tables i.e FOREIGN KEY.
Conclusion
The use of Foreign Keys in SQLite ensures referential integrity, allowing for valid relationships between tables. This feature is particularly crucial for JOIN operations, simplifying queries by establishing connections between tables through common columns. With the ability to enforce data consistency and facilitate efficient data retrieval, SQLite, along with Foreign Keys, provides a robust solution for managing relational databases.
Similar Reads
PL/SQL Foreign Key
Maintaining data integrity is essential in relational database management systems. One essential concept in ensuring data consistency is the use of foreign keys. In PL/SQL, a foreign key creates relationships between tables, ensuring that the data in one table corresponds to the data in another. Thi
7 min read
PostgreSQL - Foreign Key
Foreign keys play a crucial role in relational databases by establishing relationships between tables and safeguarding data integrity. In this PostgreSQL foreign key tutorial, we'll cover how foreign keys work, their importance and how to create them. We will also learn about foreign key constraints
5 min read
SQL FOREIGN KEY Constraint
A FOREIGN KEY constraint is a fundamental concept in relational databases, ensuring data integrity by enforcing relationships between tables. By linking a child table to a parent table, the foreign key establishes referential integrity. This constraint ensures that the values in the foreign key colu
5 min read
SQLite Joins
SQLite is a server-less database engine and it is written in C programming language. It is developed by D. Richard Hipp in the year 2000. The main motive for developing SQLite is to overcome the use of complex database engines like MySQL etc. It has become one of the most popularly used database eng
5 min read
Foreign key in MS SQL Server
A foreign key in SQL Server plays a crucial role in establishing and enforcing relationships between tables. It is a column or a set of columns in a table that references the primary key or a unique key in another table. By using foreign key constraints the SQL Server keeps data consistent between r
6 min read
Foreign Key in MariaDB
MariaDB is an open-source database system which is similar to MySQL. It provide various features such as high availability and vertical scalability to allow database to scale up over various nodes or single node as features like Galera Cluster in MariaDB. The Foreign keys are the most important feat
6 min read
Composite Key in SQL
A composite key is a primary key that is made up of more than one column to uniquely identify records in a table. Unlike a single-column primary key, a composite key combines two or more columns to ensure uniqueness. While any of the individual columns in a composite key might not be unique on their
2 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
FULL OUTER JOIN in SQLite
In the area of data querying and manipulation, the ability to combine information from different sources is important. SQLite, a popular embedded database engine, offers a range of join operations to fast process. One such operation FULL OUTER JOIN is particularly powerful as it allows us to merge d
4 min read
MySQL RIGHT JOIN
In databases, data is stored in multiple tables and it is often necessary sometimes to combine two or more tables to fetch the required data. In MySQL, joins enable the merging of multiple tables based on the common columns. In this article, we are going to explore MySQL RIGHT JOINS which is a type
5 min read