Menu Bar

SQL: Constraints

Chapter 8:
Constraints: Add all these constraints to your students and teachers table..
Create primary key for students and teachers tables
  1. Cell default value = null
  2. Not NULL
  3. Unique
  4. Check
  5. Primary key
  6. Foreign key
  7. Default
  8. Candidate Key
  9. Composite key
  10. Auto Increment


Null: By default all records are taken into Null values in database tables. A field with a Null value is a field with no value.

Not NULL constraint: Enforces that the column will not accept NULL values. The Not NULL constraints are used to enforce domain integrity, as the check constraints.



Unique constraint: It enforces the uniqueness of the values in a set of columns, so no duplicate values are entered. The unique key constraints are used to enforce entity integrity as the primary key constraints. Each table can have only one primary key. If there are multiple Unique identifiers for a multiple columns, such column pairs are often referred to as alternate keys or candidate keys. In practice, one of two columns is logically promoted to primary key using the Primary key constraint, and the other is usually declared by a Unique constraint. Internally, Primary key and Unique constraints are handled almost identically.





Check constraint: It is used to limit the values that can be placed in a column. The check constraints are used to enforce domain integrity. Check constraints allow us to define an expression for a table that must not evaluate to false for a data modification statement to succeed. Check constraints deal only with some logical expression for the specific row already being operated on, so no additional i/o required.




Describe table: To check whether the constraints applied to tables or not, describe the table by using this query.

exec sp_columns employee




Primary Key: It is a unique identification for a row within a database table. Every table should have a primary key constraint to uniquely identify each row and only one primary key constraint can be created for each table. The primary key constraints are used to enforce entity integrity.

[or]



Foreign Key: A foreign key in one table points to a primary key in another table. Foreign keys prevent actions that would leave rows with foreign key values when there are no primary keys with that value. The foreign key constraints are used to enforce referential integrity.



[ OR ]






Default constraint: The Default constraint provides a default value to a column when the insert statement does not provide a specific value.




Candidate key: A Candidate key can be any column or a combination of columns that can qualify as unique key in database. There can be multiple Candidate keys in one table. Each Candidate key can qualify as Primary key.


Composite key: When we create keys on more than one column then that key is known as composite key.



Auto-Increment: Auto-Increment allows a unique number to be generated automatically when a new record is inserted into a table.