sql constraints

SQL constraints are used to specify rules for the data in a table. These are used to limit the type of data in a table. Constraints ensure the accuracy and reliability of the data in the database.

SQL constraints are

NOT NULL : It make sure that a column cannot store NULL value

e.g CREATE table CUSTOMER (customer_id int NOT NULL , Name varchar(60));

UNIQUE : It make sure that each row for a column must have unique value

e.g CREATE table CUSTOMER (customer_id int NOT NULL UNIQUE, Name varchar(60));

PRIMARY KEY : Primary key is one of the important constraints.It is make sure that each row of a column is unique and not null. Primary key uniquely identifies each record in a database.

e.g CREATE table CUSTOMER (customer_id int NOT NULL PRIMARY KEY, Name varchar(60));

FOREIGN KEY: Foreign key is used to relate two tables, A foreign key is a field in a table that matches another field(primary key) of another table.

e.g CREATE table Order_info(order_id int PRIMARY KEY,product_name varchar(60) NOT NULL,
c_id int FOREIGN KEY REFERENCES CUSTOMER (customer_id));

CHECK : CHECK is used to restrict the value of a column between a given range.

e.g CREATE table CUSTOMER (customer_id int NOT NULL CHECK(s_id > 100)
, Name varchar(60));

Leave a Reply

Your email address will not be published. Required fields are marked *