Open In App

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 languages like Java and Python, including PHP language.

In this article, We will understand the Unique Constraint with examples and so on.



Create Database

For performing the operations in the table in a particular database. we have to create a table in the database. we can create a database with the help of the below syntax.

CREATE DATABASE database_name ;

now we will create a database with the name details. After creating the database in the server we will create a table in the database. The above syntax will create a database in the server.
Query:



CREATE DATABASE DETAILS;

Explanation:

Creating a Table with Constraints

Before getting to use the constraints while creating a table in the database we will get to know about the constraint meaning and the constraints available in the mariaDB . we will discuss about the check constraint and unique constraint in the MariaDB.

Generally constraint is nothing but a rule. In this we will discuss about the check constraint and unique constraint.

In Database the data is stored in the form of table. A table is simply defined as set of records values along with the field names.

Syntax:

CREATE TABLE table_name
(
column_name data_type,
column_name1 data_type1 [constraint_name]
...................................,
);

Let’s create a sample table in our database without constraints in a given database.

Creating Table Without Constraints

Query:

CREATE TABLE students
(
id INT,
name VARCHAR(17),
course VARCHAR(15),
PRIMARY KEY (id)
);

Explanation:

Creating a Table Using Check Constraint

Query:

CREATE TABLE employee
(
id INT,
name VARCHAR(255),
age INT CHECK( age > 18 ),
salary INT,
PRIMARY KEY (id)
);

Explanation:

Condition 1: Let’s Insert some records for checking Check Constraints

INSERT INTO employee (id, name, age, salary) 2 VALUES
(1,"Krishna", 25, 200000),
(2, "Rama",26,100000),
(3, "Hanuman", 20, 300000),
(4, "Shiva",19,400000);

Explanation:

Output:

RECORDS IN THE EMPLOYEE

Condition 2: Check values during Insertion by applying the CHECK Constraints.

Age constraint failed.

Explanation:

Using the Unique Constraint

The unique constraint in the MariaDB is used to check whether the values in the column are unique or not. It should have mainly atomic values.

Creating a Table with Unique Constraint:

Condition 1: Giving unique constraint for the Single column.

Query:

CREATE TABLE staff
(
id INT,
name VARCHAR(255),
subject VARCHAR(255),
subject_id INT UNIQUE,
PRIMARY KEY (id)
);

Explanation:

Condition 2: Giving Unique Constraint for Multiple Columns

Query:

CREATE TABLE STUDENTS
(
id INT,
name VARCHAR(255),
subject VARCHAR(255),
subject_id INT,
email VARCHAR(255),
UNIQUE (id, subject_id, email)
);

Explanation:

Adding the Unique Constraint to the Table

Query:

CREATE TABLE employeees
(
name VARCHAR(255),
subject VARCHAR(255),
id INT,
subject_id INT
PRIMARY KEY (id)
);

Explanation:

Now we will add the unique constraint to any of the column that was given to the table.

we will add the unique constraint to the subject_id in the table.

Query:

ALTER TABLE employeees ADD CONSTRAINT UNIQUE (subject_id);

Explanation:

Dropping the Unique Constraint in the Table

Query:

ALTER TABLE employeees DROP CONSTRAINT subject_id;

Explanation:

Conclusion

The Constraints that were created in any of the table helps us to prevent the duplication of data. The problem is that whenever the problem of data duplication occurs in the data base then processing the queries will be difficult . The constraints are also used to keep the records in the database based on the field data type. It is because whenever we want to insert the data in a proper format the constraints will restrict the insertion of data when we insert the data in the improper format.


Article Tags :