10
MySQL Training By Manish Mittal OpenSource Technologies Pvt. Ltd. http:// www.opensourcetechnologies.co m

MySQL Training

Embed Size (px)

DESCRIPTION

This is a simple MySQL by Manish Mittal of OpenSource Technologies Pvt. Ltd. It contains Joins, Triggers, Stored procedures, and functions

Citation preview

Page 1: MySQL Training

http://www.opensourcetechnologies.com

MySQL Training

By Manish MittalOpenSource Technologies Pvt. Ltd.

Page 2: MySQL Training

http://www.opensourcetechnologies.com

Types of Joins• CROSS JOIN:-This type of join is the simplest join. The cross join result in cartesian

product of all the records from two tables. select * from employee cross join department

• INNER JOIN OR EQUI JOIN:-This is the type of join where tables are combined based on a common column.select * from employee , department where employee .deptid= department.depid;

• OUTER JOIN:- Join is used to combine all rows of one table with matching rows from the other table and also show unmatchable records from other table. It is used whenever multiple tables must be accessed through a SQL SELECT statementSELECT * FROM roseindia AS R -> LEFT JOIN newstrack AS N -> ON R.EmpId = N.EmpId

Page 3: MySQL Training

http://www.opensourcetechnologies.com

Triggers

• A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table. Some uses for triggers are to perform checks of values to be inserted into a table or to perform calculations on values involved in an update.

Page 4: MySQL Training

http://www.opensourcetechnologies.com

Trigger Example

• CREATE TRIGGER ins_sum BEFORE INSERT ON account -> FOR EACH ROW SET @sum = @sum + NEW.amount;

• create trigger bi_emps_fer before insert on emps for each row begin insert into audit (user_name, table_name, update_date) values (current_user(),’emps’,now()); end; //

Page 5: MySQL Training

http://www.opensourcetechnologies.com

Procedure Example

• delimiter // • CREATE PROCEDURE simpleproc (OUT

param1 INT) -> BEGIN -> SELECT COUNT(*) INTO param1 FROM t; ->

• END//• delimiter ; • CALL simpleproc(@a);

Page 6: MySQL Training

http://www.opensourcetechnologies.com

Function Example

• CREATE FUNCTION hello (s CHAR(20)) RETURNS CHAR(50) DETERMINISTIC -> RETURN CONCAT('Hello, ',s,'!');

• SELECT hello('world');

Page 7: MySQL Training

http://www.opensourcetechnologies.com

S.Pro Vs functions

• There is one main difference between functions and procedures.

A function must return a value and it can be only a single value. Any number of parameters can be passed in but only 1 value can be passed out. This value coming out must be done via the RETURN.

A Procedure doesn't have to return anything. But it can accept any number of parameters in and also any number of parameters out. There is no RETURN in a procedure.

Page 8: MySQL Training

http://www.opensourcetechnologies.com

Transaction

• The START TRANSACTION or BEGIN statement begins a new transaction. COMMIT commits the current transaction, making its changes permanent. ROLLBACK rolls back the current transaction, canceling its changes. The SET autocommit statement disables or enables the default autocommit mode for the current session.

Page 9: MySQL Training

http://www.opensourcetechnologies.com

Save points

• SAVEPOINT identifier • ROLLBACK TO [SAVEPOINT] identifier • RELEASE SAVEPOINT identifier

Page 10: MySQL Training

http://www.opensourcetechnologies.com

Cursors

• Cursor can be created inside the stored procedures, functions and triggers. Cursors are used for rows iteration returned by a query on a row-by-row basis. It is different from typical SQL commands that operate on all the rows in the set returned by a query at one time.