SlideShare a Scribd company logo
Getting Started With
MySQL II
Database Views
2
A database view is a virtual table or logical table which is defined as a SQL
SELECT query with joins.
Most database management systems, including MySQL, allow you to update data
in the underlying tables through the database view with some prerequisites.
Advantages of database view:
• A database view allows you to simplify complex queries: a database view is
defined by an SQL statement that associates with many underlying tables.
• A database view helps limit data access to specific users.
• A database view provides extra security layer.
• A database view enables computed columns.
Database Views
3
Disadvantages of database view:
• Performance: querying data from a database view can be slow especially if the
view is created based on other views.
• Tables dependency: you create a view based on underlying tables of the
database. Whenever you change the structure of those tables that view
associated with, you have to change the view as well.
Database Views – CREATE VIEW
4
Syntax:
CREATE
VIEW [database_name].[view_name]
AS
[SELECT statement]
There are some rules that the SELECT statement must follow:
• The SELECT statement can contain a subquery in WHERE clause but not in
the FROM clause.
• The SELECT statement cannot refer to any variables including local
variables, user variables, and session variables.
Database Views – CREATE OR REPLACE
VIEW
5
You can use CREATE OR REPLACE VIEW statement to either create or replace
an existing view. If a view already exists, MySQL simply modifies the view. In
case the view does not exist, MySQL creates a new view.
Syntax:
CREATE OR REPLACE
VIEW [database_name].[view_name]
AS
[SELECT statement]
Database Views – CREATE VIEW
6
Execute the example below in World database.
Database Views – CREATE OR REPLACE
VIEW
7
Execute the example below in World database.
Stored Procedures
8
Introduction to MySQL Stored Procedures
A stored procedure is a segment of declarative SQL statements stored inside
the database catalog.
A stored procedure can be invoked by triggers, other stored procedures, and
applications such as Java, Python,R, PHP, ERP etc.
Stored Procedures
9
MySQL stored procedures advantages:
• Typically stored procedures help increase the performance of the
applications. Once created, stored procedures are compiled and stored in
the database.
• Stored procedures help reduce the traffic between application and
database server because instead of sending multiple lengthy SQL
statements, the application has to send only name and parameters of the
stored procedure.
• Stored procedures are reusable and transparent to any applications.
• Stored procedures are secure.
Stored Procedures
10
MySQL stored procedures disadvantages
• If you use a lot of stored procedures, the memory usage of every
connection that is using those stored procedures will increase
substantially.
• Constructs of stored procedures make it more difficult to develop stored
procedures that have complicated business logic.
• It is difficult to debug stored procedures. Only a few database
management systems allow you to debug stored procedures.
Unfortunately, MySQL does not provide facilities for debugging stored
procedures.
• It is not easy to develop and maintain stored procedures.
Stored Procedures
11
Steps:
1 Right Click on Stored Procedure – Create Procedure
Stored Procedures
12
Steps:
2 Enter your commands in the editor and SAVE. Give the procedure a name.
Click on Apply.
Stored Procedures
13
Steps:
3 Your procedure can now be called programmatically or in the SQL Editor as
below: Call GetCountryDetails();
The statements in the procedure will get executed.
Triggers
14
A trigger or database trigger is a stored program executed automatically
to respond to a specific event e.g., insert, update or delete occurred in a
table.
A SQL trigger is a special type of stored procedure. It is special because it is
not called directly like a stored procedure. The main difference between a
trigger and a stored procedure is that a trigger is called automatically when a
data modification event is made against a table whereas a stored procedure
must be called explicitly.
Triggers
15
Advantages of using SQL triggers
• SQL triggers provide an alternative way to check the integrity of data.
• SQL triggers can catch errors in business logic in the database layer.
• SQL triggers provide an alternative way to run scheduled tasks. By using
SQL triggers, you don’t have to wait to run the scheduled tasks because
the triggers are invoked automatically before or after a change is made to
the data in the tables.
• SQL triggers are very useful to audit the changes of data in tables.
Triggers
16
Disadvantages of using SQL triggers
• SQL triggers only can provide an extended validation and they cannot
replace all the validations. Some simple validations have to be done in the
application layer. For example, you can validate user’s inputs in the client
side by using JavaScript or in the server side using server-side scripting
languages such as JSP, PHP, ASP.NET, Perl, etc.
• SQL triggers are invoked and executed invisible from the client
applications, therefore, it is difficult to figure out what happens in the
database layer.
• SQL triggers may increase the overhead of the database server.
Triggers
17
Example:
We want to capture the details of people performing any action on a
particular table employee, actions such as anyone executing BACKEND
UPDATE should get captured. For this we need to first create an Audit table
which will store these details as below:
CREATE TABLE employees_audit (
id INT AUTO_INCREMENT PRIMARY KEY,
emp_id INT NOT NULL,
last_name VARCHAR(50) NOT NULL,
first_name VARCHAR(50) NOT NULL,
jobtitle varchar(100),
changedate DATETIME DEFAULT NULL,
action VARCHAR(50) DEFAULT NULL);
Triggers
18
Example:
Next step would be writing the trigger.
DELIMITER $$
CREATE TRIGGER before_employee_update
BEFORE UPDATE ON employee
FOR EACH ROW
BEGIN
INSERT INTO employees_audit
SET action = 'update',
emp_id = OLD.emp_id,
last_name = OLD.last_name,
first_name = OLD.first_name,
jobtitle = OLD.jobtitle,
changedate = NOW();
END$$
DELIMITER ;
Triggers
19
Example:
Now we will fire an update on the employee table
update employee set jobtitle = 'programmer' where emp_id = 2;
This action will get captured in employees_audit table and can be viewed
later.
MySQL for Excel
20
• MySQL for Excel enables you to work with a MySQL database from
within Microsoft Excel.
• MySQL data can be imported into Excel, Excel data can be
exported into MySQL as a new table or appended to a current
table, and MySQL for Excel enables you to edit the MySQL data
directly from within Excel.
MySQL for Excel
21
Import MySQL Data into Excel
Data can be imported from MySQL into a Microsoft Excel spreadsheet by
using the Import MySQL Data option after selecting either a table, view, or
procedure to import.
Append Excel Data into MySQL
Data from a Microsoft Excel spreadsheet can be appended to a MySQL
database table by using the Append Excel MySQL Data to Table option.
Export Excel Data into MySQL
Data from a Microsoft Excel spreadsheet can be exported to a new MySQL
database table by using the Export Excel Data to New Table option. Exporting
data looks like so:
MySQL for Excel
22
Example:
Open the excel you want to export/Import data from. Click on MySQL for
Excel
Pre-requisite for doing the same is to install the MySQL Add-on utility
MySQL for Excel
23
Example:
Click on Local Instance MySQL. Enter the password requested and click OK
MySQL for Excel
24
Example:
Select the database in which you want to create a new table or append an
existing table with this excel data
MySQL for Excel
25
Example:
If you want to create a new table, then select the excel sheet and click on Export Excel
Data to New table and then give the name of the table in the column for Name in the
dialogue box. Click on Export Data
MySQL for Excel
26
Example:
After clicking Export Data below screen should appear
MySQL for Excel
27
Example:
Login to backend to check a new table is created and verify rows inserted. Similarly you
can explore more options available in the MySQL for Excel utility
To work with MySQL database in R
28
mydb <- dbConnect(MySQL(), user='user', password='password',
dbname='database_name', host='host')
# Create a database connection object.
install.packages(“RMySQL”)
library(RMySQL)
# Save a results set object to retrieve data from the database
rs = dbSendQuery(mydb, "select * from some_table")
# Access the result in R
data = fetch(rs, n)
Queries can be run using the dbSendQuery().
To access the results in R, fetch() is used.
This saves the results of the query as a data frame object.
n= specifies no. of records to be retrieved. A Value of
-1 in place of n retrieves all pending records.
# Install RMySQL package
THANK YOU!
29

More Related Content

What's hot

Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETE
Abrar ali
 
SQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate TableSQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate Table
1keydata
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
1keydata
 
View, Store Procedure & Function and Trigger in MySQL - Thaipt
View, Store Procedure & Function and Trigger in MySQL - ThaiptView, Store Procedure & Function and Trigger in MySQL - Thaipt
View, Store Procedure & Function and Trigger in MySQL - ThaiptFramgia Vietnam
 
Sq lite
Sq liteSq lite
SQL for interview
SQL for interviewSQL for interview
SQL for interview
Aditya Kumar Tripathy
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
Md.Mojibul Hoque
 
MySql:Introduction
MySql:IntroductionMySql:Introduction
MySql:Introduction
DataminingTools Inc
 
Database Systems - SQL - DCL Statements (Chapter 3/4)
Database Systems - SQL - DCL Statements (Chapter 3/4)Database Systems - SQL - DCL Statements (Chapter 3/4)
Database Systems - SQL - DCL Statements (Chapter 3/4)
Vidyasagar Mundroy
 
SQL Differences SQL Interview Questions
SQL Differences  SQL Interview QuestionsSQL Differences  SQL Interview Questions
SQL Differences SQL Interview Questions
MLR Institute of Technology
 
Sql intro & ddl 1
Sql intro & ddl 1Sql intro & ddl 1
Sql intro & ddl 1
Dr. C.V. Suresh Babu
 
Sql DML
Sql DMLSql DML
Sql DML
Vikas Gupta
 
Intro to T-SQL - 1st session
Intro to T-SQL - 1st sessionIntro to T-SQL - 1st session
Intro to T-SQL - 1st session
Medhat Dawoud
 
Chapter8 my sql revision tour
Chapter8 my sql revision tourChapter8 my sql revision tour
Chapter8 my sql revision tour
KV(AFS) Utarlai, Barmer (Rajasthan)
 
Sql server T-sql basics ppt-3
Sql server T-sql basics  ppt-3Sql server T-sql basics  ppt-3
Sql server T-sql basics ppt-3
Vibrant Technologies & Computers
 

What's hot (19)

Using T-SQL
Using T-SQL Using T-SQL
Using T-SQL
 
Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETE
 
MySql slides (ppt)
MySql slides (ppt)MySql slides (ppt)
MySql slides (ppt)
 
SQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate TableSQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate Table
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
 
View, Store Procedure & Function and Trigger in MySQL - Thaipt
View, Store Procedure & Function and Trigger in MySQL - ThaiptView, Store Procedure & Function and Trigger in MySQL - Thaipt
View, Store Procedure & Function and Trigger in MySQL - Thaipt
 
Sq lite
Sq liteSq lite
Sq lite
 
SQL for interview
SQL for interviewSQL for interview
SQL for interview
 
T-SQL Overview
T-SQL OverviewT-SQL Overview
T-SQL Overview
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
MySql:Introduction
MySql:IntroductionMySql:Introduction
MySql:Introduction
 
Database Systems - SQL - DCL Statements (Chapter 3/4)
Database Systems - SQL - DCL Statements (Chapter 3/4)Database Systems - SQL - DCL Statements (Chapter 3/4)
Database Systems - SQL - DCL Statements (Chapter 3/4)
 
SQL Differences SQL Interview Questions
SQL Differences  SQL Interview QuestionsSQL Differences  SQL Interview Questions
SQL Differences SQL Interview Questions
 
Sql intro & ddl 1
Sql intro & ddl 1Sql intro & ddl 1
Sql intro & ddl 1
 
Sql DML
Sql DMLSql DML
Sql DML
 
lovely
lovelylovely
lovely
 
Intro to T-SQL - 1st session
Intro to T-SQL - 1st sessionIntro to T-SQL - 1st session
Intro to T-SQL - 1st session
 
Chapter8 my sql revision tour
Chapter8 my sql revision tourChapter8 my sql revision tour
Chapter8 my sql revision tour
 
Sql server T-sql basics ppt-3
Sql server T-sql basics  ppt-3Sql server T-sql basics  ppt-3
Sql server T-sql basics ppt-3
 

Similar to Getting Started with MySQL II

PostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
PostgreSQL Performance Tables Partitioning vs. Aggregated Data TablesPostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
PostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
Sperasoft
 
SQL Tunning
SQL TunningSQL Tunning
SQL Tunning
Dhananjay Goel
 
Data Redaction - OTN TOUR LA 2015
Data Redaction - OTN TOUR LA 2015 Data Redaction - OTN TOUR LA 2015
Data Redaction - OTN TOUR LA 2015
Alex Zaballa
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsmaxpane
 
Oracle Data Redaction
Oracle Data RedactionOracle Data Redaction
Oracle Data Redaction
Alex Zaballa
 
Database versioning with liquibase
Database versioning with liquibaseDatabase versioning with liquibase
Database versioning with liquibase
Return on Intelligence
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
metsarin
 
New features of sql server 2005
New features of sql server 2005New features of sql server 2005
New features of sql server 2005Govind Raj
 
Database Automation with MySQL Triggers and Event Schedulers
Database Automation with MySQL Triggers and Event SchedulersDatabase Automation with MySQL Triggers and Event Schedulers
Database Automation with MySQL Triggers and Event Schedulers
Abdul Rahman Sherzad
 
SQL PPT.pptx
SQL PPT.pptxSQL PPT.pptx
SQL PPT.pptx
Kulbir4
 
Automatic upgrade and new error logging in my sql 8.0 oct
Automatic upgrade and new error logging in my sql 8.0 octAutomatic upgrade and new error logging in my sql 8.0 oct
Automatic upgrade and new error logging in my sql 8.0 oct
Ståle Deraas
 
05_DP_300T00A_Optimize.pptx
05_DP_300T00A_Optimize.pptx05_DP_300T00A_Optimize.pptx
05_DP_300T00A_Optimize.pptx
KareemBullard1
 
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
Dave Stokes
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQL
Vikash Sharma
 
HTG-SQL Server 2005 - Backup & Recovery.pdf
HTG-SQL Server 2005 - Backup & Recovery.pdfHTG-SQL Server 2005 - Backup & Recovery.pdf
HTG-SQL Server 2005 - Backup & Recovery.pdf
Sami Asmar
 
SQL_SERVER_BASIC_1_Training.pptx
SQL_SERVER_BASIC_1_Training.pptxSQL_SERVER_BASIC_1_Training.pptx
SQL_SERVER_BASIC_1_Training.pptx
KashifManzoorMeo
 
Performance tuning
Performance tuningPerformance tuning
Performance tuning
ami111
 
Sql server 2012 tutorials writing transact-sql statements
Sql server 2012 tutorials   writing transact-sql statementsSql server 2012 tutorials   writing transact-sql statements
Sql server 2012 tutorials writing transact-sql statementsSteve Xu
 
Creating other schema objects
Creating other schema objectsCreating other schema objects
Creating other schema objects
Syed Zaid Irshad
 
Capture Change and Apply It!
Capture Change and Apply It!Capture Change and Apply It!
Capture Change and Apply It!Steve Wake
 

Similar to Getting Started with MySQL II (20)

PostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
PostgreSQL Performance Tables Partitioning vs. Aggregated Data TablesPostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
PostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
 
SQL Tunning
SQL TunningSQL Tunning
SQL Tunning
 
Data Redaction - OTN TOUR LA 2015
Data Redaction - OTN TOUR LA 2015 Data Redaction - OTN TOUR LA 2015
Data Redaction - OTN TOUR LA 2015
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactions
 
Oracle Data Redaction
Oracle Data RedactionOracle Data Redaction
Oracle Data Redaction
 
Database versioning with liquibase
Database versioning with liquibaseDatabase versioning with liquibase
Database versioning with liquibase
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
 
New features of sql server 2005
New features of sql server 2005New features of sql server 2005
New features of sql server 2005
 
Database Automation with MySQL Triggers and Event Schedulers
Database Automation with MySQL Triggers and Event SchedulersDatabase Automation with MySQL Triggers and Event Schedulers
Database Automation with MySQL Triggers and Event Schedulers
 
SQL PPT.pptx
SQL PPT.pptxSQL PPT.pptx
SQL PPT.pptx
 
Automatic upgrade and new error logging in my sql 8.0 oct
Automatic upgrade and new error logging in my sql 8.0 octAutomatic upgrade and new error logging in my sql 8.0 oct
Automatic upgrade and new error logging in my sql 8.0 oct
 
05_DP_300T00A_Optimize.pptx
05_DP_300T00A_Optimize.pptx05_DP_300T00A_Optimize.pptx
05_DP_300T00A_Optimize.pptx
 
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQL
 
HTG-SQL Server 2005 - Backup & Recovery.pdf
HTG-SQL Server 2005 - Backup & Recovery.pdfHTG-SQL Server 2005 - Backup & Recovery.pdf
HTG-SQL Server 2005 - Backup & Recovery.pdf
 
SQL_SERVER_BASIC_1_Training.pptx
SQL_SERVER_BASIC_1_Training.pptxSQL_SERVER_BASIC_1_Training.pptx
SQL_SERVER_BASIC_1_Training.pptx
 
Performance tuning
Performance tuningPerformance tuning
Performance tuning
 
Sql server 2012 tutorials writing transact-sql statements
Sql server 2012 tutorials   writing transact-sql statementsSql server 2012 tutorials   writing transact-sql statements
Sql server 2012 tutorials writing transact-sql statements
 
Creating other schema objects
Creating other schema objectsCreating other schema objects
Creating other schema objects
 
Capture Change and Apply It!
Capture Change and Apply It!Capture Change and Apply It!
Capture Change and Apply It!
 

More from Sankhya_Analytics

Getting Started with Python
Getting Started with PythonGetting Started with Python
Getting Started with Python
Sankhya_Analytics
 
Data Management in Python
Data Management in PythonData Management in Python
Data Management in Python
Sankhya_Analytics
 
Basic Analysis using Python
Basic Analysis using PythonBasic Analysis using Python
Basic Analysis using Python
Sankhya_Analytics
 
Getting Started with R
Getting Started with RGetting Started with R
Getting Started with R
Sankhya_Analytics
 
Data Management in R
Data Management in RData Management in R
Data Management in R
Sankhya_Analytics
 
Basic Analysis using R
Basic Analysis using RBasic Analysis using R
Basic Analysis using R
Sankhya_Analytics
 
R Get Started II
R Get Started IIR Get Started II
R Get Started II
Sankhya_Analytics
 
R Get Started I
R Get Started IR Get Started I
R Get Started I
Sankhya_Analytics
 

More from Sankhya_Analytics (8)

Getting Started with Python
Getting Started with PythonGetting Started with Python
Getting Started with Python
 
Data Management in Python
Data Management in PythonData Management in Python
Data Management in Python
 
Basic Analysis using Python
Basic Analysis using PythonBasic Analysis using Python
Basic Analysis using Python
 
Getting Started with R
Getting Started with RGetting Started with R
Getting Started with R
 
Data Management in R
Data Management in RData Management in R
Data Management in R
 
Basic Analysis using R
Basic Analysis using RBasic Analysis using R
Basic Analysis using R
 
R Get Started II
R Get Started IIR Get Started II
R Get Started II
 
R Get Started I
R Get Started IR Get Started I
R Get Started I
 

Recently uploaded

一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
enxupq
 
FP Growth Algorithm and its Applications
FP Growth Algorithm and its ApplicationsFP Growth Algorithm and its Applications
FP Growth Algorithm and its Applications
MaleehaSheikh2
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdfSample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Linda486226
 
Business update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMIBusiness update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMI
AlejandraGmez176757
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
ewymefz
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
axoqas
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
Oppotus
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
vcaxypu
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
ewymefz
 
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
correoyaya
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
axoqas
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
ewymefz
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
Opendatabay
 
Investigate & Recover / StarCompliance.io / Crypto_Crimes
Investigate & Recover / StarCompliance.io / Crypto_CrimesInvestigate & Recover / StarCompliance.io / Crypto_Crimes
Investigate & Recover / StarCompliance.io / Crypto_Crimes
StarCompliance.io
 
Tabula.io Cheatsheet: automate your data workflows
Tabula.io Cheatsheet: automate your data workflowsTabula.io Cheatsheet: automate your data workflows
Tabula.io Cheatsheet: automate your data workflows
alex933524
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Subhajit Sahu
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape ReportSOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
TravisMalana
 

Recently uploaded (20)

一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
 
FP Growth Algorithm and its Applications
FP Growth Algorithm and its ApplicationsFP Growth Algorithm and its Applications
FP Growth Algorithm and its Applications
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdfSample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
 
Business update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMIBusiness update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMI
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
 
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
 
Investigate & Recover / StarCompliance.io / Crypto_Crimes
Investigate & Recover / StarCompliance.io / Crypto_CrimesInvestigate & Recover / StarCompliance.io / Crypto_Crimes
Investigate & Recover / StarCompliance.io / Crypto_Crimes
 
Tabula.io Cheatsheet: automate your data workflows
Tabula.io Cheatsheet: automate your data workflowsTabula.io Cheatsheet: automate your data workflows
Tabula.io Cheatsheet: automate your data workflows
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape ReportSOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape Report
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
 

Getting Started with MySQL II

  • 2. Database Views 2 A database view is a virtual table or logical table which is defined as a SQL SELECT query with joins. Most database management systems, including MySQL, allow you to update data in the underlying tables through the database view with some prerequisites. Advantages of database view: • A database view allows you to simplify complex queries: a database view is defined by an SQL statement that associates with many underlying tables. • A database view helps limit data access to specific users. • A database view provides extra security layer. • A database view enables computed columns.
  • 3. Database Views 3 Disadvantages of database view: • Performance: querying data from a database view can be slow especially if the view is created based on other views. • Tables dependency: you create a view based on underlying tables of the database. Whenever you change the structure of those tables that view associated with, you have to change the view as well.
  • 4. Database Views – CREATE VIEW 4 Syntax: CREATE VIEW [database_name].[view_name] AS [SELECT statement] There are some rules that the SELECT statement must follow: • The SELECT statement can contain a subquery in WHERE clause but not in the FROM clause. • The SELECT statement cannot refer to any variables including local variables, user variables, and session variables.
  • 5. Database Views – CREATE OR REPLACE VIEW 5 You can use CREATE OR REPLACE VIEW statement to either create or replace an existing view. If a view already exists, MySQL simply modifies the view. In case the view does not exist, MySQL creates a new view. Syntax: CREATE OR REPLACE VIEW [database_name].[view_name] AS [SELECT statement]
  • 6. Database Views – CREATE VIEW 6 Execute the example below in World database.
  • 7. Database Views – CREATE OR REPLACE VIEW 7 Execute the example below in World database.
  • 8. Stored Procedures 8 Introduction to MySQL Stored Procedures A stored procedure is a segment of declarative SQL statements stored inside the database catalog. A stored procedure can be invoked by triggers, other stored procedures, and applications such as Java, Python,R, PHP, ERP etc.
  • 9. Stored Procedures 9 MySQL stored procedures advantages: • Typically stored procedures help increase the performance of the applications. Once created, stored procedures are compiled and stored in the database. • Stored procedures help reduce the traffic between application and database server because instead of sending multiple lengthy SQL statements, the application has to send only name and parameters of the stored procedure. • Stored procedures are reusable and transparent to any applications. • Stored procedures are secure.
  • 10. Stored Procedures 10 MySQL stored procedures disadvantages • If you use a lot of stored procedures, the memory usage of every connection that is using those stored procedures will increase substantially. • Constructs of stored procedures make it more difficult to develop stored procedures that have complicated business logic. • It is difficult to debug stored procedures. Only a few database management systems allow you to debug stored procedures. Unfortunately, MySQL does not provide facilities for debugging stored procedures. • It is not easy to develop and maintain stored procedures.
  • 11. Stored Procedures 11 Steps: 1 Right Click on Stored Procedure – Create Procedure
  • 12. Stored Procedures 12 Steps: 2 Enter your commands in the editor and SAVE. Give the procedure a name. Click on Apply.
  • 13. Stored Procedures 13 Steps: 3 Your procedure can now be called programmatically or in the SQL Editor as below: Call GetCountryDetails(); The statements in the procedure will get executed.
  • 14. Triggers 14 A trigger or database trigger is a stored program executed automatically to respond to a specific event e.g., insert, update or delete occurred in a table. A SQL trigger is a special type of stored procedure. It is special because it is not called directly like a stored procedure. The main difference between a trigger and a stored procedure is that a trigger is called automatically when a data modification event is made against a table whereas a stored procedure must be called explicitly.
  • 15. Triggers 15 Advantages of using SQL triggers • SQL triggers provide an alternative way to check the integrity of data. • SQL triggers can catch errors in business logic in the database layer. • SQL triggers provide an alternative way to run scheduled tasks. By using SQL triggers, you don’t have to wait to run the scheduled tasks because the triggers are invoked automatically before or after a change is made to the data in the tables. • SQL triggers are very useful to audit the changes of data in tables.
  • 16. Triggers 16 Disadvantages of using SQL triggers • SQL triggers only can provide an extended validation and they cannot replace all the validations. Some simple validations have to be done in the application layer. For example, you can validate user’s inputs in the client side by using JavaScript or in the server side using server-side scripting languages such as JSP, PHP, ASP.NET, Perl, etc. • SQL triggers are invoked and executed invisible from the client applications, therefore, it is difficult to figure out what happens in the database layer. • SQL triggers may increase the overhead of the database server.
  • 17. Triggers 17 Example: We want to capture the details of people performing any action on a particular table employee, actions such as anyone executing BACKEND UPDATE should get captured. For this we need to first create an Audit table which will store these details as below: CREATE TABLE employees_audit ( id INT AUTO_INCREMENT PRIMARY KEY, emp_id INT NOT NULL, last_name VARCHAR(50) NOT NULL, first_name VARCHAR(50) NOT NULL, jobtitle varchar(100), changedate DATETIME DEFAULT NULL, action VARCHAR(50) DEFAULT NULL);
  • 18. Triggers 18 Example: Next step would be writing the trigger. DELIMITER $$ CREATE TRIGGER before_employee_update BEFORE UPDATE ON employee FOR EACH ROW BEGIN INSERT INTO employees_audit SET action = 'update', emp_id = OLD.emp_id, last_name = OLD.last_name, first_name = OLD.first_name, jobtitle = OLD.jobtitle, changedate = NOW(); END$$ DELIMITER ;
  • 19. Triggers 19 Example: Now we will fire an update on the employee table update employee set jobtitle = 'programmer' where emp_id = 2; This action will get captured in employees_audit table and can be viewed later.
  • 20. MySQL for Excel 20 • MySQL for Excel enables you to work with a MySQL database from within Microsoft Excel. • MySQL data can be imported into Excel, Excel data can be exported into MySQL as a new table or appended to a current table, and MySQL for Excel enables you to edit the MySQL data directly from within Excel.
  • 21. MySQL for Excel 21 Import MySQL Data into Excel Data can be imported from MySQL into a Microsoft Excel spreadsheet by using the Import MySQL Data option after selecting either a table, view, or procedure to import. Append Excel Data into MySQL Data from a Microsoft Excel spreadsheet can be appended to a MySQL database table by using the Append Excel MySQL Data to Table option. Export Excel Data into MySQL Data from a Microsoft Excel spreadsheet can be exported to a new MySQL database table by using the Export Excel Data to New Table option. Exporting data looks like so:
  • 22. MySQL for Excel 22 Example: Open the excel you want to export/Import data from. Click on MySQL for Excel Pre-requisite for doing the same is to install the MySQL Add-on utility
  • 23. MySQL for Excel 23 Example: Click on Local Instance MySQL. Enter the password requested and click OK
  • 24. MySQL for Excel 24 Example: Select the database in which you want to create a new table or append an existing table with this excel data
  • 25. MySQL for Excel 25 Example: If you want to create a new table, then select the excel sheet and click on Export Excel Data to New table and then give the name of the table in the column for Name in the dialogue box. Click on Export Data
  • 26. MySQL for Excel 26 Example: After clicking Export Data below screen should appear
  • 27. MySQL for Excel 27 Example: Login to backend to check a new table is created and verify rows inserted. Similarly you can explore more options available in the MySQL for Excel utility
  • 28. To work with MySQL database in R 28 mydb <- dbConnect(MySQL(), user='user', password='password', dbname='database_name', host='host') # Create a database connection object. install.packages(“RMySQL”) library(RMySQL) # Save a results set object to retrieve data from the database rs = dbSendQuery(mydb, "select * from some_table") # Access the result in R data = fetch(rs, n) Queries can be run using the dbSendQuery(). To access the results in R, fetch() is used. This saves the results of the query as a data frame object. n= specifies no. of records to be retrieved. A Value of -1 in place of n retrieves all pending records. # Install RMySQL package