EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials MariaDB Tutorial MariaDB Foreign Key
Secondary Sidebar
MariaDB Tutorial
  • MariaDB
    • MariaDB Versions
    • MariaDB? list users
    • MariaDB Commands
    • MariaDB odbc
    • MariaDB Workbench
    • MariaDB for windows
    • MariaDB Server
    • MariaDB? Data Types
    • MariaDB? boolean
    • MariaDB phpMyAdmin
    • MariaDB Mysqldump
    • MariaDB Java Connector
    • MariaDB insert
    • MariaDB UPDATE
    • MariaDB? rename column
    • MariaDB AUTO_INCREMENT
    • MariaDB Timezone
    • MariaDB GROUP_CONCAT
    • MariaDB wait_timeout
    • MariaDB MaxScale
    • MariaDB? with
    • MariaDB GUI
    • MariaDB? create?table
    • MariaDB? SHOW TABLES
    • MariaDB alter table
    • MariaDB List Tables
    • MariaDB JSON Functions
    • MariaDB Foreign Key
    • MariaDB? trigger
    • MariaDB Grant All Privileges
    • MariaDB Select Database
    • MariaDB? create database
    • MariaDB Delete Database
    • MariaDB Join
    • MariaDB JSON
    • MariaDB? show databases
    • MariaDB List Databases
    • MariaDB Functions
    • MariaDB? TIMESTAMP
    • MariaDB create user
    • MariaDB add user
    • MariaDB Max Connections
    • MariaDB show users
    • MariaDB Delete User
    • MariaDB? change user password
    • MariaDB? change root password
    • MariaDB reset root password
    • MariaDB IF
    • MariaDB bind-address
    • MariaDB Transaction
    • MariaDB Cluster
    • MariaDB Logs
    • MariaDB Encryption
    • MariaDB? backup
    • MariaDB Replication
    • MariaDB max_allowed_packet
    • MariaDB? performance tuning
    • MariaDB export database
    • MariaDB? import SQL

MariaDB Foreign Key

By Sohel SayyadSohel Sayyad

MariaDB Foreign Key

Definition of MariaDB Foreign Key

MariaDB provides referential integrity constraints we call as foreign key. Foreign key is a set of columns or columns in a parent table that gives the reference to another set of columns or columns we call a child table. Another way we can say that referential integrity constraint between two tables. Those tables contain foreign keys we call the child table and the table which the foreign key is referenced is known as the parent table. Basically, foreign key column in the child table, is a reference of the primary key from the parent table. When we use a foreign key in MariaDB it enforces some integrity rules every time.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

create table table_name (colm name 1, colm name 2, ………colm name N ) [constraint name] foreign key [foreign key name] (colm name or list) reference parent_table_name (column list or set) [on delete reference_option] [on update reference_option];

Explanation:

In above syntax first, we use create table statement after that we constraint name to specify the foreign key name, if we skip this constraint the MariaDB will use the default generated name. In the next part foreign key followed by a foreign name with a list of column names separated by a comma within parentheses. After that, we specify the parent name with the column list after the reference keyword as shown above syntax. Finally, we determine how foreign key maintains referential integrity between two tables that is the child and parent table by using on delete and on update clauses.

How does foreign key work in MariaDB?

Basically foreign key is work when the value of both tables matches that is enforced the referential integrity. Let’s see how foreign key works in MariaDB with different parameter, when we use a foreign key in a table at that time we can control the operation by using different parameter as follows.

On delete no action: This is the default parameter in foreign keys. If any existing referencing key is deleted then the transaction fails at the end of query. The key will be updated by using an update clause.

On update no action: This is a default parameter in foreign key. If any existing key is updated from the parent table then the transaction fails at the end of the query. The foreign key we can delete by using the delete clause.

Examples

Let’s see how foreign keys work in MariaDB with help of different examples as follows.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,130 ratings)

There are two ways to create foreign keys or we can say to add foreign in the table as follows.

1. Create foreign keys at the time of table creation.

create table device(
type_id int auto_increment,
name varchar(100) not null,
primary key(type_id)
);

Explanation:

In the above example, we use create table statement, here we created table name as device with different attribute such as type_id and name with different data type as shown above statement. We assign type_id as a primary key. The result of the above statement we illustrate by using the following snapshot.

MariaDB Foreign Key 1

Let’s create another to implement foreign key constraints.

create table device_type(
device_id int auto_increment,
device_name varchar(100) not null,
type_id int,
primary key(device_id),
constraint fk_de_type
foreign key(type_id)
references device (type_id)
);

Explanation:

In the above example, we use a create table statement. Here we created table name as device_type with a different attributes such as device_id, device_name, and type_id with different data types as well as here we define foreign key name as fk_de_type as shown in above statement type_id is a reference of device table that means (type_id). The result of the above statement we illustrate by using the following snapshot.

MariaDB Foreign Key 2

Now we insert some records into the device table by using the following statement as follows.

insert into device(name)
values
('Television '),
('Computer'),
('Printer'),
('Mouse'),
('Mobile');
select * from device;

Explanation:

With the help of a statement, we inserted the above records into the device. The result of the above statement we illustrate by using the following snapshot.

MariaDB Foreign Key 3

Now insert some record into the device_type table by using the following statement as follows.

insert into device_type (device_id, device_name)
values
(1, 'Samsung'),
(2,'Oppo'),
(3,'Sound'),
(4, 'Head set'),
(5, 'Speaker'),
(8,' iPhone');
select * from device_type;

Explanation:

With the help of a statement, we inserted the above records into the device_type table. The result of the above statement we illustrate by using the following snapshot.

MariaDB Foreign Key 4

We can perform different operations with the help of restrict reference, null reference, and cascade reference option.

2. Second way to create foreign keys is by using the alter table command.

In this method, we can use the alter table command to add foreign keys into the table.

After successfully creating a foreign key we can perform different operations on table and effect shows on both tables, we can perform update, delete, and alter operations.

Rules and regulation for using the foreign key

Basically foreign keys are used as referential integrity constraints in MariaDB and it is works between parent table and child table. We can create foreign keys at the time of table creation or we can use an alter command to create foreign key. When we us reference_option in foreign key, the reference_option accept following five different values as follows.

Cascade: When we make some changes means update or delete in parent table then corresponding rows from child table automatically update or delete.

Set Null: When we deleted rows from parent table then corresponding rows from child table are null.

Restrict: if reference rows is updated from parent table then referencing rows from child table is restricted.

No action: It works same as restrict function as mentioned above.

Set Default: In MariaDB, the set default value worked with the obsolete PBXT storage engine. in which that foreign key set default value in MariaDB, if default value of foreign key is not available then MariaDB shows error message.

Conclusion

We hope from this article you have understood about the MariaDB Foreign Key. From the above article, we have learned the basic syntax of MariaDB Foreign Key and we also see different examples of MariaDB Foreign Key. We also learned the rules of MariaDB Foreign Key. From this article, we learned how and when we use MariaDB Foreign Key.

Recommended Articles

This is a guide to MariaDB Foreign Key. Here we discuss the definition, How does foreign key work in MariaDB? and examples. You may also have a look at the following articles to learn more –

  1. MariaDB vs MySQL
  2. MariaDB vs MongoDB
  3. DBMS vs File System
  4. Database Management Software
Popular Course in this category
SQL Training Program (7 Courses, 8+ Projects)
  7 Online Courses |  8 Hands-on Projects |  73+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more