Creating Table Without Constraints

Query:

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

Explanation:

  • In the above image we have created a table with the name students.
  • We can see that the a table with the name students is created at the left side.
  • As mentioned we have not added any constraints in the given table while creating the column.
  • In the above table we have id , name and course as column names.
  • The id is the primary key in the above table . Similiar to the sql the primary key is unique identification for any records in the table.
  • The primary key should be unique and it should not have any null values while inserting the records in the table.

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:

  • In the above image we have created a table with the name employee.
  • The employee table has the the column names id ,name , age and salary.
  • The id is the primary key of the table employee and every record in the table is uniquely identified with the primary key.
  • The age is created with the check constraint and the value in the column age should be greater than 18 .
  • Now we will insert the records in the table.

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:

  • In the above employee table we have successfully inserted the record values.
  • We have created unique key for every record in the insert operation.
  • After that we have inserted every column value satisfying the specific datatype in the table.
  • We have added the age value greater than 18 in every value.

Output:

RECORDS IN THE EMPLOYEE

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

Age constraint failed.

Explanation:

  • In the above image we have inserted records in the table employee.
  • The insertion was not successfull because the records doesn’t satisfy the check constraint in the table.
  • The age value is less than 18. So the record is not inserted successfully in the table.

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