SQL Presentation1
SQL Presentation1
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
• 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.
• Ans. Select E_Name from emp, dept where Emp.E_No=Dept.E_No and Emp.E_Add=Dept.Dept_Add;
NATURAL 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)