Open In App

MariaDB Primary Key

Primary keys are­ important in the field of relational database­s. They keep the­ data unique and aid in easy data fetching. This article is about understanding the primary keys in MariaDB. MariaDB is a wide­ly used open-source syste­m for managing relational databases.

The MariaDB database is used for various purposes such as data warehousing, e-commerce, enterprise-level features, and logging applications. MariaDB will efficiently enable you to meet all your workload. It works in any cloud database and works at any scale either small or large.



In this article, We will learn about the PRIMARY Key in MariaDB along with examples and perform various operations and queries and so on.

PRIMARY Key in MariaDB

PRIMARY key is a very important part of the database, it gives each record in a table a unique value making sure values aren’t the same. This guarantees that no two records have the same value in the column picked as the PRIMARY key.



Primary keys are really important because they keep data clean and prevent duplicates. The primary key also connects different tables using foreign keys.

It lets us link records between tables. The PRIMARY key helps keep data correct and is also important for making it quicker to get data. Searching and getting data is often fast and automatic for the primary key. The key can be one or more columns.

Defining a Primary Key

When we make­ a table in MariaDB, we pick the column or group of columns to be­ the PRIMARY key. This chosen columns must have­ only unique values. The syste­m helps enforce this rule­, no duplicate values allowed.

Syntax:

CREATE TABLE employees
(
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50)
);

Output:

creating a Primary Key

Explanation:

Adding a Primary Key to a Table

To add a primary key in a table­ that’s already there, we­ use an ALTER TABLE statement. We ge­t the choice to make a ne­w column or use one that’s already the­re as the primary key. The­ query for doing this :

Syntax:

ALTER TABLE students
ADD PRIMARY KEY (student_id);

Output:

Adding a Primary Key

Explanation:

Dropping a Primary Key From a Table

Removing a primary key from a table is needy in certain scenarios. We’ll explore the syntax and steps involved in dropping a primary key, emphasizing the careful considerations to maintain data integrity.

Syntax:

ALTER TABLE employees
DROP PRIMARY KEY;

Output:

Dropping a Primary Key

Explanation:

Primary Key and Auto_Increment Column

Understanding the relationship between primary keys and auto_increment columns is reuqired for efficient database management. We’ll discuss how an auto_increment column can be part of a primary key and its implications.

Syntax:

CREATE TABLE orders
(
order_id INT AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(100),
quantity INT
);

Output:

column Primary Key and Auto_increment

Explanation:

Multiple MariaDB Primary Key Example

Several examples will be provided to illustrate different scenarios of defining primary keys, including single-column primary keys, composite primary keys, and primary keys with auto_increment columns.

Syntax (Single-column primary key):

CREATE TABLE books
(
book_id INT PRIMARY KEY,
title VARCHAR(100),
author VARCHAR(50)
);

Output:

single primary key

Explanation:

Syntax (Composite primary key):

CREATE TABLE purchase
(
customer_id INT,
product_id INT,
purchase_date DATE,
PRIMARY KEY (customer_id, product_id)
);

Output:

Composite primary key

Explanation:

Syntax (Primary key with auto_increment column):

CREATE TABLE customers 
(
customer_id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
);

Output:

Primary key with auto increment

Explanation:

Conclusion

In this article, We have learned about the PRIMARY Key in which Primary Keys ensure each row in a table has a unique, non-null value and preventing data duplication and maintaining data integrity.

We also have seen the Add the PRIMARY key in Existing Table, Creating the PRIMARY in New Table, PRIMARY Key with Single and Composite Key. A PRIMARY keys helps kee­p our data in order and makes sure our database­ works its best.


Article Tags :