SlideShare a Scribd company logo
1 of 22
MySQL Tutorial
Introduction to Database
Learning Objectives
 Read and write Data Definition grammar of SQL
 Read and write data modification statements
 (INSERT, UPDATE, DELETE)
 Read and write basic SELECT FROM WHERE
queries
 Use aggregate functions
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)
Part1: SQL used for Data Definition
 Allows the specification of not only a set of
relations but also information about each
relation, including:
 The schema for each relation
 The domain of values associated with each
attribute
 Integrity constraints
Domain Types in SQL
 Similar to data types in classical programming languages
Type Description
CHAR(n) Fixed length character string, with specified length n
VARCHAR(n) Variable length character string, with specified
maximum length n
INTEGER Integer (a machine-dependent finite subset of the
integers)
SMALLINT(n) A small integer (a finite subset of INTEGER)
FLOAT(M,D) Floating point number, with total number of digits M
and number of digits following the decimal point D
DOUBLE(M,D) Double-precision floating point number
CREATE DATABASE
 An SQL relation is defined using the CREATE
DATABASE command:
 create database [database name]
 Example
 create database mydatabase
CREATE TABLE
 An SQL relation is defined using the CREATE
TABLE command:
 Create table [tablename] (A1 T1,A2 T2, … An Tn,
(integrity-constraint1),
…,
(integrity-constraintk))
 Each Ai is an attribute name in the table
 Each Ti is the data type of values for Ai
 Example
 Create table student
(flashlineID char(9) not null,
name varchar(30),
age integer,
department varchar(20),
primary key (flashlineID) );
Integrity constraint
DROP and ALTER TABLE
 The DROP TABLE command deletes all
information about the dropped relation from the
database
 The ALTER TABLE command is used to add
attributes to or remove attributes from an
existing relation (table):
alter table tablename actions
where actions can be one of following actions:
ADD Attribute
DROP Attribute
ADD PRIMARY KEY (Attribute_name1,…)
DROP PRIMARY KEY
Part2: Modifying the database
3 basic cases:
Add a tuple INSERT INTO table_name VALUES
(Val1, Val2, … , Valn)
Change
tuples
UPDATE table_name
SET A1=val1, A2=val2, …, An=valn
WHERE tuple_selection_predicate
Remove
tuples
DELETE FROM table_name
WHERE tuple_selection_predicate
INSERTION
 Add a new tuple to student
insert into student
values(‘999999999’,’Mike’,18,’computer science’)
or equivalently
insert into student(flashlineID,name,age,department)
values(‘999999999’,’Mike’,18,’computer science’)
 Add a new tuple to student with age set to null
insert into student
values(‘999999999’,’Mike’,null,’computer science’)
UPDATE
 Set all department to ‘computer science’
update student
set department=‘computer science’
 In table account(account_number, balance,
branch_name, branch_city), increase the
balances of all accounts by 6%
update account
set balance=balance*1.06
DELETION
 Delete records of all students in the
university
delete from student
 Delete the students who study computer
science
delete from student
where department=‘computer science’
Part3: Basic Query Structure
 A typical SQL query has the form:
select A1, A2, …, An
from table1, table2, …, tablem
where P
 Ai represents an attribute
 tablei represents a table
 P is a constraints (condition)
 This query is equivalent to the relational algebra
expression:
 Example
Select flashlineID, name from student
Where department=‘computer science’
))
,...,
,
(
( 2
1
,...,
, 2
1 m
P
A
A
A table
table
table
n


The SELECT Clause – Duplicate tuples
 Unlike pure relational algebra, SQL does not automatically
remove duplicate tuples from relations or query results
 To eliminate duplicates, insert the keyword distinct after
select.
 Example: Find the names of all students in the university, and
remove duplicates
select distinct name
from student
The SELECT Clause – Expressions, as
 An star in the select clause denotes “all attributes”
select * from student
 An expression can be assigned a name using as
 Example
select FlashlineID as ID
from student
Note: as is rename clause, also can be used to rename
table name
select name as myname
from student as S
The WHERE Clause
 The WHERE clause specifies the conditions
(constraints) results satisfy
 Corresponds to the selection predicate σ
 Comparisons and Booleans are as follows:
 Comparison operator: <, <=, >,>=, =, <>
 Logical operators: and, or, not
 Example
 Find names of all students in computer science
department with age smaller than 18
select names
from student
where department=‘computer science’ and age<18
Aggregate Functions
 Aggregate functions operate on the multiset of
values of a attribute and return a value
avg(attribute): average value
min(attribute): minimum value
max(attribute): maximum value
sum(attribute): sum of values
count(attribute): number of values
 To obtain the value when duplicates are removed,
insert the keyword distinct before attribute
name:
avg(distinct attribute)
Aggregation: GROUP BY clause
 GROUP BY attribute operate in this sequence
1. Groups the attribute set’s members into subsets by value
2. Performs the aggregate separately on each subset
3. Produces a result value for each subset
 Example: list each department and its number
of students
select department, count(distinct name) as number
from student
group by department
Note: if a select clause contains any aggregate functions, then all non-aggregated terms
in the select clause must be used in a group by clause. Ex: department is not
aggregated, so it must be in the group by clause.
Null Values and Aggregate
 The youngest student in the university
select *
from student
where age=min(age)
 Above statement ignores null amounts
 Result is null if there is no non-null amount
 All aggregate operations except count(*)
ignore tuples with null values on the
aggregated attributes.
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/~nruan/bank_db.s
ql
http://www.cs.kent.edu/~nruan/bank_dat
a.sql
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)
Command for accessing MySQL
 Access linux server
>ssh hercules.cs.kent.edu
 Access MySQL
>mysql –u [username] –p
>password:[password]

More Related Content

What's hot

Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySqlDhyey Dattani
 
CBSE XII Database Concepts And MySQL Presentation
CBSE XII Database Concepts And MySQL PresentationCBSE XII Database Concepts And MySQL Presentation
CBSE XII Database Concepts And MySQL PresentationGuru Ji
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .NetGreg Sohl
 
Entity Relationship Diagram
Entity Relationship DiagramEntity Relationship Diagram
Entity Relationship DiagramShakila Mahjabin
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java ProgrammingMath-Circle
 
E-R diagram in Database
E-R diagram in DatabaseE-R diagram in Database
E-R diagram in DatabaseFatiha Qureshi
 
String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)Anwar Hasan Shuvo
 
Dbms
DbmsDbms
DbmsCAG
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaRaghu nath
 
Relational Algebra & Calculus
Relational Algebra & CalculusRelational Algebra & Calculus
Relational Algebra & CalculusAbdullah Khosa
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Javabackdoor
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to javaSaba Ameer
 

What's hot (20)

Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySql
 
CBSE XII Database Concepts And MySQL Presentation
CBSE XII Database Concepts And MySQL PresentationCBSE XII Database Concepts And MySQL Presentation
CBSE XII Database Concepts And MySQL Presentation
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .Net
 
Chapter8 my sql revision tour
Chapter8 my sql revision tourChapter8 my sql revision tour
Chapter8 my sql revision tour
 
Python-DataAbstarction.pptx
Python-DataAbstarction.pptxPython-DataAbstarction.pptx
Python-DataAbstarction.pptx
 
Sql commands
Sql commandsSql commands
Sql commands
 
Entity Relationship Diagram
Entity Relationship DiagramEntity Relationship Diagram
Entity Relationship Diagram
 
For Beginers - ADO.Net
For Beginers - ADO.NetFor Beginers - ADO.Net
For Beginers - ADO.Net
 
Java program structure
Java program structureJava program structure
Java program structure
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
 
E-R diagram in Database
E-R diagram in DatabaseE-R diagram in Database
E-R diagram in Database
 
String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Entity relationship modelling
Entity relationship modellingEntity relationship modelling
Entity relationship modelling
 
Dbms
DbmsDbms
Dbms
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Relational Algebra & Calculus
Relational Algebra & CalculusRelational Algebra & Calculus
Relational Algebra & Calculus
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Java spring ppt
Java spring pptJava spring ppt
Java spring ppt
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 

Similar to mysql.ppt

STRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESSTRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESVENNILAV6
 
Its about a sql topic for basic structured query language
Its about a sql topic for basic structured query languageIts about a sql topic for basic structured query language
Its about a sql topic for basic structured query languageIMsKanchanaI
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functionsVikas Gupta
 
DBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxDBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxjainendraKUMAR55
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDev Chauhan
 
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQueryPPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQueryAbhishek590097
 
Key functions in_oracle_sql
Key functions in_oracle_sqlKey functions in_oracle_sql
Key functions in_oracle_sqlpgolhar
 
4 the sql_standard
4 the  sql_standard4 the  sql_standard
4 the sql_standardUtkarsh De
 

Similar to mysql.ppt (20)

PPT
PPTPPT
PPT
 
Db1 lecture4
Db1 lecture4Db1 lecture4
Db1 lecture4
 
STRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESSTRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIES
 
Its about a sql topic for basic structured query language
Its about a sql topic for basic structured query languageIts about a sql topic for basic structured query language
Its about a sql topic for basic structured query language
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
lect 2.pptx
lect 2.pptxlect 2.pptx
lect 2.pptx
 
SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
 
DBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxDBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptx
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
 
Chapter-5.ppt
Chapter-5.pptChapter-5.ppt
Chapter-5.ppt
 
SQL : introduction
SQL : introductionSQL : introduction
SQL : introduction
 
Templates
TemplatesTemplates
Templates
 
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQueryPPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
Module03
Module03Module03
Module03
 
ADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASADADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASAD
 
Key functions in_oracle_sql
Key functions in_oracle_sqlKey functions in_oracle_sql
Key functions in_oracle_sql
 
Module 3
Module 3Module 3
Module 3
 
Unit04 dbms
Unit04 dbmsUnit04 dbms
Unit04 dbms
 
4 the sql_standard
4 the  sql_standard4 the  sql_standard
4 the sql_standard
 

Recently uploaded

Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 

Recently uploaded (20)

young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 

mysql.ppt

  • 2. Learning Objectives  Read and write Data Definition grammar of SQL  Read and write data modification statements  (INSERT, UPDATE, DELETE)  Read and write basic SELECT FROM WHERE queries  Use aggregate functions
  • 3. 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)
  • 4. Part1: SQL used for Data Definition  Allows the specification of not only a set of relations but also information about each relation, including:  The schema for each relation  The domain of values associated with each attribute  Integrity constraints
  • 5. Domain Types in SQL  Similar to data types in classical programming languages Type Description CHAR(n) Fixed length character string, with specified length n VARCHAR(n) Variable length character string, with specified maximum length n INTEGER Integer (a machine-dependent finite subset of the integers) SMALLINT(n) A small integer (a finite subset of INTEGER) FLOAT(M,D) Floating point number, with total number of digits M and number of digits following the decimal point D DOUBLE(M,D) Double-precision floating point number
  • 6. CREATE DATABASE  An SQL relation is defined using the CREATE DATABASE command:  create database [database name]  Example  create database mydatabase
  • 7. CREATE TABLE  An SQL relation is defined using the CREATE TABLE command:  Create table [tablename] (A1 T1,A2 T2, … An Tn, (integrity-constraint1), …, (integrity-constraintk))  Each Ai is an attribute name in the table  Each Ti is the data type of values for Ai  Example  Create table student (flashlineID char(9) not null, name varchar(30), age integer, department varchar(20), primary key (flashlineID) ); Integrity constraint
  • 8. DROP and ALTER TABLE  The DROP TABLE command deletes all information about the dropped relation from the database  The ALTER TABLE command is used to add attributes to or remove attributes from an existing relation (table): alter table tablename actions where actions can be one of following actions: ADD Attribute DROP Attribute ADD PRIMARY KEY (Attribute_name1,…) DROP PRIMARY KEY
  • 9. Part2: Modifying the database 3 basic cases: Add a tuple INSERT INTO table_name VALUES (Val1, Val2, … , Valn) Change tuples UPDATE table_name SET A1=val1, A2=val2, …, An=valn WHERE tuple_selection_predicate Remove tuples DELETE FROM table_name WHERE tuple_selection_predicate
  • 10. INSERTION  Add a new tuple to student insert into student values(‘999999999’,’Mike’,18,’computer science’) or equivalently insert into student(flashlineID,name,age,department) values(‘999999999’,’Mike’,18,’computer science’)  Add a new tuple to student with age set to null insert into student values(‘999999999’,’Mike’,null,’computer science’)
  • 11. UPDATE  Set all department to ‘computer science’ update student set department=‘computer science’  In table account(account_number, balance, branch_name, branch_city), increase the balances of all accounts by 6% update account set balance=balance*1.06
  • 12. DELETION  Delete records of all students in the university delete from student  Delete the students who study computer science delete from student where department=‘computer science’
  • 13. Part3: Basic Query Structure  A typical SQL query has the form: select A1, A2, …, An from table1, table2, …, tablem where P  Ai represents an attribute  tablei represents a table  P is a constraints (condition)  This query is equivalent to the relational algebra expression:  Example Select flashlineID, name from student Where department=‘computer science’ )) ,..., , ( ( 2 1 ,..., , 2 1 m P A A A table table table n  
  • 14. The SELECT Clause – Duplicate tuples  Unlike pure relational algebra, SQL does not automatically remove duplicate tuples from relations or query results  To eliminate duplicates, insert the keyword distinct after select.  Example: Find the names of all students in the university, and remove duplicates select distinct name from student
  • 15. The SELECT Clause – Expressions, as  An star in the select clause denotes “all attributes” select * from student  An expression can be assigned a name using as  Example select FlashlineID as ID from student Note: as is rename clause, also can be used to rename table name select name as myname from student as S
  • 16. The WHERE Clause  The WHERE clause specifies the conditions (constraints) results satisfy  Corresponds to the selection predicate σ  Comparisons and Booleans are as follows:  Comparison operator: <, <=, >,>=, =, <>  Logical operators: and, or, not  Example  Find names of all students in computer science department with age smaller than 18 select names from student where department=‘computer science’ and age<18
  • 17. Aggregate Functions  Aggregate functions operate on the multiset of values of a attribute and return a value avg(attribute): average value min(attribute): minimum value max(attribute): maximum value sum(attribute): sum of values count(attribute): number of values  To obtain the value when duplicates are removed, insert the keyword distinct before attribute name: avg(distinct attribute)
  • 18. Aggregation: GROUP BY clause  GROUP BY attribute operate in this sequence 1. Groups the attribute set’s members into subsets by value 2. Performs the aggregate separately on each subset 3. Produces a result value for each subset  Example: list each department and its number of students select department, count(distinct name) as number from student group by department Note: if a select clause contains any aggregate functions, then all non-aggregated terms in the select clause must be used in a group by clause. Ex: department is not aggregated, so it must be in the group by clause.
  • 19. Null Values and Aggregate  The youngest student in the university select * from student where age=min(age)  Above statement ignores null amounts  Result is null if there is no non-null amount  All aggregate operations except count(*) ignore tuples with null values on the aggregated attributes.
  • 20. 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/~nruan/bank_db.s ql http://www.cs.kent.edu/~nruan/bank_dat a.sql
  • 21. 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)
  • 22. Command for accessing MySQL  Access linux server >ssh hercules.cs.kent.edu  Access MySQL >mysql –u [username] –p >password:[password]