Constraint is the set of ristriction upon table column like not be null data set and not be duplicate contion to checks etc.
Primary key is master key that make table to MASTER TABLE. If any field have primary so it mean that value not be set NULL and DUPLICATE.
mysql> CREATE Table t1( -> id int primary key -> ); Query OK, 0 rows affected (0.06 sec) mysql> CREATE Table t2( -> id int, -> Constraint pk Primary Key(id) -> ); Query OK, 0 rows affected (0.13 sec) mysql> ALTER Table t3 ADD Constraint pk Primary key(primary_column); Query OK, 0 rows affected (0.13 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql>
Foreign key is set table as Secondary Table it exmple like mark table of student. It is check column data always insert that present in that primary field.
mysql> CREATE TABLE t4( -> id int references t1(id) -> ); Query OK, 0 rows affected (0.05 sec) mysql> CREATE TABLE t5( -> id int, -> Constraint fk Foreign key(id) references t2(id) -> ); Query OK, 0 rows affected (0.07 sec) mysql>
The unique constraint is set column have not be store duplicate data. if you can select unique data from table you can use distinct.
mysql> CREATE Table rollno( -> rollno int unique -> ); Query OK, 0 rows affected (0.07 sec) mysql> ALTER Table bill ADD Column bill_id int UNIQUE; Query OK, 0 rows affected (0.17 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql>
The NOT NULL Constraint is set column data have be not null if you can entity new row. With deal to not null you can use is Operator.
mysql> CREATE Table personal( -> fname char(30) NOT NULL -> ); Query OK, 0 rows affected (0.07 sec) mysql> ALTER Table personal ADD Column lname char(30) NOT NULL; Query OK, 0 rows affected (0.17 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql>
The NULL Constraint is set you can insert it data is null (void). With deal to null you can use is Operator.
mysql> CREATE Table personal( -> fname char(30) NULL -> ); Query OK, 0 rows affected (0.07 sec) mysql> ALTER Table personal ADD Column lname char(30) NULL; Query OK, 0 rows affected (0.17 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql>
The Check Constraint is used for set condition inside the column.
mysql> CREATE Table sex( -> gender char(1) CHECK(in('T','M','F')) -> ); Query OK, 0 rows affected (0.07 sec) mysql> ALTER Table personal ADD Column m1 int CHECK(m1 > 100); Query OK, 0 rows affected (0.17 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql>
The default Constraint is use to set default value of the column like if user not enter last_name it set default NULL.
mysql> CREATE Table departement( -> student_Class char('40') DEFAULT 'BCA' -> ); Query OK, 0 rows affected (0.07 sec) mysql> ALTER Table personal ADD Column status char(30) default 'bachlor'; Query OK, 0 rows affected (0.17 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql>