Class Xii Cs

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

 DBMS CONSTRAINTS

BY- HENA FATMA


PGT CS
Database Constraints
Constraints are used to specify rules
for the data in a table.

 It enforces limits to the data or type


of data that can be
inserted/updated/deleted from a table.
Purpose

 The main purpose of constraints


is to maintain the data
integrity during database
operations (update/delete/insert)
into a table.

 The term data integrity simply


means that the data stored in the
table is valid.
Levels of Constraints
 There are two levels where a
constraint can be defined:
1. Column Level- A column level
constraint references a single
column and is defined along with
the definition of the column.
2. Table Level- A table level
constraint references one or
more columns and is defined
separately from the definitions
of the columns. Normally, it is
Types of constraints

 NOT NULL
 UNIQUE
 CHECK
 DEFAULT
 Key Constraints – PRIMARY KEY
NOT NULL Constraint
NOT NULL constraint makes sure that a column
does not hold NULL value. Normally, when we
don’t provide value for a particular column
while inserting a record into a table, it takes
NULL value by default. But, by specifying NOT
NULL constraint, we can be sure that a
particular column(s) cannot have NULL values.
Ex:
CREATE TABLE STUDENT
(
ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL,
STU_AGE INT NOT NULL,
STU_ADDRESS VARCHAR (235),
PRIMARY KEY (ROLL_NO)
);
UNIQUE Constraint
UNIQUE Constraint enforces a column or set
of columns to have unique values. If a
column has a UNIQUE constraint, it means
that particular column cannot have duplicate
values in a table.
Ex:
CREATE TABLE STUDENT
(
ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL UNIQUE,
STU_AGE INT NOT NULL,
STU_ADDRESS VARCHAR (35) UNIQUE,
PRIMARY KEY (ROLL_NO)
);
CHECK Constraint
CHECK constraint is used to restrict the
value of a column between a range. It
performs check on the values, before storing
them into the database. Its like condition
checking before saving data into a column.
Ex:
CREATE TABLE STUDENT
( ROLL_NO INT NOT NULL CHECK(ROLL_NO >1000) ,
STU_NAME VARCHAR (35) NOT NULL,
STU_AGE INT NOT NULL,
EXAM_FEE INT DEFAULT 10000,
STU_ADDRESS VARCHAR (35)
PRIMARY KEY (ROLL_NO);
DEFAULT Constraint
The DEFAULT constraint provides a
default value to a column when there
is no value provided while inserting a
record into a table.
Ex:
CREATE TABLE STUDENT
(
ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL,
STU_AGE INT NOT NULL,
EXAM_FEE INT DEFAULT 10000,
STU_ADDRESS VARCHAR (35) ,
PRIMARY KEY (ROLL_NO)
);
Key Constraints

1. PRIMARY KEY:
 Uniquely identifies each record in a table. It must
have unique values and cannot contain nulls.
 In the below example the ROLL_NO field is
marked as primary key, that means the ROLL_NO
field cannot have duplicate and null values.
Ex:
CREATE TABLE STUDENT
(
ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL UNIQUE,
STU_AGE INT NOT NULL,
STU_ADDRESS VARCHAR (35) UNIQUE,
PRIMARY KEY (ROLL_NO)
);
constraints ensures that a
column cannot have NULL
values?
a) UNIQUE b) NOT
NULL
c) PRIMARY KEY d) DEFAULT
Which of these constraints
automatically enforces both
uniqueness and NOT NULL conditions?
a) UNIQUE b) PRIMARY
KEY
c) FOREIGN KEY d) CHECK
You are a think tank!!! Answer
the following:
1. You have a table Products with a Price
column. Write a SQL statement to
ensure that the price cannot be less
than 0.
2. A table Orders contains the columns
OrderID, CustomerID, and OrderDate.
Which constraints would you apply to
ensure:
 OrderID is unique and cannot be NULL.
 OrderDate always has a valid date by
default as today's date.
THANK YOU

You might also like