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

SQL Notes Chapter 3

Uploaded by

devap9407
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

SQL Notes Chapter 3

Uploaded by

devap9407
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CONSTRAINTS

Constraints are the additional validation that is given for a column.


Types of constraints
1. UNIQUE
2. NOT NULL
3. CHECK
4. PRIMARY KEY
5. FOREIGN KEY
STUDENT TABLE
SID SNAME BRANCH PHONE NO AGE TID
101 DINGA MECH 1234567890 18 T3
102 DINGI ECE 9876504321 22 T1
103 PENGA CSE 8765432109 21 T2

TEACHERS TABLE
TID TNAME SALARY
T1 SMITH 25000
T2 MILLER 30000
T3 ADAMS 40000

1.UNIQUE
UNIQUE is a constraint which is given for a column where the column should not
accept duplicate or repeated value.
Example: UNIQUE(SID), UNIQUE (PHONE NO)
2.NOT NULL
NOT NULL is a constraint which is given for a column where the column should not
accept null values.
NULL: NULL is an empty space or nothing.
Whenever we create a column in the table we have to assign either null constraint or
NOT NULL constraint.
“0” is not a NULL value.
Example: SID NOT NULL
SNAME NOT NULL
3.CHECK
Check is an extra validation which depends on the condition
If the condition is true then the value will be accepted and if the condition is false then
the value will be rejected.
Example: CHECK(LENGTH(NUMBER)=10)
CHECK(AGE>=18)
4.PRIMARY KEY
Primary key is a constraint which is used to identify the records uniquely from the
table.
Characteristics of PRIMARY KEY
1. Primary key cannot accept duplicate or repeated values.
2. Primary key cannot accept null values.
3. Primary key is a combination of UNIQUE and NOT NULL constraints.
4. In a table, we can have only one primary key.
5. Primary key is not mandatory but it is recommended to have one in a table.
Example: PRIMARY KEY(SID)
PRIMARY KEY(TID)
5.FOREIGN KEY
Foreign key is used to establish the connection between 2 tables. Foreign key is also
known as REFERENTIAL INTEGRITY CONSTRAINT.
Characteristics of FOREIGN KEY
1. Foreign key can accept duplicate or repeated values.
2. Foreign key can accept null values.
3. Foreign key is not a combination of unique and not null constraints.
4. In a table, we can have any number of foreign keys.
5. To be a foreign key it should be primary key in its own table.
6. Foreign key present in child table, but it actually belongs to parent table.
Example: FOREIGN KEY(TID)

You might also like