CHECK Constraint
A CHECK constraint verifies that a value in a column or collection of columns meets a Boolean expression before adding or upgrading it. The general syntax of the MariaDB CHECK constraint by the following query:
Syntax:
YOUR_COLUMN_NAME DATA TYPE CHECK(CONSTRAINT);
Now, let’s see the Content_Writing_Classes table by the following query:
CREATE TABLE Content_Writing_Classes
(
STUDENT_ID INT AUTO_INCREMENT PRIMARY KEY,
SITES_NAME VARCHAR(50),
TOTAL_ADMISSIONS INT,
CONSTRAINT POSITIVE_TOTAL_ADMISSIONS
CHECK(TOTAL_ADMISSIONS> 0)
);
Explanation: In the Content_Writing_Classes table, the data type of the TOTAL_ADMISSIONS column is an integer. To have a positive total number of admissions, we have added the CHECK constraint in the following query:
CHECK(TOTAL_ADMISSIONS > 0);
The check constraint appears after the data type in the column description. The check keyword is preceded by a Boolean expression enclosed in brackets.
Once we have used the check constraint is placed then we can’t insert or update a value to the column which will evaluate the condition to be false.
Constraints provide restrictions on the data you can add to a table. This allows you to enforce data integrity from MariaDB, rather than through application logic. When a statement violates a constraint, MariaDB throws an error.
There are four types of table constraints:
Constraint |
Description |
---|---|
PRIMARY KEY |
Sets the column for referencing rows. Values must be unique and not null. |
FOREIGN KEY |
Sets the column to reference the primary key on another table. |
UNIQUE |
Requires values in column or columns only occur once in the table. |
CHECK |
Checks whether the data meets the given condition. |
MariaDB CHECK Constraint
MariaDB Server is one of the most popular open-source relational databases. It’s made by the original developers of MySQL and guaranteed to stay open source. It is part of most cloud offerings and the default in most Linux distributions.
MariaDB Server turns data into structured information in a wide array of applications, ranging from banking to websites. Originally designed as an, enhanced, drop-in replacement for MySQL, MariaDB Server is used because it is fast, scalable, and robust, with a rich ecosystem of storage engines, plugins, and many other tools that make it very versatile for a wide variety of use cases.
MariaDB is widely used to maintain high compatibility with open-source, with exact matching with MySQL APIs and commands. It includes new storage engines like Aria, ColumnStore, and MyRocks.
Contact Us