Constraints in SQL
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
Should I buy a tablet with 4G LTE/5G connectivity or just WiFi?
How to adjust line spacing in Word tables very easily
10 Useful Table Formatting Tips in Microsoft Word
What's notable about Free Fire OB34 Summon?
How to update Windows offline using Portable Update
Quickly fix Unmountable Boot Volume error on Windows 10/11