InterviewSolution
| 1. |
Explain SQL constraints with example. |
|
Answer» SQL Constraints are rules used to limit the type of data that can be stored into a table, to maintain the accuracy and integrity of the data inside table.
Constraints are used to make sure that the integrity of data is maintained in the database. The NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT are the most used constraints that can be applied to a table. 1. NOT NULL Constraint: NOT NULL constraint restricts a column from having a NULL value. Once *NOT NULL* constraint is applied to a column, you cannot store null value to that column. It enforces a column to contain a proper value. 2. UNIQUE Constraint: UNIQUE constraint ensures that a field or column will only have unique values. A UNIQUE constraint field will not have duplicate data. 3. Primary Key Constraint: The primary key constraint uniquely identifies each record in a database. A Primary Key must contain unique value and it must not contain a null value. 4. Foreign Key Constraint: FOREIGN KEY is used to relate two tables. A FOREIGN KEY in one table points to a PRIMARY KEY in another table. ) 5. 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. |
|