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

5 ★ | 1 Vote

May be interested

  • 10 tips to help you become a great Pokemon trainer10 tips to help you become a great Pokemon trainer
    in the article below, tipsmake.com will introduce to you some tips to easily become a great trainer in this pokemon go game offline ...
  • How to use GoChat application in Pokémon GOHow to use GoChat application in Pokémon GO
    pokémon go has become a very hot phenomenon since its debut. any information or tricks related to the game are read and applied by players during the process of catching pokémon. and the gochat chat app for pokémon go players brings space to capture pokémon much more interesting.
  • How to play Pokemon GO in Landscape Mode on the iPhoneHow to play Pokemon GO in Landscape Mode on the iPhone
    although players can play pokemon go in portrait mode. however, if you want to watch and play games on a large and eye-catching screen, players can switch to playing games in landscape mode.
  • The secret to controlling Pokemon Go employees at workThe secret to controlling Pokemon Go employees at work
    these days, hr managers are faced with an extremely painful problem that is the status of priority employees playing pokemon go more than work. this has caused a small impact on productivity and efficiency.
  • Check out the 'buffalo' Pokémon in Pokémon GoCheck out the 'buffalo' Pokémon in Pokémon Go
    each type of pokemon has hp, cp, ability to attack and endure differently. based on these indicators, players can determine as well as choosing the most powerful pokemon for their offensive tactics.
  • Sitting home can also locate Pokemon around, do you believe it?Sitting home can also locate Pokemon around, do you believe it?
    the tightening of the niantic developers' rules to prevent players from abusing the support tools also brings annoyance, such as those who have no conditions to move much, go away, it is hard to know. get the location of the pokemon around the area they live in
  • 5 undeniable benefits when playing Pokemon Go5 undeniable benefits when playing Pokemon Go
    get to know many new people, breathe fresh air, relieve stress, increase concentration thanks to going out for a walk .... are compelling reasons to force you to try pokemon go now .
  • Want to earn the fastest Pokécoins in Pokémon Go? So don't miss this article!Want to earn the fastest Pokécoins in Pokémon Go?  So don't miss this article!
    pokécoins in pokémon go play the role of buying items in the store. the more coins you earn, the more likely you are to buy more items. to earn pokécoins, players will have to complete certain tasks or buy real money.
  • Pokémon systems when fighting in Pokémon GoPokémon systems when fighting in Pokémon Go
    each pokémon system in pokémon go has different strengths, along with a specific weakness. this type of pokémon will have the power to attack the other pokémon, but can defeat the other pokémon. if you know the characteristics of each type, it will be easier to choose which pokémon to battle.
  • The terms you need to know when playing Pokémon GoThe terms you need to know when playing Pokémon Go
    pokémon go is the most prominent name in recent days. this game of capturing and training virtual animals has created a relatively new way of playing, as players have to constantly move to catch pokémon. during the process of joining pokémon go, you will encounter and use a lot of important terms. so what do they mean?