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
 >show databases;
 Access/change database
 >Use [database_name]
 Showing the current selected database
 > select database();
 Showing tables in the current database
 >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
branch-name amount
Downtown
Redwood
3000
4000
customer-name loan-number
Jones
Smith
L-170
L-230
loan-number
L-170
L-230
Example
Select * from loan left join borrower on
loan.loan-number = borrower.loan-number
branch-name amount
Downtown
Redwood
Perryridge
3000
4000
1700
customer-name loan-number
Jones
Smith
null
L-170
L-230
null
loan-number
L-170
L-230
L-260
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

Viewers also liked

Viewers also liked (7)

Mule and web services
Mule and web servicesMule and web services
Mule and web services
 
Global warming
Global warmingGlobal warming
Global warming
 
Grindrod Overview 2015
Grindrod Overview 2015Grindrod Overview 2015
Grindrod Overview 2015
 
Json
JsonJson
Json
 
Trends
TrendsTrends
Trends
 
Software Architecture
Software ArchitectureSoftware Architecture
Software Architecture
 
야놀자 회사소개서 201507
야놀자 회사소개서 201507야놀자 회사소개서 201507
야놀자 회사소개서 201507
 

Similar to My sql advacnce topics

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
 
5. Other Relational Languages in DBMS
5. Other Relational Languages in DBMS5. Other Relational Languages in DBMS
5. Other Relational Languages in DBMSkoolkampus
 
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
 
BITM3730Week15.pptx
BITM3730Week15.pptxBITM3730Week15.pptx
BITM3730Week15.pptxMattMarino13
 
MySQL USER MANAGEMENT,ROUTINES & TRIGGERS.
MySQL USER MANAGEMENT,ROUTINES & TRIGGERS.MySQL USER MANAGEMENT,ROUTINES & TRIGGERS.
MySQL USER MANAGEMENT,ROUTINES & TRIGGERS.Prabhu Raja Singh
 

Similar to My sql advacnce topics (20)

Mysql tutorial
Mysql tutorialMysql tutorial
Mysql tutorial
 
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
 
5. Other Relational Languages in DBMS
5. Other Relational Languages in DBMS5. Other Relational Languages in DBMS
5. Other Relational Languages in DBMS
 
Assignment#04
Assignment#04Assignment#04
Assignment#04
 
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
 
dbms first unit
dbms first unitdbms first unit
dbms first unit
 
SQL PPT.ppt
SQL PPT.pptSQL PPT.ppt
SQL PPT.ppt
 
BITM3730Week15.pptx
BITM3730Week15.pptxBITM3730Week15.pptx
BITM3730Week15.pptx
 
Marcus Matthews
Marcus MatthewsMarcus Matthews
Marcus Matthews
 
Unit04 dbms
Unit04 dbmsUnit04 dbms
Unit04 dbms
 
Assignment#09
Assignment#09Assignment#09
Assignment#09
 
MySQL USER MANAGEMENT,ROUTINES & TRIGGERS.
MySQL USER MANAGEMENT,ROUTINES & TRIGGERS.MySQL USER MANAGEMENT,ROUTINES & TRIGGERS.
MySQL USER MANAGEMENT,ROUTINES & TRIGGERS.
 
Ch3
Ch3Ch3
Ch3
 
Ch5
Ch5Ch5
Ch5
 
Using Mysql.pptx
Using Mysql.pptxUsing Mysql.pptx
Using Mysql.pptx
 
E-R diagram & SQL
E-R diagram & SQLE-R diagram & SQL
E-R diagram & SQL
 
Ch4
Ch4Ch4
Ch4
 

More from Prabhat gangwar

More from Prabhat gangwar (20)

Middleware
MiddlewareMiddleware
Middleware
 
Pseudolocalization
PseudolocalizationPseudolocalization
Pseudolocalization
 
Mule anypoint studio
Mule anypoint studioMule anypoint studio
Mule anypoint studio
 
Mule anypoint platform
Mule anypoint platformMule anypoint platform
Mule anypoint platform
 
What is cluster analysis
What is cluster analysisWhat is cluster analysis
What is cluster analysis
 
clustering and load balancing
clustering and load balancingclustering and load balancing
clustering and load balancing
 
Middleware systems overview and introduction
Middleware systems overview and introductionMiddleware systems overview and introduction
Middleware systems overview and introduction
 
Restful api modeling language
Restful api modeling languageRestful api modeling language
Restful api modeling language
 
Mule esb
Mule esbMule esb
Mule esb
 
Mule fundamentals
Mule fundamentalsMule fundamentals
Mule fundamentals
 
Gsm architecture
Gsm architectureGsm architecture
Gsm architecture
 
Oracle vs-mulesoft-api-manager-features
Oracle vs-mulesoft-api-manager-featuresOracle vs-mulesoft-api-manager-features
Oracle vs-mulesoft-api-manager-features
 
Oracle real application_cluster
Oracle real application_clusterOracle real application_cluster
Oracle real application_cluster
 
Introducing adf business components
Introducing adf business componentsIntroducing adf business components
Introducing adf business components
 
File transfer methods
File transfer methodsFile transfer methods
File transfer methods
 
Ftp tftp
Ftp tftpFtp tftp
Ftp tftp
 
Bpm
BpmBpm
Bpm
 
Seo
SeoSeo
Seo
 
Vedic mathmetics
Vedic mathmeticsVedic mathmetics
Vedic mathmetics
 
Buissness model analysis
Buissness model analysisBuissness model analysis
Buissness model analysis
 

Recently uploaded

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 

Recently uploaded (20)

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 

My sql advacnce topics

  • 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  >show databases;  Access/change database  >Use [database_name]  Showing the current selected database  > select database();  Showing tables in the current database  >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 branch-name amount Downtown Redwood 3000 4000 customer-name loan-number Jones Smith L-170 L-230 loan-number L-170 L-230
  • 22. Example Select * from loan left join borrower on loan.loan-number = borrower.loan-number branch-name amount Downtown Redwood Perryridge 3000 4000 1700 customer-name loan-number Jones Smith null L-170 L-230 null loan-number L-170 L-230 L-260
  • 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);