Constraints in SQL
Constraint is the rule applied on the data columns of a table.
Constraint is the rule applied on the data columns of a table. They are used to check the validity of input data, ensuring the accuracy, reliability and integrity of data in the database.
Common types of constraints
Here are some of the most common constraints used in SQL.
Constraint Meaning NOT NULL Make sure the data of the column is not NULL. DEFAULT Provides a default value for a column in case the column data is not entered or not specified. UNIQUE Ensuring that the data of the column is unique, all values in a column are different without duplication. PRIMARY Key Used to set the primary key on the table, the value of the column as the primary key must be unique, not duplicated. The declaration of the primary key constraint requires columns to NOT NULL. FOREIGN Key Used to set foreign keys on the table, referring to another table through the value of the linked column. The value of the linked column must be unique in the other table. CHECK Ensure all values in a column satisfy some conditions. INDEX Use to create and retrieve data from the database quickly.Constraints can be specified when you create a table with a CREATE TABLE statement or after creating a table with the ALTER TABLE statement.
Delete binding (Drop Constraint)
Any defined constraints can be deleted using the ALTER TABLE command with the DROP CONSTRAINT option.
For example, to drop Constraint primary key in NHANVIEN table, you can use the following command.
ALTER TABLE NHANVIEN DROP CONSTRAINT NHANVIEN_PK;
Some databases can provide shortcuts to drop the most binding in a shorter way. For example, to remove the primary key constraint for a table in Oracle, you can use the following command.
ALTER TABLE NHANVIEN DROP PRIMARY KEY;
In the next section, we will learn about the JOIN clause in SQL, remember to follow it.
Previous article: Sort results in SQL
Next lesson: JOIN clause in SQL
You should read it
- Check constraints in SQL Server
- Unique constraints in SQL Server
- ALTER TABLE statement in SQL
- Foreign Key with Set Null in SQL Server
- Relationships between tables in Access
- 1-1 relationship between tables in ACCESS
- Foreign Key foreign key in SQL Server
- Integrated terminal in SQL Operations Studio (preview)
- How to remove Microsoft Teams on Windows 11
- 1-n relationship between tables in Access
- Completely remove URL Mal Virus - http://107.170.47.181
- How to completely remove Coc Coc?
Maybe you are interested
How to mute any application on Windows 10 Cause the phone is constantly powered off and the way to fix it Infographic videos about the mobile messaging application battle in Vietnam New scanning technology brings 3D images of live insects Forensics use bacteria in the mouth to estimate the victim's death time Flipping the magic trick suspended above does not have magic