Chapter - 9 MySQL Advanced 2
Chapter - 9 MySQL Advanced 2
9:
More On Database &
SQL
– Advanced Concepts
Informatics Practices
Class XII (CBSE Board)
Integrity Constraints
•One of the major responsibility of a DBMS is to maintain the
Integrity of the data i.e. Data being stored in the Database
must be correct and valid.
•An Integrity Constraints or Constraints are the rules, condition
or checks applicable to a column or table which ensures the
integrity or validity of data.
•The following constraints are commonly used in MySQL.
NOT NULL
PRIMARY KEY
Most of the constraints are applied with
UNIQUE * Column definition which are called
DEFAULT * Column-Level (in-line Constraints) ,but
CHECK * some of them may be applied at column
Level as well as Table-Level (Out-line
FOREIGN KEY * constraints) i.e. after defining all the
columns. Ex.- Primary Key & Foreign Key
EMPLOYEE Primary
DEPARTMENT
EmpID DeptNo key
Name DeptNam
Foreig
City e Head
n
Sal Location
Key
DeptN
Parent
oChild Table
Table
Implementing Foreign Key Cont.
. Paren
CREATE TABLE t
table
(Department
DeptNo char(2)NOT NULL PRIMARY KEY,
DeptNam char(10) NOT NULL,
e Head char(30) );
Child Table
CREATE TABLE Employee
which
in Foreign
( EmpNo char(3) NOT NULL PRIMARY KEY, key is defined.
Name char(30) NOT NULL,
City char(20),
Sal decimal(8,2), Parent table and
column to be
DeptNo char(2),
referenced..
FOREGIN KEY (DeptNo) REFERENCES Departmet (DeptNo));
mysql ALTEColumn
Changing TABL Student
Name ofDROP PRIMARY
Existing
> R E KEY;
Column
ALTER TABLE <Table Name>
CHANGE <Old name><New
Definition> mysql> ALTER TABLE
Student
CHANGE Name Stname
Char(40);
Viewing & Disabling Constraints
To View the Constraints
The following command will show all the details like
columns definitions and constraints of EMP table.
mysql> SHOW CREATE TABLE EMP;
Alternatively you can use DESCribe command:
mysql> DESC EMP;
R S T (Equi Join)
A B C C X Y
A B C C X Y
p q s s q r
p q s s q r
m n t t n m
m n t t n m
o p s o p s
o p s s p r
l m u
The Natural Join is much similar to Equi Join i.e. records are joined
on the equality condition of Joining Column except that the
common column appears one time.
Consider the following table R and S having C as Join column.
R S T (Natural Join)
A B C C X Y
A B C X Y
p q s s q r
p q s q r
m n t t n m
m n t n m
o p s o p s
o p s p r
l m u
Ex. Find out the name of Employees working in same city from where
they belongs (hometown) .