Adding the Unique Constraint to the Table
- In this we will use the alter keyword to add the unique constraint to the table.
- First we will create a table in the database without any constraints in the database.
- Then will use the alter along with the unique constraint to add the unique constraint to the given column.
- First we will create a table without any constraint in database.
Query:
CREATE TABLE employeees
(
name VARCHAR(255),
subject VARCHAR(255),
id INT,
subject_id INT
PRIMARY KEY (id)
);
Explanation:
- In the above image we have created a table with the name employees.
- In the employees table we have created some of the columns such as id, name, subject and the subject_id.
- The id in the table employees was given as the primary key.
- Except the primary key we have not added any constraints to the above table.
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:
- In the above image we have used the alter keyword to add the unique constraint to the a subject_id.
- we have used the alter keyword along with add to add the constraint to the column subject_id in the table.
- Then the unique constraint will be added to the table automatically.
- With the help of the above syntax we can add the unique constraint to the table in the database.
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.
Contact Us