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:
Condition 2: Check values during Insertion by applying the CHECK Constraints.
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.
Contact Us