English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to use SQL constraints.
Constraints are restrictions on one or more columns of a table to limit the type of values that can be stored in the column. Constraints provide a standard mechanism to maintain the accuracy and integrity of data within a database table.
There are several types of constraints in SQL, including:
Now, let's discuss each constraint in detail.
The NOT NULL constraint specifies that the specified column does not accept NULL values.
This means that if NOT NULL imposes a constraint on a column, then you must insert a new row in the table without adding a non-NULL value for that column.
以下SQL语句创建一个名为person'sThe table, which has four columns, of which three columnsid,nameemployees (employees)table, andNULL values are not accepted.
column to create a default value. id INT NOT NULL, ) id INT NOT NULL PRIMARY KEY,3name VARCHAR( 0) NOT NULL, ) birth_date DATE,15) NOT NULL );
Note:A null value or NULL is different from zero, a blank or a string of zero length, such as ''. NULL indicates that no input has been entered.
The PRIMARY KEY constraint identifies the column or set of columns that uniquely identify the row values in the table. No two rows in the table can have the same primary key value. Similarly, you cannot input NULL values in the primary key column.
以下SQL语句创建一个名为The following SQL statement creates a table namedpersonsidThe column specified as the primary key. This means that the field does not allow NULL or duplicate values.
column to create a default value. CREATE TABLE persons ( ) id INT NOT NULL PRIMARY KEY,3name VARCHAR( 0) NOT NULL, ) birth_date DATE,15) NOT NULL );
Tip:The primary key is usually composed of a single column in a table, but it can also be composed of multiple columns to form the primary key, for example, the email address or assigned identification number of an employee is the logical primary key of the employee table.
The UNIQUE constraint restricts one or more columns in a table to contain unique values.
Although both UNIQUE constraints and PRIMARY KEY constraints enforce uniqueness, UNIQUE should be used when you want to enforce uniqueness on a column or a combination of columns (not the primary key) rather than a constraint.
以下SQL语句创建一个名为The following SQL statement creates a table namedpersonstable, andphone
column to create a default value. CREATE TABLE persons ( ) id INT NOT NULL PRIMARY KEY,3name VARCHAR( 0) NOT NULL, ) birth_date DATE,15column specified as unique. This means that the field does not allow duplicate values. );
Note:) NOT NULL UNIQUE
DEFAULT constraint (DEFAULT)
DEFAULT constraint specifies the default value for a column.column's default value is whenINSERT
statement, the database engine will insert a value into the column when a specific value is not explicitly assigned. The following SQL statement is for/CountryRegion
column to create a default value. CREATE TABLE persons ( ) id INT NOT NULL PRIMARY KEY,3name VARCHAR( 0) NOT NULL, ) birth_date DATE,15phone VARCHAR( ) NOT NULL UNIQUE,3country VARCHAR( );
Note:0) NOT NULL DEFAULT 'Australia'
FOREIGN KEY constraint (FOREIGN KEY)
foreign key (FK) is a column or a combination of columns used to establish and strengthen relationships between data in two tables.This showsemployees (employees)Departmentanddepartments(employeestabledept_idcolumn is an example of the relationship betweendepartmentstable's primary key column. Therefore,dept_id that matches thecolumnnamed employeestable's foreign key forDepartmenttable.
In MySQL, you can create a FOREIGN KEY when creating a table by defining constraints. The following statement creates a foreign key inemployeestabledept_idto establish a foreign key on thedepartmentstabledept_idcolumn.
CREATE TABLE employees ( emp_id INT NOT NULL PRIMARY KEY, emp_name VARCHAR(55) NOT NULL, hire_date DATE NOT NULL, salary INT dept_id INT, FOREIGN KEY (dept_id) REFERENCES departments(dept_id) );
CHECK constraints are used to limit the values that can be placed in a column.
For example, you can create a CHECK constraint to limit the value range of the salary column, which only allows values between3to10between 0,000 and 0,000. This can prevent the input of salaries that exceed the normal salary range. Here is an example:
CREATE TABLE employees ( emp_id INT NOT NULL PRIMARY KEY, emp_name VARCHAR(55) NOT NULL, hire_date DATE NOT NULL, salary INT NOT NULL CHECK (salary >= 3000 AND salary <= 10000), dept_id INT, FOREIGN KEY (dept_id) REFERENCES departments(dept_id) );
Note: MySQL does not support SQL check constraints. Although, all MySQL storage engines will parse the CHECK clause, they will ignore it.