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:
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.
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.
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.
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.
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.
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 –