SQL constraints

Types of Constraint in SQL

 

Unique constraint

This constant and shows that NO2 Rows have the same value in the specified column (s).
For example, unique constraint applied on an code of employee table and shows that no rose have the same and code value as shown below.

Create table employee
( ecode integer NOT NULL UNIQUE,
  ename char(20) NOT NULL,
  Sex char(1) NOT NULL,
  grade char(2),
  gross decimal ) ;

PRIMARY KEY CONSTRAINT

This constant declares a column as the primary key of the table.this constraint is similar to the unique constraint accept that only one column (aur one group of columns) can be applied in this constraint.
The primary key cannot allow null values does this constraint must be applied to columns declared as not null considered for the following SQL statement :

CREATE TABLE employee
( ecode integer NOT NULL Primary key,
  ename char(20) NOT NULL,
  Sex char(1) NOT NULL,
  grade char(2),
  gross decimal ) ;

Default constraint

 Default value can be specified for a column using the default clause. When a user does not enter a value for a column (Having default value),automatically the define default value is inserted in the field.
Consider the following SQL statement:
CREATE TABLE employee
( ecode integer NOT NULL Primary key,
  ename char(20) NOT NULL,
  Sex char(1) NOT NULL,
  grade char(2) Default='E1',
  gross decimal ) ;

According to the above command, if no value is provided for grade, the default value E1 will be entered. The data type of the default value has to be compatible with the data type of the column to which it is assigned. A column can have only one default value.

Check Constraint

 This constant limits values that can be inserted into a column of a table. For instance, consider the following SQL statement :

CREATE TABLE employee
( ecode integer NOT NULL Primary key,
  ename char(20) NOT NULL,
  Sex char(1) NOT NULL,
  grade char(2),
  gross decimal check (gross>2000)
) ;

This statement ensures that the value inserted for gross must be greater than 2000. When a check constraint involves more than one column form the same table, it is specified after all the columns have been defined.

Comments