What is constraints?
- A set of rules are defined to enforce the database integrity, is called as Constraints.
- The main purpose of constraint is to permit or prohibit the values in the columns.
- The above constraints are associated only with the tables.
- These constraints are applied to the table at the time of table creation.
Following are the Types of Constraint,
1. Primary Key
2. Foreign Key
3. Unique Key
4. Check Constraint
5. Not Null
| Primary Key |
For Example: Empid INT PRIMARY KEY, Syntax: PRIMARY KEY (column1, column2); |
| Foreign Key |
For example: Empid INT PRIMARY KEY, FOREIGN KEY (Empid) references Department (Empid); |
| Unique Key |
Note: Important thing to understand about the primary key and unique key, is that a table can have many unique constraints, but only one primary key can have per table. |
| Check Constraint |
For example: Age INT NOT NULL CHECK (Age >= 19) |
Null
- Null is a reserved keyword in SQL that indicates a data value does not exist in the database.
- It identifies the Null special marker.
- Null introduced by E. F. Codd, Creator of the relational database model.
- A Null value indicates that the value is unknown, not applicable and has no value, but it does not mean that it has a zero value or a field which contains spaces.
- It is used to represent a missing value.
- In the above example, NOT NULL indicates that the column should accept explicit value of the given data type. There are two columns 'address' and 'phone_no' which are not assigned with the NOT NULL, which means that these columns could be NULL.
- Null is not a data value, but it is a marker of an absent value.
- It can cause problems when selecting data, because when comparing an unknown value to any other value, the result is always unknown and not included in the final results.
- IS NULL or IS NOT NULL is used to check for a NULL value.
- All aggregate function (min, max, sum, count) ignores the NULL values except count() function.
For example:
CREATE TABLE employee(empid INT NOT NULL,
ename CHAR NOT NULL,
age INT NOT NULL,
city CHAR(25),
phone_no VARCHAR(20),
PRIMARY KEY (empid));
Example of IS NOT NULL:
<employee> Table
| empid | ename | age | city | phone_no |
|---|---|---|---|---|
| 001 | ABC | 28 | Pune | 12345678 |
| 002 | XYZ | 30 | Mumbai | 54687901 |
| 003 | PQR | 35 | Kolhapur |
SELECT empid, ename, age, city, phone_no
FROM employee
WHERE phone_no IS NOT NULL;
Output:
| empid | ename | age | city | phone_no |
|---|---|---|---|---|
| 001 | ABC | 28 | Pune | 12345678 |
| 002 | XYZ | 30 | Mumbai | 54687901 |
Example of IS NULL:
SELECT empid, ename, age, city, phone_no
FROM employee
WHERE phone_no IS NULL;
Output:
| empid | ename | age | city | phone_no |
|---|---|---|---|---|
| 003 | PQR | 35 | Kolhapur |
When not to use NULL?
- NULL is not specify explicitly in SELECT statement. For eg. SELECT NULL.
- NULL is not specify explicitly as an operand of scalar expression. For example, X + NULL, it is illegal.
- NULL is not specify explicitly as an operand of a conditional expression. For example, WHERE a = NULL, it is illegal.


