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.

Similar Reads

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

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

Creating Table Without Constraints

Query:...

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

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

Dropping the Unique Constraint in the Table

Query:...

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

Contact Us