Class Xii Cs
Class Xii Cs
Class Xii Cs
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