SQL Notes Chapter 3
SQL Notes Chapter 3
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)