MySQL Tutorial
Constraints in MySQL are rules applied to table columns to ensure the validity and accuracy of the data. They help maintain data integrity by restricting what values can be inserted into a table.
In MySQL, you can create constraints using the following syntax:
CREATE TABLE table_name ( column1 data_type constraint_type, column2 data_type constraint_type, column3 data_type );
The following constraints are commonly used in SQL:
NOT NULL - Ensures that a column cannot have a NULL
value.
UNIQUE - Ensures that all values in a column are unique.
PRIMARY KEY - Uniquely identifies each record in a table. Combines NOT NULL
+ UNIQUE
.
FOREIGN KEY - Ensures referential integrity by linking a column to another table.
CHECK - Ensures that a column meets a specified condition.
DEFAULT - Provides a default value for a column when no value is specified.
AUTO_INCREMENT - Automatically generates a unique number for a column.