MariaDB is a fast, scalable, open-source community-supported relational database management system that’s also an enhanced version of MySQL. Content management systems (CMS) are a key application of MariaDB. A CMS is a publication system through which web creators can push and manage large quantities of content on a website. In this article, we will understand the Unique Index in MariaDB along with its syntax, examples, and so on.
MariaDB Unique Index
- A unique index in MariaDB means that the values in a specified column or a group of columns are unique all over the table.
- Unlike a normal index, a unique index applies a constraint on the data, preventing the insertion of duplicate values(data).
- This improves data integrity and allows for improved querying or say fast run query.
- In MariaDB, a primary key is a UNIQUE index. If no primary key is explicitly defined for a table, a unique index is created on the columns designated as the primary key.
- Simply we can say a unique index will ensure that any of two rows of the same column in a table can't have the same value.
- Let's take some examples to understand it.
MariaDB Unique Index Statement Examples
To create a unique index, the UNIQUE keyword is used with the CREATE INDEX statement.
Syntax:
CREATE UNIQUE INDEX index_name ON tbl_name (column1, column2, ...);
Example 1: Create Single Column Unique Index in Exiting Table
Let's suppose we have a employees table(already created) which consist of some columns line first_name, last_name, username, userid, and userpassword as Columns. Now we will create a Unique index for username column.
Query:
CREATE UNIQUE INDEX unique_username ON employees (username);
Output:
output of single column unique indexExplanation: In the above query, we have create a unique index for the column username as unique_username of employees table.
Example 2: Create Multiple Columns Unique Index in Exiting Table
Let's create a unique index on multiple columns of employees table
Query:
CREATE UNIQUE INDEX index_name ON employees (first_name, last_name);
Output:
output multiple columns unique indexExplanation: In the above Query, we have create a unique index on column first_name, last_name of employees table.
Example 3: Adding a Single Column Unique Index in New Table Creation
Let's create a new table and along with creation we will define the unique index for a column.
Query:
CREATE TABLE students
(
student_id INT PRIMARY KEY,
student_name VARCHAR(50),
UNIQUE INDEX index_sid (student_id)
);
Output:
output creating new table with unique Index columnExplanation: In the above Query, we have create a table called students which consist of student_id, student_name as Columns. Here we have created a Unique Index for the column student_id as a index_sid.
Example 4: Altering an Existing Table to Add a Unique Index to a Column
Let's suppose we have created a table called customers and while creating we have forget to create the Unique index for the column called email. So we can resolve this problem with the help of ALTER TABLE. Let's see how can we do.
Query:
ALTER TABLE customers
ADD UNIQUE INDEX index_email (email);
Output:
output alter table to add index columnExplanation: In the above query, we have create a Unique Index for the column called email as a index_email with the help of ALTER TABLE Command.
MariaDB Unique Index and Null Values
Its Allows one NULL value per unique indexed column, but NULL is not considered equal to any other value. Or we can say that, the multiple rows of that column can have NULL values. It Supports a fast search for unique values in the indexed columns. However, other then non-NULL values, each rows should must be unique value.
Example:
# Creating a table with a unique index on the 'studentId' column
//creating a table
CREATE TABLE students
(
studentId INT UNIQUE,
studentName VARCHAR(50)
);
//inserting data into the table
# Inserting data with NULL values in 'studentId'
INSERT INTO students (studentId, studentName) VALUES
(1, ' Student1 '),
(2, ' Student2 '),
(NULL, ' Student3 '),
(NULL, ' Student4 ');
Explanation: In the example, the unique index on the studentId column allows multiple NULL values. The uniqueness constraint is only applied to non-NULL values.
Output:
output MariaDB Unique Index and Null ValuesMariaDB Unique Index and Unique Constraint
Unique Index
A unique index in MariaDB is a database object that ensures the uniqueness of values in one or more columns. It allows the database to optimize queries for unique values and helps maintain data integrity.
Unique Constraint
A unique constraint is almost a unique index with a specific name. It provides a more clear and detailed way of ensuring uniqueness, making it easier to understand the purpose of the constraint.
Syntax:
ALTER TABLE tbl_name
ADD CONSTRAINT constraint_name
UNIQUE (column1, column2, ...);
Example:
ALTER TABLE employees
ADD CONSTRAINT unique_email
UNIQUE (empEmail);
Output:
Mariadb Unique Contraints Explanation: In the above Query, We have add the Constraints which is UNIQUE to the empEmail Columns as a unique_email
Conclusion
In this article, we looked at the basics of MariaDB Unique Index, and its operations with syntax and Examples. It is necessary to remember that, while unique indexes and unique constraints are related, but they are not the same thing. And using the UNIQUE Index we can improve our querying. Now after reading the whole article we have a more knowledge about the Unique Index in MariaDB.
Similar Reads
MySQL Unique Index
In MySQL, a unique index is a crucial tool for ensuring that values in a specific column or a group of columns within a table are unique. This feature is essential to maintain data integrity by preventing duplicate entries where uniqueness is required. In this guide, we'll explore MySQL's unique ind
6 min read
SQL Unique Index
A SQL Unique Index is a constraint that ensures no two rows in the indexed columns of a table have the same values, thereby preventing duplicate entries. Unique indexes are crucial for maintaining data integrity, particularly when you need specific columns or combinations of columns to hold only uni
6 min read
Unique Indexes in MongoDB
A unique index in MongoDB is a powerful feature that ensures the uniqueness of values in specific fields within a collection by preventing duplicate entries. Understanding how to create a unique index in MongoDB is essential for maintaining data integrity especially for critical fields such as usern
7 min read
Unique Constraint in MariaDB
MariaDB uses relational databases similar to SQL. SQL is the precedence of MariaDB similar to the SQL mariaDB also uses some of the querying language to process through the databases. The main advantage of the MariaDB is that it runs on various operating systems and supports many programming languag
7 min read
PL/SQL Unique Index
A PL/SQL Unique Index is a powerful database object used to ensure the uniqueness of values in one or more columns of a table. In Oracle databases, indexes play an important role in enhancing query performance by enabling quick retrieval of rows. The unique index specifically enforces a rule that no
6 min read
Showing indexes in MariaDB
In the area of database optimization, indexes play an important role in enhancing performance and speeding up query execution. MariaDB which is a powerful open-source relational database system that offers several indexing options to fast the data retrieval and boost efficiency. In this article, we
3 min read
PostgreSQL - UNIQUE Index
In PostgreSQL, a UNIQUE index is used to ensure that the values in one or more columns are unique across the rows in a table. This is essential for maintaining data integrity and avoiding duplicate entries. This article will provide a detailed overview of UNIQUE indexes, including syntax, examples,
4 min read
Subqueries and EXISTS in MariaDB
Subqueries and EXISTS are powerful tools in MariaDB that enable us to write complex and efficient queries. Subqueries allow us to nest one query inside another and provide a flexible way to retrieve data. The EXISTS operator, on the other hand, it checks for the existence of rows returned by a subqu
6 min read
INSERT RETURNING in MariaDB
MariaDB, an open-source relational database management system, continues to evolve with new features and enhancements. One such feature introduced in version 10.5 is the INSERT...RETURNING statement, which offers a convenient and efficient way to insert data into a table and retrieve the inserted ro
3 min read
MariaDB Subqueries
MariaDB utilizes SQL, a structured query language, as an open-source Relational database management system (RDBMS). It manages and manipulates data efficiently. One powerful function that contributes to the flexibility of MariaDB is using subqueries. This article will focus on MariaDB subqueries. We
5 min read