013. Table Creation & Manipulation Commands
013. Table Creation & Manipulation Commands
&
DATA MANIPULATION
COMMANDS
A constraints refers to condition or limitation
we apply on any column so that only correct
information will be entered in table.
MySQL allows to apply constraint by two
methods
At the time of table creation
After table creation
PRIMARY KEY : ensures unique value in any column also forces
data entry mandatory. Only one primary key can be applied in one
table
UNIQUE : also allows unique value in any column but it allows
NULL values and can be applied to n times
NOT NULL : it will make data entry mandatory for applied
column i.e. NULL will not be allowed
DEFAULT : it allows to specify any value which will be
automatically inserted in applied column if we not specify
applied column at the time of data entry using INSERT
CHECK : allows to specify range of values that can be entered in
applied column like salary must be greater than 2000, marks must
be greater than 0 or dept must be in given list of values etc.
Note: in mysql the database engine will ignore the
Check constraints.
Default value
‘Marketing’ is
automatically
inserted
Create another table to store training details of
employee as-
mysql> create table training(empno int,
trainingname varchar(20),startdate date,
enddate date, constraint myfkey foreign
key(empno) references ABCLtd(empno));
Error, empno 3
not in ABCLtd