SQL constraints

‌Constraints
Constraints are used to apply conditions or restrictions on Database objects like the tables

Different types of constraints are:
1.Null.
2.Not Null.
3.Primary key constraint.
4.Unique key constraint.
5.Foreign key Constraint.
6.Checked Constraints.
7.Composite primary key Constraint.
8.Default Constraints.
9.Unique +Not Null constraint.

‌Null constraint
If we apply null constraint for a column then column will allow null values.By default every column will allow null values.
‌Syntax to apply null constraint at the time of creating table.
Create table emp(eno int null,ename vacharu(30)).
‌Not Null Constraint
If will not allow null values.
‌Syntax
Create table tablename(colname datatype not null).
Ex:create table e1(eno int not null,ename vacharu (50),salary money).
‌syntax to apply the not null constraint after creating the table
alter table tablename alter column column name datatype not null.
‌Primary key.
Primary key constraint will not allow duplicate values and null values.w e can apply only one primary key Constraint on a table. Primary key Constraint will have constraint name .

‌Q,why primary key will not allow null values?
Whenever we apply primary key constraint on a column then automatically not null constraint will be added to that column and that not null constraint will not allow null values.
‌what is the purpose of constraint name?
The purpose of constraint name is to identify the Constraint.if we want to drop the constraint we use constraint name.
‌synatx to apply primary key constraint at the timeof creating a table.
create table tablename(colname datayp2 primary key).
Ex: create table e2 (eno int primary key ,ename vacharu(50).
‌syntax 2
Create table tablename (colname datatype constraint Constraintname primary key).
create table e3(eno int constraint pk_e3_eno primary key,ename vacharu(50)).
‌Syntax to apply primary key constraint after creating the table.
create table e4(eno int ename vaechar (50)
‌.1,alter table e4 add primary key(eno).
Here:error will be generated because of table column eno allows null values .so first make it as not null type.
alter table e4 alter column eno int not null

‌Can we apply primary key of nullable column.
‌No.

‌.2, syntax 2
Alter table tablename add constraint Constraintname primary key(colname).

‌syntax to drop primary key ..
‌alter table tablename drop constraint Constraintname.
‌Ex:alter table e3 drop constraint pk_e3_eno

‌Unique key constraint.
Unique key constraint will not allow duplicate values.
unique key constraint will allow null values. Unique constraint will not allow two null value ,that is it will allow only one null value..
We can apply more than one unique constraint on a single table .
‌synatx to apply unique key at the timeof table creation.
‌create table tablename (colname datatype unique).
‌create table e5(eno int unique,ename vaechar(50).
‌syntax to apply unique key after table creation .
‌alter table tablename add unique (colname)

Leave a comment