SlideShare a Scribd company logo
1 of 25
MySQL Tutorial
Introduction to Database
Introduction of MySQL






MySQL is an SQL (Structured Query Language)
based relational database management system
(DBMS)
MySQL is compatible with standard SQL
MySQL is frequently used by PHP and Perl
Commercial version of MySQL is also provided
(including technical support)
Resource


MySQL and GUI Client can be downloaded
from
http://dev.mysql.com/downloads/



The SQL script for creating database
‘bank’ can be found at



http://www.cs.kent.edu/~mabuata/DB10_lab/bank_db.sql
http://www.cs.kent.edu/~mabuata/DB10_lab/bank_data.sql
Command for accessing MySQL


Access from DB server
>ssh dbdev.cs.kent.edu
Start MySQL
>mysql –u [username] –p
>Enter password:[password]



From a departmental machine
>mysql -u [username] -h dbdev.cs.kent.edu –p
>Enter password:[password]
Entering & Editing commands


Prompt mysql>





issue a command
Mysql sends it to the server for execution
displays the results
prints another mysql>

a command could span multiple lines
 A command normally consists of SQL
statement followed by a semicolon

Command prompt
prompt

meaning

mysql>

Ready for new command.

->

Waiting for next line of multiple-line command.

‘>

Waiting for next line, waiting for completion of a
string that began with a single quote (“'”).

“>

Waiting for next line, waiting for completion of a
string that began with a double quote (“"”).

`>

Waiting for next line, waiting for completion of an
identifier that began with a backtick (“`”).

/*>

Waiting for next line, waiting for completion of a
comment that began with /*.
MySQL commands
help h
 Quit/exit q
 Cancel the command c
 Change database use
 …etc

Info about databases and tables


Listing the databases on the MySQL server host




Access/change database




> select database();

Showing tables in the current database




>Use [database_name]

Showing the current selected database




>show databases;

>show tables;

Showing the structure of a table


> describe [table_name];
Banking Example
branch (branch-name, branch-city, assets)
customer (customer-name, customer-street,
customer-city)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
employee (employee-name, branch-name, salary)
CREATE DATABASE


An SQL relation is defined using the CREATE
DATABASE command:




create database [database name]

Example


create database mydatabase
SQL Script for creating tables


The SQL script for creating database
‘bank’ can be found at

http://www.cs.kent.edu/~mabuata/DB10_lab/bank_db.sql
http://www.cs.kent.edu/~mabuata/DB10_lab/bank_data.sql

Notice: we do not have permission to create database,
so you have to type command “use [your_account]” to
work on your database.
Query


To find all loan number for loans made at the Perryridge
branch with loan amounts greater than $1100.
select loan_number from loan
where branch_name = ‘Perryridge’ and amount>1100;



Find the loan number of those loans with loan amounts
between $1,000 and $1,500 (that is, ≥$1,000 and ≤$1,500)
select loan_number from loan
where amount between 1000 and 1500;
Query
Find the names of all branches that have greater assets than
some branch located in Brooklyn.
select distinct T.branch_name
from branch as T, branch as S
where T.assets > S.assets and S.branch_city = ‘Brooklyn’;
Find the customer names and their loan numbers for all customers
having a loan at some branch.
select customer_name, T.loan_number, S.amount
from borrower as T, loan as S
where T.loan_number = S.loan_number;
Set Operation


Find all customers who have a loan, an account, or both:
(select customer_name from depositor)
union
(select customer_name from borrower);



Find all customers who have an account but no loan.
(no minus operator provided in mysql)
select customer_name from depositor
where customer_name not in
(select customer_name from borrower);
Aggregate function


Find the number of depositors for each branch.
select branch_name, count (distinct customer_name)
from depositor, account
where depositor.account_number = account.account_number
group by branch_name;



Find the names of all branches where the average account balance
is more than $500.
select branch_name, avg (balance)
from account
group by branch_name
having avg(balance) > 500;
Nested Subqueries


Find all customers who have both an account and a loan at
the bank.
select distinct customer_name
from borrower
where customer_name in
(select customer_name from depositor);



Find all customers who have a loan at the bank but do not
have an account at the bank
select distinct customer_name
from borrower
where customer_name not in
(select customer_name from depositor);
Nested Subquery


Find the names of all branches that have greater assets
than all branches located in Horseneck.
select branch_name
from branch
where assets > all
(select assets
from branch
where branch_city = ‘Horseneck’);
Create View (new feature in mysql 5.0)


A view consisting of branches and their customers
create view all_customer as
(select branch_name, customer_name
from depositor, account
where depositor.account_number =
account.account_number)
union
(select branch_name, customer_name
from borrower, loan
where borrower.loan_number=loan.loan_number);
Joined Relations








Join operations take two relations and return as a result
another relation.
These additional operations are typically used as subquery
expressions in the from clause
Join condition – defines which tuples in the two relations
match, and what attributes are present in the result of the join.
Join type – defines how tuples in each relation that do not
match any tuple in the other relation (based on the join
condition) are treated.
Joined Relations – Datasets for Examples


Relation loan

Relation borrower

Note: borrower information missing for L-260 and loan
information missing for L-155
Joined Relations – Examples



Select * from loan inner join borrower on
loan.loan-number = borrower.loan-number

loan-number

branch-name

amount

customer-name

loan-number

L-170

Downtown

3000

Jones

L-170

L-230

Redwood

4000

Smith

L-230
Example

Select * from loan left join borrower on
loan.loan-number = borrower.loan-number
loan-number

branch-name

amount

customer-name

loan-number

L-170

Downtown

3000

Jones

L-170

L-230

Redwood

4000

Smith

L-230

L-260

Perryridge

1700

null

null
Modification of Database


Increase all accounts with balances over $800 by 7%, all
other accounts receive 8%.
update account
set balance = balance ∗ 1.07
where balance > 800;
update account
set balance = balance ∗ 1.08
where balance ≤ 800;
Modification of Database


Increase all accounts with balances over $700 by 6%, all
other accounts receive 5%.
update account
set balance =case
when balance <= 700 then balance *1.05
else balance * 1.06
end;
Modification of Database


Delete the record of all accounts with balances below the
average at the bank.
delete from account
where balance < (select avg (balance) from account);



Add a new tuple to account
insert into account
values (‘A-9732’, ‘Perryridge’,1200);

More Related Content

Similar to Mysql tutorial

SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introductionSmriti Jain
 
Database : Relational Data Model
Database : Relational Data ModelDatabase : Relational Data Model
Database : Relational Data ModelSmriti Jain
 
Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01Ankit Dubey
 
Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01Ankit Dubey
 
BITM3730Week15.pptx
BITM3730Week15.pptxBITM3730Week15.pptx
BITM3730Week15.pptxMattMarino13
 
Intro to AppExchange - Building Composite Apps
Intro to AppExchange - Building Composite AppsIntro to AppExchange - Building Composite Apps
Intro to AppExchange - Building Composite Appsdreamforce2006
 
Accessing_MySQL_from_Ruby
Accessing_MySQL_from_RubyAccessing_MySQL_from_Ruby
Accessing_MySQL_from_Rubytutorialsruby
 
Accessing_MySQL_from_Ruby
Accessing_MySQL_from_RubyAccessing_MySQL_from_Ruby
Accessing_MySQL_from_Rubytutorialsruby
 
DBMS LAB FILE1 task 1 , task 2, task3 and many more.pdf
DBMS LAB FILE1 task 1 , task 2, task3 and many more.pdfDBMS LAB FILE1 task 1 , task 2, task3 and many more.pdf
DBMS LAB FILE1 task 1 , task 2, task3 and many more.pdfAbhishekKumarPandit5
 
5. Other Relational Languages in DBMS
5. Other Relational Languages in DBMS5. Other Relational Languages in DBMS
5. Other Relational Languages in DBMSkoolkampus
 
Build Message-Based Web Services for SOA
Build Message-Based Web Services for SOABuild Message-Based Web Services for SOA
Build Message-Based Web Services for SOAJeffrey Hasan
 
UDDI from JAVA Web services
UDDI from JAVA Web services UDDI from JAVA Web services
UDDI from JAVA Web services uday katti
 
Lightning Connect Custom Adapters: Connecting Anything with Salesforce
Lightning Connect Custom Adapters: Connecting Anything with SalesforceLightning Connect Custom Adapters: Connecting Anything with Salesforce
Lightning Connect Custom Adapters: Connecting Anything with SalesforceSalesforce Developers
 

Similar to Mysql tutorial (20)

SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
Database : Relational Data Model
Database : Relational Data ModelDatabase : Relational Data Model
Database : Relational Data Model
 
Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01
 
Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01Sm relationaldatamodel-150423084157-conversion-gate01
Sm relationaldatamodel-150423084157-conversion-gate01
 
dbms first unit
dbms first unitdbms first unit
dbms first unit
 
BITM3730Week15.pptx
BITM3730Week15.pptxBITM3730Week15.pptx
BITM3730Week15.pptx
 
Assignment#04
Assignment#04Assignment#04
Assignment#04
 
SQL PPT.ppt
SQL PPT.pptSQL PPT.ppt
SQL PPT.ppt
 
Intro to AppExchange - Building Composite Apps
Intro to AppExchange - Building Composite AppsIntro to AppExchange - Building Composite Apps
Intro to AppExchange - Building Composite Apps
 
Accessing_MySQL_from_Ruby
Accessing_MySQL_from_RubyAccessing_MySQL_from_Ruby
Accessing_MySQL_from_Ruby
 
Accessing_MySQL_from_Ruby
Accessing_MySQL_from_RubyAccessing_MySQL_from_Ruby
Accessing_MySQL_from_Ruby
 
DBMS LAB FILE1 task 1 , task 2, task3 and many more.pdf
DBMS LAB FILE1 task 1 , task 2, task3 and many more.pdfDBMS LAB FILE1 task 1 , task 2, task3 and many more.pdf
DBMS LAB FILE1 task 1 , task 2, task3 and many more.pdf
 
5. Other Relational Languages in DBMS
5. Other Relational Languages in DBMS5. Other Relational Languages in DBMS
5. Other Relational Languages in DBMS
 
Build Message-Based Web Services for SOA
Build Message-Based Web Services for SOABuild Message-Based Web Services for SOA
Build Message-Based Web Services for SOA
 
Salesforce and sap integration
Salesforce and sap integrationSalesforce and sap integration
Salesforce and sap integration
 
UDDI from JAVA Web services
UDDI from JAVA Web services UDDI from JAVA Web services
UDDI from JAVA Web services
 
Active record(1)
Active record(1)Active record(1)
Active record(1)
 
Lightning Connect Custom Adapters: Connecting Anything with Salesforce
Lightning Connect Custom Adapters: Connecting Anything with SalesforceLightning Connect Custom Adapters: Connecting Anything with Salesforce
Lightning Connect Custom Adapters: Connecting Anything with Salesforce
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Exploring the Salesforce REST API
Exploring the Salesforce REST APIExploring the Salesforce REST API
Exploring the Salesforce REST API
 

More from santosh mishra (10)

My History
My HistoryMy History
My History
 
sasasasasasas
sasasasasasassasasasasasas
sasasasasasas
 
Biology
BiologyBiology
Biology
 
History
HistoryHistory
History
 
Biology
BiologyBiology
Biology
 
Mysql tutorial
Mysql tutorialMysql tutorial
Mysql tutorial
 
Mysql rab2-student
Mysql rab2-studentMysql rab2-student
Mysql rab2-student
 
32.java input-output
32.java input-output32.java input-output
32.java input-output
 
Mysql tutorial
Mysql tutorialMysql tutorial
Mysql tutorial
 
Mysql rab2-student
Mysql rab2-studentMysql rab2-student
Mysql rab2-student
 

Recently uploaded

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 

Recently uploaded (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 

Mysql tutorial

  • 2. Introduction of MySQL     MySQL is an SQL (Structured Query Language) based relational database management system (DBMS) MySQL is compatible with standard SQL MySQL is frequently used by PHP and Perl Commercial version of MySQL is also provided (including technical support)
  • 3. Resource  MySQL and GUI Client can be downloaded from http://dev.mysql.com/downloads/  The SQL script for creating database ‘bank’ can be found at   http://www.cs.kent.edu/~mabuata/DB10_lab/bank_db.sql http://www.cs.kent.edu/~mabuata/DB10_lab/bank_data.sql
  • 4. Command for accessing MySQL  Access from DB server >ssh dbdev.cs.kent.edu Start MySQL >mysql –u [username] –p >Enter password:[password]  From a departmental machine >mysql -u [username] -h dbdev.cs.kent.edu –p >Enter password:[password]
  • 5. Entering & Editing commands  Prompt mysql>     issue a command Mysql sends it to the server for execution displays the results prints another mysql> a command could span multiple lines  A command normally consists of SQL statement followed by a semicolon 
  • 6. Command prompt prompt meaning mysql> Ready for new command. -> Waiting for next line of multiple-line command. ‘> Waiting for next line, waiting for completion of a string that began with a single quote (“'”). “> Waiting for next line, waiting for completion of a string that began with a double quote (“"”). `> Waiting for next line, waiting for completion of an identifier that began with a backtick (“`”). /*> Waiting for next line, waiting for completion of a comment that began with /*.
  • 7. MySQL commands help h  Quit/exit q  Cancel the command c  Change database use  …etc 
  • 8. Info about databases and tables  Listing the databases on the MySQL server host   Access/change database   > select database(); Showing tables in the current database   >Use [database_name] Showing the current selected database   >show databases; >show tables; Showing the structure of a table  > describe [table_name];
  • 9. Banking Example branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-city) account (account-number, branch-name, balance) loan (loan-number, branch-name, amount) depositor (customer-name, account-number) borrower (customer-name, loan-number) employee (employee-name, branch-name, salary)
  • 10. CREATE DATABASE  An SQL relation is defined using the CREATE DATABASE command:   create database [database name] Example  create database mydatabase
  • 11. SQL Script for creating tables  The SQL script for creating database ‘bank’ can be found at http://www.cs.kent.edu/~mabuata/DB10_lab/bank_db.sql http://www.cs.kent.edu/~mabuata/DB10_lab/bank_data.sql Notice: we do not have permission to create database, so you have to type command “use [your_account]” to work on your database.
  • 12. Query  To find all loan number for loans made at the Perryridge branch with loan amounts greater than $1100. select loan_number from loan where branch_name = ‘Perryridge’ and amount>1100;  Find the loan number of those loans with loan amounts between $1,000 and $1,500 (that is, ≥$1,000 and ≤$1,500) select loan_number from loan where amount between 1000 and 1500;
  • 13. Query Find the names of all branches that have greater assets than some branch located in Brooklyn. select distinct T.branch_name from branch as T, branch as S where T.assets > S.assets and S.branch_city = ‘Brooklyn’; Find the customer names and their loan numbers for all customers having a loan at some branch. select customer_name, T.loan_number, S.amount from borrower as T, loan as S where T.loan_number = S.loan_number;
  • 14. Set Operation  Find all customers who have a loan, an account, or both: (select customer_name from depositor) union (select customer_name from borrower);  Find all customers who have an account but no loan. (no minus operator provided in mysql) select customer_name from depositor where customer_name not in (select customer_name from borrower);
  • 15. Aggregate function  Find the number of depositors for each branch. select branch_name, count (distinct customer_name) from depositor, account where depositor.account_number = account.account_number group by branch_name;  Find the names of all branches where the average account balance is more than $500. select branch_name, avg (balance) from account group by branch_name having avg(balance) > 500;
  • 16. Nested Subqueries  Find all customers who have both an account and a loan at the bank. select distinct customer_name from borrower where customer_name in (select customer_name from depositor);  Find all customers who have a loan at the bank but do not have an account at the bank select distinct customer_name from borrower where customer_name not in (select customer_name from depositor);
  • 17. Nested Subquery  Find the names of all branches that have greater assets than all branches located in Horseneck. select branch_name from branch where assets > all (select assets from branch where branch_city = ‘Horseneck’);
  • 18. Create View (new feature in mysql 5.0)  A view consisting of branches and their customers create view all_customer as (select branch_name, customer_name from depositor, account where depositor.account_number = account.account_number) union (select branch_name, customer_name from borrower, loan where borrower.loan_number=loan.loan_number);
  • 19. Joined Relations     Join operations take two relations and return as a result another relation. These additional operations are typically used as subquery expressions in the from clause Join condition – defines which tuples in the two relations match, and what attributes are present in the result of the join. Join type – defines how tuples in each relation that do not match any tuple in the other relation (based on the join condition) are treated.
  • 20. Joined Relations – Datasets for Examples  Relation loan Relation borrower Note: borrower information missing for L-260 and loan information missing for L-155
  • 21. Joined Relations – Examples  Select * from loan inner join borrower on loan.loan-number = borrower.loan-number loan-number branch-name amount customer-name loan-number L-170 Downtown 3000 Jones L-170 L-230 Redwood 4000 Smith L-230
  • 22. Example Select * from loan left join borrower on loan.loan-number = borrower.loan-number loan-number branch-name amount customer-name loan-number L-170 Downtown 3000 Jones L-170 L-230 Redwood 4000 Smith L-230 L-260 Perryridge 1700 null null
  • 23. Modification of Database  Increase all accounts with balances over $800 by 7%, all other accounts receive 8%. update account set balance = balance ∗ 1.07 where balance > 800; update account set balance = balance ∗ 1.08 where balance ≤ 800;
  • 24. Modification of Database  Increase all accounts with balances over $700 by 6%, all other accounts receive 5%. update account set balance =case when balance <= 700 then balance *1.05 else balance * 1.06 end;
  • 25. Modification of Database  Delete the record of all accounts with balances below the average at the bank. delete from account where balance < (select avg (balance) from account);  Add a new tuple to account insert into account values (‘A-9732’, ‘Perryridge’,1200);