Constraints
Constraints
The constraint in MySQL is used to specify the rule that allows or restricts
what values/data will be stored in the table. They provide a suitable
method to ensure data accuracy and integrity inside the table. It also
helps to limit the type of data that will be inserted inside the table. If any
interruption occurs between the constraint and data action, the action is
failed.
Syntax
o NOT NULL
o CHECK
o DEFAULT
o PRIMARY KEY
o AUTO_INCREMENT
o UNIQUE
o INDEX
o ENUM
o FOREIGN KEY
This constraint specifies that the column cannot have NULL or empty
values. The below statement creates a table with NOT NULL constraint.
Output
In the above image, we can see that the first INSERT query executes
correctly, but the second statement fails and gives an error that says
column LastName cannot be null.
UNIQUE Constraint
This constraint ensures that all values inserted into the column will be
unique. It means a column cannot stores duplicate values. MySQL allows
us to use more than one column with UNIQUE constraint in a table. The
below statement creates a table with a UNIQUE constraint:
1. mysql> CREATE TABLE ShirtBrands(Id INTEGER, BrandName VARCHAR
(40) UNIQUE, Size VARCHAR(30));
Output
In the below output, we can see that the first INSERT query executes
correctly, but the second statement fails and gives an error that says:
Duplicate entry 'Cantabil' for key BrandName.
CHECK Constraint
Before the version 8.0.16, MySQL uses the limited version of this
constraint syntax, as given below:
1. CHECK (expr)
After the version 8.0.16, MySQL uses the CHECK constraints for all storage
engines i.e., table constraint and column constraint, as given below:
Execute the listed queries to insert the values into the table:
Output
In the below output, we can see that the first INSERT query executes
successfully, but the second statement fails and gives an error that says:
CHECK constraint is violated for key Age.
DEFAULT Constraint
This constraint is used to set the default value for the particular column
where we have not specified any value. It means the column must contain
a value, including NULL.
For example, the following statement creates a table "Persons" that
contains DEFAULT constraint on the "City" column. If we have not
specified any value to the City column, it inserts the default value:
Execute the listed queries to insert the values into the table:
Output
In the below output, we can see that the first insert query that contains all
fields executes successfully, while the second insert statement does not
contain the "City" column but also executed successfully. It is because it
has a default value.
Now, executes the following statement to validate the default value for
the 4th column:
We can see that it works perfectly. It means default value "New York"
stored automatically in the City column.
The following statement creates a table "Person" and explains the use of
this primary key more clearly:
In the below output, we can see that the first insert query executes
successfully. While the second insert statement fails and gives an error
that says: Duplicate entry for the primary key column.
AUTO_INCREMENT Constraint
In the output, we can see that I have not specified any value for the auto-
increment column, so MySQL automatically generates a unique number in
the sequence order for this field.
ENUM Constraint
The ENUM data type in MySQL is a string object. It allows us to limit the
value chosen from a list of permitted values in the column specification at
the time of table creation. It is short for enumeration, which means that
each column may have one of the specified possible values. It uses
numeric indexes (1, 2, 3…) to represent string values.
Now, execute the SELECT statement to see the inserted values into the
table:
Output
INDEX Constraint
This constraint allows us to create and retrieve values from the table very
quickly and easily. An index can be created using one or more than one
column. It assigns a ROWID for each row in that way they were inserted
into the table.
The following illustration creates a table named "shirts" that contains
three columns: id, name, and size.
Next, we need to insert the values into the "Shirts" table using the below
statements:
We can use the query below to retrieve the data using the index column:
Output
Table: Persons
Table: Orders
In the above table structures, we can see that the "Person_ID" field in the
"Orders" table points to the "Person_ID" field in the "Persons" table. The
"Person_ID" is the PRIMARY KEY in the "Persons" table, while the
"Person_ID" column of the "Orders" table is a FOREIGN KEY.
Output