Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

DBMS KEY CONSTRAINTS & JOINS

KEY CONSTRAINTS

• Keys are used to establish and identify relationship between tables also we can uniquely
identify records.
• Generally key constraints are created on column level or table level.
1. Column level – In this method we are defining constraints on individual column.
Ie. When we are creating column on the same time we are defining
Constraints
Ex. Create table test (S_No number(10) not null, Name varchar(10));
2. Table level - In this method we are defining constraints on group of column.
Ie. Here first we create all columns and then last we are defining
Constraints for group of column.
Ex. Create table test (S_No number(10), Name varchar(10)
unique (S_No, Name));
TYPES OF KEYS

• Not Null
• Unique Key
• Primary Key
• Foreign Key
1. NOT NULL

• It does not accept null values.


• It can accept duplicate data.
• We can use it on column level not table level.
• Ex. Create table test (S_No number(10) not null, Name varchar(10));
2. UNIQUE KEY

• It doesn't accept duplicate data.


• It can accept null values.
• Unique key supports column level as well as table level constraints.
• Ex. Column level
Create table test (S_No number(10) unique, Name varchar(10));
Table level
Create table test (S_No number(10), Name varchar(10) unique (S_No));
3. PRIMARY KEY

• Primary key helps to identify unique record in a table.


• It doesn't accept null values.
• It doesn't accept duplicate data.
• Unique key supports column level as well as table level constraints.
• Ex. Column level
Create table test (S_No number(10) primary key, Name varchar(10));
Table level
Create table test (S_No number(10), Name varchar(10) primary key(S_No, Name));
Composite primary key.
4. FOREIGN KEY

• When we want to establish a connection between two table then we use referential
integrity constraints which is nothing but foreign key.
• One tables foreign key must belongs to another tables primary key and these column
belongs to same data types.
• The values of foreign keys are based on the values of primary key.
• Foreign key can accept null as well as duplicate data whereas primary key not.
• Ex. Create table test (S_No number(10) references emp(S_No), Name varchar(10));
WHY JOINS ARE USED?

• Whenever we want to retrieve data from more than one tables in that case we are using
joins.
• In order to use joins there is condition that both tables can have common column with
same data type.
• Joins = Cross product X Condition.
TYPES OF JOINS

• Equi joins
• Natural joins
• Self joins
• Outer joins
- Left outer join
- Right outer join
- Full outer join
EQUI JOINS

• In this join based on equality operator we are retrieving data from more than one table.
Conditional column must belong to same data type.
• Whenever tables having have common column then only we can use equi join.

E_No E_Name E_Add Dept_No Dept_Add E_No


1 Ravi Delhi D1 Delhi 1
2 Ram Hyd D2 Chennai 2
3 Madhav Kop D3 Hyd 3
4 Amrit Pune
• Find the employee name who works in a department having same location as there add?

• Ans. Select E_Name from emp, dept where Emp.E_No=Dept.E_No and Emp.E_Add=Dept.Dept_Add;
NATURAL JOIN

• This join returns only matching rows from a table.


• In this join we are not allowed to use joining condition explicitly because database server
internally uses joins.
E_No E_Name E_Add Dept_No Dept_Add E_No
1 Ravi Delhi D1 Delhi 1
2 Ram Hyd D2 Chennai 2
3 Madhav Kop D3 Hyd 3
4 Amrit Pune

• Ex. Find the employee names who are working in departments?


• Ans. Select E_Name from emp natural join Dept;
SELF JOIN

• Joining table to itself Is called self join.


• We cannot use single table name for two times in a query so we use alias name.
Stud_id Course_id Since
S1 C1 2019
S2 C2 2020
S1 C2 2020

• Ex. Find student id who is enrolled in at least two courses?


• Ans. Select Stud_id from study as t1, study as t2 where t1.Stud_id = t2.Stud_id and
t1.Course_id <> t2.Course_id;
OUTER JOIN

• There are total three types of outer join


1.Left outer join.
2.Right outer join.
3.Full outer join.
LEFT OUTER JOIN

• In this join all rows from left table and matching rows from right table will be taken.
E_No E_Name E_Add Dept_No Dept_Add E_No
1 Ravi Delhi D1 Delhi 1
2 Ram Hyd D2 Chennai 2
3 Madhav Kop D3 Hyd 3
4 Amrit Pune

• Select E_No, E_Name, Dept_Name,Dept_loc from emp left outer join dept on
(emp.dept_no=dept.dept_no)

You might also like