0% found this document useful (0 votes)
168 views

SQL Constraints

Uploaded by

Vaishnavi patil
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
168 views

SQL Constraints

Uploaded by

Vaishnavi patil
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

NOT NULL

CONSTRAIN
T
• The NOT NULL constraint in MySQL serves as
a validation rule for the columns.
• In NOT NULL constraints the column cannot contain the
NULL values.
• This constraint guarantees that each row in the table
must have a value for that particular column.
Syntax
Following is the basic syntax of NOT NULL constraint while creating a table:

CREATE TABLE table_name


( column1 datatype NOT NULL,
column2 datatype, column3
datatype NOT NULL, ... );

In the above syntax, it can be seen that NOT NULL is used after the column name
and data type of that column in the column definition.
For Example :

Let's create a table named students.


CREATE TABLE students(
reg_no INT NOT NULL,
name VARCHAR(255) NOT
NULL, admission_date DATE );

In the table students, there are three columns reg_no,


name, and admission_date.
For reg_no and name, it is defined that none of the
values in that column can be NULL but the
admission_date column is not defined with NOT NULL
constraint, so it can contain NULL values.
NOT NULL constraint prevents
the insertion of the empty or missing
data into the column of that table.

Due to this restriction, data accuracy


and reliability increase, and the
database becomes more easier to
handle.
CHECK
CONSTRAINTS
CHECK
Constraint :
• A CHECK constraint in SQL is used to limit the values
that can be inserted into a column of a table.
• It ensures that the data to specific rules or conditions.
• Helps maintain data integrity and consistency.

Synta
x : CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT,
CONSTRAINT chk_Age CHECK (Age >
18));
PRIMARY KEY
Combination of unique and not null

Primary Key uniquely


identifies each record in
a table and also used to
establish a relationship
between table.
Unique identifiers, such as
driver's license number,
student registration
Points  It contains a unique
to value .
remem  It cannot be null.
 It doesn't accept any
ber:
duplicate value.
 One table can have
only one primary key.
Let's understand it practically:
The syntax for creating
Primary Key on Roll_no
attribute of STUDENT_DETAILS
table:
→ CREATE TABLE STUDENT_DETAIL
→ Roll_no int NOT NULL PRIMARY KEY,
→ Name varchar (10) NOT NULL,
→ Marks int NOT NULL;
FOREIGN KEY AND
DEFAULT IN SQL
FOREGIN KEY

 A FOREIGN KEY is a field (or collection of fields) in one table, that


refer to the PRIMARY KEY in another table.
 The table with the foreign key is called the child table, and the table
with primary key is called the reference or parent table.
 It will allow ‘NULL’ value.
 It will allow ‘DUPLICATE’ value.
SYNTAX

CREATE TABLE orders ( order_id INT PRIMARY KEY, order_date DATE,customer_id INT,
FOREIGN KEY (customer_id) REFERENCES customers(id));
EXAMPLE OF FOREIGN KEY
DEFAULT

 The default constraint is used to set a default value for a column.


 The default value will be added to all new records, if no other value is
specified.
 SYNTAX: CREATE TABLE table_name (column1 datatype
DEFAULT default_value, column2 datatype,...);
THANK YOU

You might also like