0% found this document useful (0 votes)
54 views

Loading Data Into A Table: Joining Tables

The document provides instructions and examples for loading data into MySQL tables, creating and manipulating tables using foreign keys and joins, creating views and stored procedures, and using other advanced MySQL features like triggers and cursors. It covers topics like data import, database relationships, querying data, and procedural logic within the database. Various code examples demonstrate how to implement these MySQL features through SQL statements.

Uploaded by

arun
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Loading Data Into A Table: Joining Tables

The document provides instructions and examples for loading data into MySQL tables, creating and manipulating tables using foreign keys and joins, creating views and stored procedures, and using other advanced MySQL features like triggers and cursors. It covers topics like data import, database relationships, querying data, and procedural logic within the database. Various code examples demonstrate how to implement these MySQL features through SQL statements.

Uploaded by

arun
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 8

Loading data into a table

STEP 1: type the following in a notepad and save it.

EMP001 JACK SALESMAN EMP004


EMP002 PRIYA RECEPTIONIST EMP005
EMP003 MACK EXECUTIVE EMP004
EMP004 ASAMA EXECUTIVE EMP005
EMP005 JOHN MANAGER EMP008

STEP 2: create a table emp with suitable data types.


STEP3: To load the text file ‘emp.txt’ into the emp table

Mysql> LOAD DATA LOCAL INFILE “C:/EMP.TXT” INTO TABLE EMP;

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

Using foreign keys and referential integrity in MySQL

mysql> CREATE TABLE species (id TINYINT NOT NULL AUTO_INCREMENT,

name VARCHAR(50) NOT NULL, PRIMARY


KEY(id)) ENGINE=INNODB;

mysql> INSERT INTO species VALUES (1,'orangutan'),(2,


'elephant'),(3,'hippopotamus'),(4,'yak');

mysql> CREATE TABLE zoo ( id INT(4) NOT NULL,

name VARCHAR(50) NOT NULL,

FK_species TINYINT(4) NOT NULL,

INDEX (FK_species),

FOREIGN KEY (FK_species) REFERENCES


species (id), PRIMARY KEY(id)) ENGINE=INNODB;

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

Joining tables

Types of Join

INNER (/ EQUI JOIN)

OUTER (LEFT, RIGHT)


CROSS

Inner Join OR CROSS


SELECT t1.name, t2.salary FROM employee t1 INNER JOIN info t2 ON t1.name = t2.name;

Outer Join (LEFT)


SELECT * FROM table1 LEFT JOIN table2 ON table1.id=table2.id;
SELECT * FROM table1 LEFT JOIN table2 ON table1.id=table2.id LEFT JOIN table3 ON
table2.id=table3.id;

Outer Join (RIGHT)


SELECT * FROM table1 RIGHT JOIN table2 ON table1.id=table2.id;

Natural Join (LEFT)


SELECT * FROM t1 NATURAL LEFT JOIN t2;

Natural Join (LEFT)


SELECT * FROM t1 NATURAL RIGHT JOIN t2;

SELF JOIN
SELECT a.label, b.label from menu a, menu b where a.id=b.parent;
(id, label, parent); 3 fields....

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

Views in Mysql
create view myview1 as select rollno "Rollno" ,name "Name" from stud_details;

delete
update
insert (null) .....

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

Integrity Constraints
Foreign Key Constraint defined with on delete cascade

Create table employeemaster( empid varchar(6),


name varchar(25),
primary key(empid))engine=innodb;

Create table transactionmaster(transid varchar(6),


cusid varchar(6),
tranemployee varchar(6),
transactiondate date,
amt dec(7,2),
primary key(transid),
foreign key(tranemployee) references
employeemaster(empid) on delete cascade)engine=innodb;

Foreign Key Constraint defined with on delete set null

alter table transactionmaster drop foreign key


transactionmaster_ibfk_1;
alter table transactionmaster add foreign key(tranemployee)
references employeemaster(empid) on delete set null;

Foreign Key Constraint defined with on update cascade

alter table transactionmaster drop foreign key


transactionmaster_ibfk_1;
alter table transactionmaster add foreign key(tranemployee)
references employeemaster(empid) on update cascade;

Foreign Key Constraint defined with on update set null

alter table transactionmaster drop foreign key


transactionmaster_ibfk_1;
alter table transactionmaster add foreign key(tranemployee)
references employeemaster(empid) on update set null;

==================================================++++++++++++
+=============================

GROUPING THE DATA RETRIEVED

GROUP BY Clause

select name from employeemaster group by(name); (select only distinct


names..)

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

Stored Procedures & Functions


DELIMITER $$

DROP PROCEDURE IF EXISTS `test`.`dis_price` $$


CREATE PROCEDURE `test`.`disprice` (normal_price numeric(8,2),out discount_price
numeric(8,2))
BEGIN
case
when(normal_price>500) then
set discount_price=normal_price*.8;
when (normal_price>100) then
set discount_price=normal_price*.9;
else
set discount_price=normal_price;
end case;
END $$

DELIMITER $$
---------------------------------------------------------------------------------------------------------------------
---------------

DROP FUNCTION IF EXISTS `aa`.`isodd` $$


CREATE FUNCTION `aa`.`isodd` (a int) RETURNS INT
BEGIN
declare b int;
if mod(a,2)=0 then
set b=false;
else
set b=true;
end if;
return(b);
END $$

DELIMITER ;

...............................................................................................................................

DELIMITER $$
DROP PROCEDURE IF EXISTS `inventory`.`myproc` $$
CREATE PROCEDURE `inventory`.`myproc` ()
BEGIN
declare c int;
set c= myfun();
select c;
END $$

DELIMITER ;

DELIMITER $$

DROP FUNCTION IF EXISTS `inventory`.`myfun` $$


CREATE FUNCTION `inventory`.`myfun` () RETURNS INT
BEGIN
declare c int;
select COUNT(id) into c from sales;
return c;
END $$

DELIMITER ;

................................................................................................................

DELIMITER $$

DROP PROCEDURE IF EXISTS `studentdb`.`ReadRecords` $$


CREATE PROCEDURE `studentdb`.`ReadRecords` (v_rollno int,v_name
char(10),v_m1 int,v_m2 int,v_m3 int)
BEGIN
DECLARE tot int;
DECLARE average float;
set tot=v_m1+v_m2+v_m3;
set average=(v_m1+v_m2+v_m3)/3;

if(v_m1 >= 50 AND v_m2 >=50 AND v_m3 >=50 ) then


insert into student values(v_rollno,v_name,v_m1,v_m2,v_m3,tot,average,'p');
insert into passed values(v_rollno,v_name);
else

insert into student values(v_rollno,v_name,v_m1,v_m2,v_m3,tot,average,'f');


insert into failed values(v_rollno,v_name);
end if;
END $$

DELIMITER ;

............................................................................................................................................
................................

DELIMITER $$

DROP PROCEDURE IF EXISTS `new`.`rep2` $$


CREATE PROCEDURE `new1`.`rep2` ()
BEGIN
declare i int;
set i=0;
loop1: while i<=10 do

if mod(i,2)<>0 then
insert into new2 values(i);
end if;
set i=i+1;
end while loop1;
select * from new2;
END $$

DELIMITER ;

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

Triggers
delimiter %%
create trigger mytrig before insert on sails for each row
begin
declare id1 int;
declare qnty1 int;
set id1 = new.id;
set qnty1 = new.qnty;
update stock set qnty=qnty-qnty1 where id=id1;
end %%
delimiter;

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

CURSORS
DELIMITER $$

DROP PROCEDURE IF EXISTS `compute` $$


CREATE DEFINER=`root`@`localhost` PROCEDURE `compute`()
BEGIN
DECLARE done int DEFAULT 0;
DECLARE v_order_no int;
DECLARE total int;

DECLARE ORDERS_CURSOR CURSOR FOR SELECT order_no FROM orders;

declare continue handler for not found set done=1;


OPEN ORDERS_CURSOR;

eloop:loop
fetch ORDERS_CURSOR into v_order_no;
select sum(qnty_ordered*prize) from orderdetails where order_no=v_order_no into
total;

create table if not exists ORDERS_RESULTS (order_no int,total int);


insert into ORDERS_RESULTS values(v_order_no,total);

if done=1 then
leave eloop;
end if;

end loop eloop;

CLOSE ORDERS_CURSOR;
select * from ORDERS_RESULTS;
END $$

DELIMITER;

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

You might also like