0% found this document useful (0 votes)
8 views6 pages

SQL Day2

Uploaded by

b.nagnath194
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

SQL Day2

Uploaded by

b.nagnath194
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

=============================

joins

create table emp


(emp_id number,
emo_name varchar(20),
emp_age number,
emp_address varchar(100),
primary key ( emp_id)
)

create table projects


(emp_id number,
project_id number ,
client_id number ,
project_name varchar(100),
start_date date,
)

alter session set nls_date_format='YYYY-MM-DD'


insert into projects ( emp_id,project_id,client_id,project_name,start_date) values
( 10 , 1000, 500 , 'IBM','2018-5-12');
insert into projects ( emp_id,project_id,client_id,project_name,start_date) values
( 11 , 2000, 501 , 'SUV','2019-5-12');
insert into projects ( emp_id,project_id,client_id,project_name,start_date) values
( 12 , 3000, 502 , 'IBM','2019-7-12');
insert into projects ( emp_id,project_id,client_id,project_name,start_date) values
( 10, 4000, 503 , 'TCS','2021-8-15');
insert into projects ( emp_id,project_id,client_id,project_name,start_date) values
( 14 , 5000, 500 , 'Accenture','2018-5-12');
insert into projects ( emp_id,project_id,client_id,project_name,start_date) values
( 15 , 6000, 500 , 'Wallmart','2018-5-12');
insert into projects ( emp_id,project_id,client_id,project_name,start_date) values
( 20 , 8000, 500 , 'Wallmart','2018-5-12');
commit;

select * from emp;


select * from projects;

select e.emp_id , e.emo_name , e.emp_address , p.project_id , p.project_name


from emp e
INNER JOIN projects p
on e.emp_id=p.emp_id;

select e.emp_id , e.emo_name , e.emp_address , p.project_id , p.project_name


from emp e
LEFT JOIN projects p
on e.emp_id=p.emp_id;
select e.emp_id , e.emo_name , e.emp_address , p.project_id , p.project_name
from emp e
RIGHT JOIN projects p
on e.emp_id=p.emp_id;

select e.emp_id , e.emo_name , e.emp_address , p.project_id , p.project_name


from emp e
FULL JOIN projects p
on e.emp_id=p.emp_id;

----

select e.emp_id , e.emo_name , e.emp_address , p.project_id , p.project_name


from emp e
LEFT JOIN projects p
on e.emp_id=p.emp_id
UNION
select e.emp_id , e.emo_name , e.emp_address , p.project_id , p.project_name
from emp e
RIGHT JOIN projects p
on e.emp_id=p.emp_id;

-----------

Natural join

select *
from emp e
INNER JOIN projects p
on e.emp_id=p.emp_id;

select *
from emp e
NATURAL JOIN projects p

-----

differnece between inner join and

create table studnet


(roll_no number,
student_name varchar (20)
)

create table marks


(
roll_no number,
marks varchar (20)
)
insert into studnet ( roll_no , student_name ) values ( 1, 'A');
insert into studnet ( roll_no , student_name ) values ( 2, 'B');
insert into studnet ( roll_no , student_name ) values ( 3, 'C');
commit;

insert into marks( roll_no , marks ) values ( 2, 70);


insert into marks( roll_no , marks ) values ( 3, 50);
insert into marks( roll_no , marks ) values ( 4, 85);
commit;

insert into student ( roll_no , name ) values ( 1, 'A');


insert into student ( roll_no , name ) values ( 2, 'B');
insert into student ( roll_no , name ) values ( 3, 'C');

insert into marks( roll_no , marks ) values ( 2, 70);


insert into marks( roll_no , marks ) values ( 3, 50);
insert into marks( roll_no , marks ) values ( 4, 85);

select * from studnet;


select * from marks;

select *
from studnet s
INNER JOIN marks m
on e.emp_id=p.emp_id;

select *
from emp e
NATURAL JOIN projects p

------------

Has join

create table emp


(emp_id number,
emo_name varchar(20),
emp_age number,
emp_address varchar(100),
primary key ( emp_id)
)

create table projects


(emp_id number,
project_id number ,
client_id number ,
project_name varchar(100),
start_date date,
)

select /* +use_hash( a b) */ a.emp_id,b.project_id,b.project_name from emp1


a,projects1 b where a.emp_id=b.emp_id;

============================

Self JOin

SELECT a.ROLL_NO , b.student_name


FROM studnet a, studnetb
WHERE a.ROLL_NO < b.ROLL_NO;

========================================================

VIEW:

select * from projects;

create view project_vew as select EMP_ID , PROJECT_NAME from projects;

select * from project_vew ;

insert into project_vew ( emp_id, project_name) values (40,'XYZ');


commit

select * from project_vew ;

update project_vew set project_name='AAA' where emp_id=40;


commit

select * from project_vew ;

delete from project_vew where emp_id=40;


commit;

Update the view , updates the base tables as well.

Updating a View
A view can be updated under certain conditions which are given below −

The SELECT clause may not contain the keyword DISTINCT.

The SELECT clause may not contain summary functions.

The SELECT clause may not contain set functions.

The SELECT clause may not contain set operators.

The SELECT clause may not contain an ORDER BY clause.


The FROM clause may not contain multiple tables.

The WHERE clause may not contain subqueries.

The query may not contain GROUP BY or HAVING.

Calculated columns may not be updated.

All NOT NULL columns from the base table must be included in the view in order for
the INSERT query to function.

create view project_vew1 as select distinct EMP_ID from projects;


select * from project_vew1
update project_vew1 set EMP_ID=40 where emp_id=20;

===================================================================================
=============

Triggers

CREATE TABLE employees(


Emp_id NUMBER,
salary NUMBER
);

insert into employees ( Emp_id , salary) values (100, 10000);


insert into employees ( Emp_id , salary) values (200, 20000);
commit;

DROP TABLE Emp_log;


CREATE TABLE Emp_log (
Emp_id NUMBER,
Log_date DATE,
New_salary NUMBER,
Action VARCHAR2(20));

CREATE OR REPLACE TRIGGER log_salary_increase


AFTER UPDATE OF salary ON employees
FOR EACH ROW
BEGIN
INSERT INTO Emp_log (Emp_id, Log_date, New_salary, Action)
VALUES (:NEW.Emp_id, SYSDATE, :NEW.salary, 'New Salary');
END;
/

UPDATE employees
SET salary = salary + 1000.0
commit;

SELECT * FROM Emp_log;


===================================================================================
=============

Stored Procedure

CREATE OR REPLACE PROCEDURE welcome_msg


(para1_name IN VARCHAR2)
IS
BEGIN
dbms_output.put_line ('Hello World! '|| para1_name);
END;
/

EXEC welcome_msg ('Welcome!');

You might also like