SlideShare a Scribd company logo
COSC 2206 Internet Tools MySQL Database System Installation Overview SQL summary
2-Tier Architecture Web Browser (Client) Web Server PHP
3-Tier Architecture Web Browser (Client) Database Server Web Server PHP
SQL links Tutorials http://www.w3schools.com/sql/ http://www.sqlzoo.net   http://sqlcourse.com   (part 2) http://sqlcourse2/com  (part 1)  MySQL online reference manual http://dev.mysql.com/doc/mysql/en/Reference.html
Installation Summary More detailed installation instructions are given on the CD Install MySQL in  c:\mysql MySQL can be installed as a service (Win 2000/XP) Can make icons on the desktop for starting and stopping the server.
Command Line Client The standard command line client is c:\mysql\bin\mysql.exe The command line client can be used to send commands and SQL queries to the MySQL server There are also GUI clients such as MyCC
Client-Server Interaction MySQL Server Client Program Make a request (SQL query) Get results Client program can be a MySQL command line client, GUI client, or a program written in any language such as C, Perl, PHP, Java that has an interface to the MySQL server.
Connecting to the Server Use a command prompt that sets the path to  c:\mysql\bin The following command connects to the server: mysql -u root -p you are prompted for the root password. you can now send comands and SQL statements to the server
WARNING WARNING WARNING Always assume that everything is case sensitive, especially table names. This is not the case in Windows XP but it is the case in Linux
Entering commands (1) Show all the databases SHOW DATABASES; mysql> SHOW DATABASES; +-------------+ | Database  | +-------------+ | bookstore  | | employee_db | | mysql  | | student_db  | | test  | | web_db  | +-------------+
Entering commands (2) Choosing a database and showing its tables USE test; SHOW tables; mysql> USE test; Database changed mysql> SHOW tables; +----------------+ | Tables_in_test | +----------------+ | books  | | name2  | | names  | | test  | +----------------+ 4 rows in set (0.00 sec) mysql>
Entering commands (3) Show the structure of a table DESCRIBE names; mysql> DESCRIBE names; +-----------+-------------+------+-----+---------+----------------+ | Field  | Type  | Null | Key | Default | Extra  | +-----------+-------------+------+-----+---------+----------------+ | id  | int(11)  |  | PRI | NULL  | auto_increment | | firstName | varchar(20) |  |  |  |  | | lastName  | varchar(20) |  |  |  |  | +-----------+-------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec) mysql>
Entering commands (4) Show the rows of a table (all columns) SELECT * FROM names; mysql> SELECT * FROM names; +----+-----------+------------+ | id | firstName | lastName  | +----+-----------+------------+ |  1 | Fred  | Flintstone | |  2 | Barney  | Rubble  | +----+-----------+------------+ 2 rows in set (0.00 sec) mysql>
Entering commands (5) Inserting a new record INSERT INTO names (firstName, lastName) VALUES ('Rock','Quarry'); SELECT * FROM names; mysql> INSERT INTO names (firstName, lastName) VALUES ('Ralph', 'Quarry'); Query OK, 1 row affected (0.02 sec) mysql> SELECT * FROM names; +----+-----------+------------+ | id | firstName | lastName  | +----+-----------+------------+ |  1 | Fred  | Flintstone | |  2 | Barney  | Rubble  | |  3 | Ralph  | Quarry  | +----+-----------+------------+ 3 rows in set (0.00 sec) mysql>
Entering commands (6) Updating a record UPDATE names SET lastName = 'Stone' WHERE id=3; SELECT * FROM names; mysql> UPDATE names SET lastName = 'Stone' WHERE id=3; Query OK, 1 row affected (0.28 sec) Rows matched: 1  Changed: 1  Warnings: 0 mysql> SELECT * FROM names; +----+-----------+------------+ | id | firstName | lastName  | +----+-----------+------------+ |  1 | Fred  | Flintstone | |  2 | Barney  | Rubble  | |  3 | Ralph  | Stone  | +----+-----------+------------+ 3 rows in set (0.00 sec) mysql>
Logging output The commands you type and their ouput can be logged to a file by using the following command inside the MySQL command line client tee log.txt Here log.txt is the name of the file
Executing SQL files (1) It is usually better to use an editor to write an SQL script and send it to the server. A file of SQL commands such as  books.sql  can be executed by the server by using a command such as C:\mysql\bin\mysql -u root -p < books.sql This assumes that  books.sql  is in your current directory. Otherwise the complete path to  books.sql  must be supplied
Executing SQL files (2) A file of SQL commands such as  books.sql  can also be executed from inside the MySQL client using the  source  command source c:\.....\books.sql Here the full path to  books.sql  should be used.
Documentation MySQL comes with a tutorial and complete documentation in a HUGE file: c:\mysql\Docs\manual.html Table of contents with links: c:\mysql\Docs\manual_toc.html Use this file to locate the link to the topic you are interested in.
Database concepts (1) A relational database management system consists of a number of databases. Each database consists of a number of tables. Example table isbn title author pub year price books table rows (records) column headings
Some SQL data types (1) Each entry in a row has a type specified by the column. Numeric data types TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT FLOAT(display_length, decimals) DOUBLE(display_length, decimals) DECIMAL(display_length, decimals) NUMERIC  is the same as  DECIMAL
Some SQL data types (2) Date and time types DATE format is YYYY-MM-DD DATETIME format YYYY-MM-DD HH:MM:SS TIMESTAMP format YYYYMMDDHHMMSS TIME format HH:MM:SS YEAR default length is 4
SQL data types (3) String types CHAR fixed length string, e.g.,  CHAR(20) VARCHAR variable length string, e.g., VARCHAR(20) BLOB, TINYBLOB, MEDIUMBLOB, LONGBLOB same as  TEXT ,  TINYTEXT  ... ENUM list of items from which value is selected
SQL commands SHOW, USE SHOW Display databases or tables in current database; Example (command line client): show databases; show tables; USE Specify which database to use Example use bookstore;
The CREATE Command (1) CREATE  creates a database table CREATE TABLE table_name (   column_name1 column_type1,   column_name2 column_type2,   ...   column_nameN column_typeN ); Note: To create a database use the statement CREATE db_name;
The CREATE Command (2) Specifying primary keys CREATE TABLE table_name (   column_name1 column_type1 NOT NULL   DEFAULT '0',   column_name2 column_type2,   ...   column_nameN column_typeN,   PRIMARY KEY (column_name1) );
The CREATE Command (3) autoincrement primary integer keys CREATE TABLE table_name (   column_name1 column_type1 PRIMARY   KEY NOT NULL DEFAULT '0'   AUTO_INCREMENT,   column_name2 column_type2,   ...   column_nameN column_typeN, );
The CREATE Command (4) Can also create UNIQUE keys. They are similar to PRIMARY KEYS but can have NULL values. Can also create INDEX fields.
Conditional Creation Conditional database creation CREATE DATABASE IF NOT EXISTS db_name; Conditional table creation CREATE TABLE IF NOT EXISTS table_name;
The DROP Command To delete databases and tables use the DROP command Examples DROP DATABASE db_name; DROP DATABASE IF EXISTS db_name; DROP TABLE table_name; DROP TABLE IF EXISTS table_name; Note: Don't confuse  DROP  with  DELETE  which deletes rows of a table.
The INSERT Command Inserting rows into a table INSERT INTO table_name   ( col_1, col_2, ..., col_N) VALUES   ( val_1, val_2, ..., val_N); String values are enclosed in single quotes by default but double quotes are also allowed. Literal quotes need to be escaped using \' and \&quot;
The SELECT Command (1) Selecting rows from a table Simplest form: select all columns Select specified columns Conditional selection of rows SELECT column_list FROM table_name; SELECT * FROM table_name; SELECT column_list FROM table_name WHERE condition;
The SELECT Command (2) Specifying ascending row ordering Specifying descending row ordering SELECT column_list FROM table_name WHERE condition ORDER by ASC; SELECT column_list FROM table_name WHERE condition ORDER by DESC;
The SELECT Command (3) There are many other variations of the select command. Example: finding the number of records in a table assuming a primary key called  id : Can also perform searching using the WHERE option SELECT COUNT(id) FROM table_name
The UPDATE Command Used to modify an existing record Conditional update version UPDATE table_name SET col_1 = 'new_value1', ..., col_n = 'new_value2'; UPDATE table_name SET col_1 = 'new_value1', ..., col_n = 'new_value2' WHERE condition;
marks.sql (1) studentID first_name USE  test; CREATE TABLE  marks (   studentID SMALLINT AUTO_INCREMENT  NOT NULL ,   first_name  VARCHAR (20)  NOT NULL ,   last_name  VARCHAR (20)  NOT NULL ,   mark  SMALLINT  DEFAULT 0  NOT NULL ,   PRIMARY KEY (studentID) ); marks table last_name mark
marks.sql (2)  -- Insert some rows into marks table INSERT INTO  marks (first_name, last_name,   mark)  VALUES  ('Fred', 'Jones', 78); INSERT INTO  marks (first_name, last_name,   mark)  VALUES  ('Bill', 'James', 67); INSERT INTO  marks (first_name, last_name,   mark)  VALUES  ('Carol', 'Smith', 82); INSERT INTO  marks (first_name, last_name,   mark)  VALUES  ('Bob', 'Duncan', 60); INSERT INTO  marks (first_name, last_name,   mark)  VALUES  ('Joan', 'Davis', 86);
Executing The Script within MySQL use a command such as source  c:/.........../marks.sql This adds the  marks  table to the  test  database
The Marks Table  Selecting the complete table SELECT  *  FROM  marks; +-----------+------------+-----------+------+ | studentID | first_name | last_name | mark | +-----------+------------+-----------+------+ |  1 | Fred  | Jones  |  78 | |  2 | Bill  | James  |  67 | |  3 | Carol  | Smith  |  82 | |  4 | Bob  | Duncan  |  60 | |  5 | Joan  | Davis  |  86 | +-----------+------------+-----------+------+ 5 rows in set (0.00 sec)
The WHERE Clause (1) Select rows according to some criterion SELECT  *  FROM  marks  WHERE  studentID > 1   AND  studentID < 5; +-----------+------------+-----------+------+ | studentID | first_name | last_name | mark | +-----------+------------+-----------+------+ |  2 | Bill  | James  |  67 | |  3 | Carol  | Smith  |  82 | |  4 | Bob  | Duncan  |  60 | +-----------+------------+-----------+------+ 3 rows in set (0.01 sec)
The WHERE Clause (2) Select rows with marks >= 80 SELECT  *  FROM  marks  WHERE  mark >= 80; +-----------+------------+-----------+------+ | studentID | first_name | last_name | mark | +-----------+------------+-----------+------+ |  3 | Carol  | Smith  |  82 | |  5 | Joan  | Davis  |  86 | +-----------+------------+-----------+------+ 2 rows in set (0.00 sec)
The ORDER BY Clause Select rows according to some criterion SELECT  *  FROM  marks  ORDER BY  mark  DESC ; +-----------+------------+-----------+------+ | studentID | first_name | last_name | mark | +-----------+------------+-----------+------+ |  5 | Joan  | Davis  |  86 | |  3 | Carol  | Smith  |  82 | |  1 | Fred  | Jones  |  78 | |  2 | Bill  | James  |  67 | |  4 | Bob  | Duncan  |  60 | +-----------+------------+-----------+------+ 5 rows in set (0.00 sec)
Searching Using LIKE (1) LIKE is used to search a table for values containing a search string: There are two wild-card characters used to specifiy patterns: _ matches a single character % matches zero or more characters Can also use NOT LIKE Searching is case insensitive
Searching Using LIKE (2) Example: last names in marks table that begin with J Example: first names that have 3 letters SELECT  *  FROM  marks  WHERE  last_name   LIKE   'J%' ; SELECT  *  FROM  marks  WHERE  first_name   LIKE   '_ _ _' ;
Quoting strings If a string contains a single quote it must be backquoted (escaped) before it can be used in a query Example: find records containing  O'Reilly  in the  last_name  field. SELECT  *  FROM  marks  WHERE  last_name   =  'O\'Reilly' ;
Limiting number of rows LIMIT  can be used to specify the maximum number of rows that are to be returned by a select query. Example SELECT  *  FROM  marks  LIMIT  3; This query will return only the first 3 rows from the  marks  table To return 15 rows beginning at row 5 use SELECT  *  FROM  marks  LIMIT  4, 15;
MySQL Functions (1) How many rows are there ? Can use  COUNT (marks)  instead of  COUNT (*) SELECT   COUNT (*)  FROM  marks; +----------+ | COUNT(*) | +----------+ |  5 | +----------+ 1 row in set (0.00 sec)
MySQL Functions (2) What is the sum of all the marks? SELECT   SUM (mark)  FROM  marks; +-----------+ | SUM(mark) | +-----------+ |  373 | +-----------+ 1 row in set (0.00 sec)
MySQL Functions (3) What is the average mark? SELECT   AVG (mark)  FROM  marks; +-----------+ | AVG(mark) | +-----------+ |  74.6000 | +-----------+ 1 row in set (0.00 sec)
MySQL Functions (4) What is the minimum mark? SELECT   MIN (mark)  FROM  marks; +-----------+ | MIN(mark) | +-----------+ |  60 | +-----------+ 1 row in set (0.00 sec)
MySQL Functions (5) What is the maximum mark? SELECT   MAX (mark)  FROM  marks; +-----------+ | MAX(mark) | +-----------+ |  86 | +-----------+ 1 row in set (0.00 sec)
books.sql (1) USE  web_db; CREATE TABLE  books (   isbn  CHAR (15)  PRIMARY KEY NOT NULL ,   title  VARCHAR (100)  NOT NULL ,   author  VARCHAR (100)  NOT NULL ,   pub  VARCHAR (20)  NOT NULL ,   year YEAR NOT NULL,   price  DECIMAL (9,2)  DEFAULT NULL ); books table this is a simple design isbn title author pub year price
books.sql (2)  -- Insert some books into books table INSERT INTO  books  VALUES  ( '0-672-31784-2' ,   'PHP and MySQL Web Development' ,   'Luke Welling, Laura Thomson' ,   'Sams' , 2001, 74.95 ); INSERT INTO  books  VALUES  ( '1-861003-02-1' ,   'Professional Apache' ,   'Peter Wainwright' ,   'Wrox Press Ltd' , 1999, 74.95 );
Executing The Script within MySQL use a command such as source  c:/.........../books.sql This adds the books table to the  web_db  database
employee_db.sql (1) CREATE DATABASE IF NOT EXISTS  employee_db; USE  employee_db; DROP TABLE IF EXISTS  employees; DROP TABLE IF EXITS  jobs; employees table jobs table employeeID name position address employeeID hours
employee_db.sql (1) CREATE TABLE  employees (   employeeID  SMALLINT NOT NULL ,   name  VARCHAR (20)  NOT NULL ,   position  VARCHAR (20)  NOT NULL ,   address  VARCHAR (40)  NOT NULL ,   PRIMARY KEY  (employeeID) ); INSERT INTO  employees  VALUES  (1001, 'Fred',   'programmer', '13 Windle St'); INSERT INTO  employees  VALUES  (1002, 'Joan',   'programmer', '23 Rock St'); INSERT INTO  employees  VALUES  (1003, 'Bill',   'manager', '37 Front St');
employee_db.sql (2) CREATE TABLE  jobs (   employeeID  SMALLINT NOT NULL ,   hours  DECIMAL (5,2)  NOT NULL , ); INSERT INTO  jobs  VALUES  (1001, 13.5); INSERT INTO  jobs  VALUES  (1002, 2); INSERT INTO  jobs  VALUES  (1002, 6.25); INSERT INTO  jobs  VALUES  (1003, 4); INSERT INTO  jobs  VALUES  (1001, 1); INSERT INTO  jobs  VALUES  (1003, 7); INSERT INTO  jobs  VALUES  (1003, 9.5);
Executing The Script within MySQL use a command such as source  c:/......./employee_db.sql This creates the  employee_db  database and adds the employees and jobs tables to it
Select Queries With Joins (1) Cartesian product query SELECT  *  FROM  employees, jobs; +------------+------+------------+--------------+------------+-------+ | employeeID | name | position  | address  | employeeID | hours | +------------+------+------------+--------------+------------+-------+ |  1001 | Fred | programmer | 13 Windle St |  1001 | 13.50 | |  1002 | Joan | programmer | 23 Rock St  |  1001 | 13.50 | |  1003 | Bill | manager  | 37 Front St  |  1001 | 13.50 | |  1001 | Fred | programmer | 13 Windle St |  1002 |  2.00 | |  1002 | Joan | programmer | 23 Rock St  |  1002 |  2.00 | |  1003 | Bill | manager  | 37 Front St  |  1002 |  2.00 | |  1001 | Fred | programmer | 13 Windle St |  1002 |  6.25 | |  1002 | Joan | programmer | 23 Rock St  |  1002 |  6.25 | |  1003 | Bill | manager  | 37 Front St  |  1002 |  6.25 |
Select Queries With Joins (2) Cartesian product query (continued) |  1001 | Fred | programmer | 13 Windle St |  1003 |  4.00 | |  1002 | Joan | programmer | 23 Rock St  |  1003 |  4.00 | |  1003 | Bill | manager  | 37 Front St  |  1003 |  4.00 | |  1001 | Fred | programmer | 13 Windle St |  1001 |  1.00 | |  1002 | Joan | programmer | 23 Rock St  |  1001 |  1.00 | |  1003 | Bill | manager  | 37 Front St  |  1001 |  1.00 | |  1001 | Fred | programmer | 13 Windle St |  1003 |  7.00 | |  1002 | Joan | programmer | 23 Rock St  |  1003 |  7.00 | |  1003 | Bill | manager  | 37 Front St  |  1003 |  7.00 | |  1001 | Fred | programmer | 13 Windle St |  1003 |  9.50 | |  1002 | Joan | programmer | 23 Rock St  |  1003 |  9.50 | |  1003 | Bill | manager  | 37 Front St  |  1003 |  9.50 | +------------+------+------------+--------------+------------+-------+ 21 rows in set (0.01 sec) The cartesian product query is rarely what we want.
Select Queries With Joins (3) Substitution +------+-------+ | name | hours | +------+-------+ | Fred | 13.50 | | Joan |  2.00 | | Joan |  6.25 | | Bill |  4.00 | | Fred |  1.00 | | Bill |  7.00 | | Bill |  9.50 | +------+-------+ 7 rows in set (0.00 sec) Here we are replacing the employeeID numbers in the jobs table by the employee's name SELECT  name, hours  FROM  employees, jobs  WHERE employees.employeeID = jobs.employeeID;
Select Queries With Joins (4) Entries only for Fred +------+-------+ | name | hours | +------+-------+ | Fred | 13.50 | | Fred |  1.00 | +------+-------+ 2 rows in set (0.00 sec) SELECT  name, hours  FROM  employees, jobs  WHERE employees.employeeID = jobs.employeeID AND name = 'Fred';
Select Queries With Joins (5) Total hours worked for each person +------+------------+ | name | SUM(hours) | +------+------------+ | Bill |  20.50 | | Fred |  14.50 | | Joan |  8.25 | +------+------------+ 3 rows in set (0.00 sec) SELECT  name, SUM(hours)  FROM  employees, jobs WHERE  employees.employeeID = jobs.employeeID GROUP BY  name;
Select Queries With Joins (6) Total hours worked, for Fred +------+------------+ | name | SUM(hours) | +------+------------+ | Fred |  14.50 | +------+------------+ 1 row in set (0.00 sec) SELECT  name, SUM(hours)  FROM  employees, jobs WHERE  employees.employeeID = jobs.employeeID AND name = 'Fred'  GROUP BY  name;

More Related Content

What's hot (20)

Sq lite database
Sq lite databaseSq lite database
Sq lite database
AYESHA JAVED
 
Sql Server Basics
Sql Server BasicsSql Server Basics
Sql Server Basics
rainynovember12
 
MySQL Tutorial For Beginners | Relational Database Management System | MySQL ...
MySQL Tutorial For Beginners | Relational Database Management System | MySQL ...MySQL Tutorial For Beginners | Relational Database Management System | MySQL ...
MySQL Tutorial For Beginners | Relational Database Management System | MySQL ...
Edureka!
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
Md.Mojibul Hoque
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
MySQL and its basic commands
MySQL and its basic commandsMySQL and its basic commands
MySQL and its basic commands
Bwsrang Basumatary
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
farwa waqar
 
Mysql
MysqlMysql
Mysql
TSUBHASHRI
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
Raveena Thakur
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
Stewart Rogers
 
Introduction to triggers
Introduction to triggersIntroduction to triggers
Introduction to triggers
Command Prompt., Inc
 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In Sql
Anurag
 
Sql select
Sql select Sql select
Sql select
Mudasir Syed
 
Object Relational Database Management System(ORDBMS)
Object Relational Database Management System(ORDBMS)Object Relational Database Management System(ORDBMS)
Object Relational Database Management System(ORDBMS)
Rabin BK
 
Stored procedure
Stored procedureStored procedure
Stored procedure
baabtra.com - No. 1 supplier of quality freshers
 
SQL commands
SQL commandsSQL commands
SQL commands
GirdharRatne
 
MS-SQL SERVER ARCHITECTURE
MS-SQL SERVER ARCHITECTUREMS-SQL SERVER ARCHITECTURE
MS-SQL SERVER ARCHITECTURE
Douglas Bernardini
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
NITISH KUMAR
 
Structured Query Language (SQL)
Structured Query Language (SQL)Structured Query Language (SQL)
Structured Query Language (SQL)
Syed Hassan Ali
 
Sql commands
Sql commandsSql commands
Sql commands
Balakumaran Arunachalam
 

Similar to MySql slides (ppt) (20)

mysqlHiep.ppt
mysqlHiep.pptmysqlHiep.ppt
mysqlHiep.ppt
webhostingguy
 
MySQL Database System Hiep Dinh
MySQL Database System Hiep DinhMySQL Database System Hiep Dinh
MySQL Database System Hiep Dinh
webhostingguy
 
SQl data base management and design
SQl     data base management  and designSQl     data base management  and design
SQl data base management and design
franckelsania20
 
My sql with querys
My sql with querysMy sql with querys
My sql with querys
NIRMAL FELIX
 
MySQL Presentation
MySQL PresentationMySQL Presentation
MySQL Presentation
Manish Bothra
 
My sql.ppt
My sql.pptMy sql.ppt
My sql.ppt
MAGNA COLLEGE OF ENGINEERING
 
SQL Tutorial for BCA-2
SQL Tutorial for BCA-2SQL Tutorial for BCA-2
SQL Tutorial for BCA-2
Raj vardhan
 
MYSQL
MYSQLMYSQL
MYSQL
Ankush Jain
 
chapter 8 SQL.ppt
chapter 8 SQL.pptchapter 8 SQL.ppt
chapter 8 SQL.ppt
YitbarekMurche
 
My sql Syntax
My sql SyntaxMy sql Syntax
My sql Syntax
Reka
 
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptxhjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
EliasPetros
 
Les10 Creating And Managing Tables
Les10 Creating And Managing TablesLes10 Creating And Managing Tables
Les10 Creating And Managing Tables
NETsolutions Asia: NSA – Thailand, Sripatum University: SPU
 
MYSQL
MYSQLMYSQL
MYSQL
ARJUN
 
database-querry-student-note
database-querry-student-notedatabase-querry-student-note
database-querry-student-note
Leerpiny Makouach
 
Interactive SQL: SQL, Features of SQL, DDL & DML
Interactive SQL: SQL, Features of SQL,  DDL & DMLInteractive SQL: SQL, Features of SQL,  DDL & DML
Interactive SQL: SQL, Features of SQL, DDL & DML
IsakkiDeviP
 
LECTURE NOTES.pdf
LECTURE NOTES.pdfLECTURE NOTES.pdf
LECTURE NOTES.pdf
ChryslerPanaguiton
 
LECTURE NOTES.pdf
LECTURE NOTES.pdfLECTURE NOTES.pdf
LECTURE NOTES.pdf
ChryslerPanaguiton
 
Introduction databases and MYSQL
Introduction databases and MYSQLIntroduction databases and MYSQL
Introduction databases and MYSQL
Naeem Junejo
 
PHP mysql Introduction database
 PHP mysql  Introduction database PHP mysql  Introduction database
PHP mysql Introduction database
Mudasir Syed
 
Module 3
Module 3Module 3
Module 3
cs19club
 
Ad

More from webhostingguy (20)

File Upload
File UploadFile Upload
File Upload
webhostingguy
 
Running and Developing Tests with the Apache::Test Framework
Running and Developing Tests with the Apache::Test FrameworkRunning and Developing Tests with the Apache::Test Framework
Running and Developing Tests with the Apache::Test Framework
webhostingguy
 
MySQL and memcached Guide
MySQL and memcached GuideMySQL and memcached Guide
MySQL and memcached Guide
webhostingguy
 
Novell® iChain® 2.3
Novell® iChain® 2.3Novell® iChain® 2.3
Novell® iChain® 2.3
webhostingguy
 
Load-balancing web servers Load-balancing web servers
Load-balancing web servers Load-balancing web serversLoad-balancing web servers Load-balancing web servers
Load-balancing web servers Load-balancing web servers
webhostingguy
 
SQL Server 2008 Consolidation
SQL Server 2008 ConsolidationSQL Server 2008 Consolidation
SQL Server 2008 Consolidation
webhostingguy
 
What is mod_perl?
What is mod_perl?What is mod_perl?
What is mod_perl?
webhostingguy
 
What is mod_perl?
What is mod_perl?What is mod_perl?
What is mod_perl?
webhostingguy
 
Master Service Agreement
Master Service AgreementMaster Service Agreement
Master Service Agreement
webhostingguy
 
Notes8
Notes8Notes8
Notes8
webhostingguy
 
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
webhostingguy
 
Dell Reference Architecture Guide Deploying Microsoft® SQL ...
Dell Reference Architecture Guide Deploying Microsoft® SQL ...Dell Reference Architecture Guide Deploying Microsoft® SQL ...
Dell Reference Architecture Guide Deploying Microsoft® SQL ...
webhostingguy
 
Managing Diverse IT Infrastructure
Managing Diverse IT InfrastructureManaging Diverse IT Infrastructure
Managing Diverse IT Infrastructure
webhostingguy
 
Web design for business.ppt
Web design for business.pptWeb design for business.ppt
Web design for business.ppt
webhostingguy
 
IT Power Management Strategy
IT Power Management Strategy IT Power Management Strategy
IT Power Management Strategy
webhostingguy
 
Excel and SQL Quick Tricks for Merchandisers
Excel and SQL Quick Tricks for MerchandisersExcel and SQL Quick Tricks for Merchandisers
Excel and SQL Quick Tricks for Merchandisers
webhostingguy
 
OLUG_xen.ppt
OLUG_xen.pptOLUG_xen.ppt
OLUG_xen.ppt
webhostingguy
 
Parallels Hosting Products
Parallels Hosting ProductsParallels Hosting Products
Parallels Hosting Products
webhostingguy
 
Microsoft PowerPoint presentation 2.175 Mb
Microsoft PowerPoint presentation 2.175 MbMicrosoft PowerPoint presentation 2.175 Mb
Microsoft PowerPoint presentation 2.175 Mb
webhostingguy
 
Reseller's Guide
Reseller's GuideReseller's Guide
Reseller's Guide
webhostingguy
 
Running and Developing Tests with the Apache::Test Framework
Running and Developing Tests with the Apache::Test FrameworkRunning and Developing Tests with the Apache::Test Framework
Running and Developing Tests with the Apache::Test Framework
webhostingguy
 
MySQL and memcached Guide
MySQL and memcached GuideMySQL and memcached Guide
MySQL and memcached Guide
webhostingguy
 
Novell® iChain® 2.3
Novell® iChain® 2.3Novell® iChain® 2.3
Novell® iChain® 2.3
webhostingguy
 
Load-balancing web servers Load-balancing web servers
Load-balancing web servers Load-balancing web serversLoad-balancing web servers Load-balancing web servers
Load-balancing web servers Load-balancing web servers
webhostingguy
 
SQL Server 2008 Consolidation
SQL Server 2008 ConsolidationSQL Server 2008 Consolidation
SQL Server 2008 Consolidation
webhostingguy
 
Master Service Agreement
Master Service AgreementMaster Service Agreement
Master Service Agreement
webhostingguy
 
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...PHP and MySQL PHP Written as a set of CGI binaries in C in ...
PHP and MySQL PHP Written as a set of CGI binaries in C in ...
webhostingguy
 
Dell Reference Architecture Guide Deploying Microsoft® SQL ...
Dell Reference Architecture Guide Deploying Microsoft® SQL ...Dell Reference Architecture Guide Deploying Microsoft® SQL ...
Dell Reference Architecture Guide Deploying Microsoft® SQL ...
webhostingguy
 
Managing Diverse IT Infrastructure
Managing Diverse IT InfrastructureManaging Diverse IT Infrastructure
Managing Diverse IT Infrastructure
webhostingguy
 
Web design for business.ppt
Web design for business.pptWeb design for business.ppt
Web design for business.ppt
webhostingguy
 
IT Power Management Strategy
IT Power Management Strategy IT Power Management Strategy
IT Power Management Strategy
webhostingguy
 
Excel and SQL Quick Tricks for Merchandisers
Excel and SQL Quick Tricks for MerchandisersExcel and SQL Quick Tricks for Merchandisers
Excel and SQL Quick Tricks for Merchandisers
webhostingguy
 
Parallels Hosting Products
Parallels Hosting ProductsParallels Hosting Products
Parallels Hosting Products
webhostingguy
 
Microsoft PowerPoint presentation 2.175 Mb
Microsoft PowerPoint presentation 2.175 MbMicrosoft PowerPoint presentation 2.175 Mb
Microsoft PowerPoint presentation 2.175 Mb
webhostingguy
 
Ad

MySql slides (ppt)

  • 1. COSC 2206 Internet Tools MySQL Database System Installation Overview SQL summary
  • 2. 2-Tier Architecture Web Browser (Client) Web Server PHP
  • 3. 3-Tier Architecture Web Browser (Client) Database Server Web Server PHP
  • 4. SQL links Tutorials http://www.w3schools.com/sql/ http://www.sqlzoo.net http://sqlcourse.com (part 2) http://sqlcourse2/com (part 1) MySQL online reference manual http://dev.mysql.com/doc/mysql/en/Reference.html
  • 5. Installation Summary More detailed installation instructions are given on the CD Install MySQL in c:\mysql MySQL can be installed as a service (Win 2000/XP) Can make icons on the desktop for starting and stopping the server.
  • 6. Command Line Client The standard command line client is c:\mysql\bin\mysql.exe The command line client can be used to send commands and SQL queries to the MySQL server There are also GUI clients such as MyCC
  • 7. Client-Server Interaction MySQL Server Client Program Make a request (SQL query) Get results Client program can be a MySQL command line client, GUI client, or a program written in any language such as C, Perl, PHP, Java that has an interface to the MySQL server.
  • 8. Connecting to the Server Use a command prompt that sets the path to c:\mysql\bin The following command connects to the server: mysql -u root -p you are prompted for the root password. you can now send comands and SQL statements to the server
  • 9. WARNING WARNING WARNING Always assume that everything is case sensitive, especially table names. This is not the case in Windows XP but it is the case in Linux
  • 10. Entering commands (1) Show all the databases SHOW DATABASES; mysql> SHOW DATABASES; +-------------+ | Database | +-------------+ | bookstore | | employee_db | | mysql | | student_db | | test | | web_db | +-------------+
  • 11. Entering commands (2) Choosing a database and showing its tables USE test; SHOW tables; mysql> USE test; Database changed mysql> SHOW tables; +----------------+ | Tables_in_test | +----------------+ | books | | name2 | | names | | test | +----------------+ 4 rows in set (0.00 sec) mysql>
  • 12. Entering commands (3) Show the structure of a table DESCRIBE names; mysql> DESCRIBE names; +-----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+----------------+ | id | int(11) | | PRI | NULL | auto_increment | | firstName | varchar(20) | | | | | | lastName | varchar(20) | | | | | +-----------+-------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec) mysql>
  • 13. Entering commands (4) Show the rows of a table (all columns) SELECT * FROM names; mysql> SELECT * FROM names; +----+-----------+------------+ | id | firstName | lastName | +----+-----------+------------+ | 1 | Fred | Flintstone | | 2 | Barney | Rubble | +----+-----------+------------+ 2 rows in set (0.00 sec) mysql>
  • 14. Entering commands (5) Inserting a new record INSERT INTO names (firstName, lastName) VALUES ('Rock','Quarry'); SELECT * FROM names; mysql> INSERT INTO names (firstName, lastName) VALUES ('Ralph', 'Quarry'); Query OK, 1 row affected (0.02 sec) mysql> SELECT * FROM names; +----+-----------+------------+ | id | firstName | lastName | +----+-----------+------------+ | 1 | Fred | Flintstone | | 2 | Barney | Rubble | | 3 | Ralph | Quarry | +----+-----------+------------+ 3 rows in set (0.00 sec) mysql>
  • 15. Entering commands (6) Updating a record UPDATE names SET lastName = 'Stone' WHERE id=3; SELECT * FROM names; mysql> UPDATE names SET lastName = 'Stone' WHERE id=3; Query OK, 1 row affected (0.28 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> SELECT * FROM names; +----+-----------+------------+ | id | firstName | lastName | +----+-----------+------------+ | 1 | Fred | Flintstone | | 2 | Barney | Rubble | | 3 | Ralph | Stone | +----+-----------+------------+ 3 rows in set (0.00 sec) mysql>
  • 16. Logging output The commands you type and their ouput can be logged to a file by using the following command inside the MySQL command line client tee log.txt Here log.txt is the name of the file
  • 17. Executing SQL files (1) It is usually better to use an editor to write an SQL script and send it to the server. A file of SQL commands such as books.sql can be executed by the server by using a command such as C:\mysql\bin\mysql -u root -p < books.sql This assumes that books.sql is in your current directory. Otherwise the complete path to books.sql must be supplied
  • 18. Executing SQL files (2) A file of SQL commands such as books.sql can also be executed from inside the MySQL client using the source command source c:\.....\books.sql Here the full path to books.sql should be used.
  • 19. Documentation MySQL comes with a tutorial and complete documentation in a HUGE file: c:\mysql\Docs\manual.html Table of contents with links: c:\mysql\Docs\manual_toc.html Use this file to locate the link to the topic you are interested in.
  • 20. Database concepts (1) A relational database management system consists of a number of databases. Each database consists of a number of tables. Example table isbn title author pub year price books table rows (records) column headings
  • 21. Some SQL data types (1) Each entry in a row has a type specified by the column. Numeric data types TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT FLOAT(display_length, decimals) DOUBLE(display_length, decimals) DECIMAL(display_length, decimals) NUMERIC is the same as DECIMAL
  • 22. Some SQL data types (2) Date and time types DATE format is YYYY-MM-DD DATETIME format YYYY-MM-DD HH:MM:SS TIMESTAMP format YYYYMMDDHHMMSS TIME format HH:MM:SS YEAR default length is 4
  • 23. SQL data types (3) String types CHAR fixed length string, e.g., CHAR(20) VARCHAR variable length string, e.g., VARCHAR(20) BLOB, TINYBLOB, MEDIUMBLOB, LONGBLOB same as TEXT , TINYTEXT ... ENUM list of items from which value is selected
  • 24. SQL commands SHOW, USE SHOW Display databases or tables in current database; Example (command line client): show databases; show tables; USE Specify which database to use Example use bookstore;
  • 25. The CREATE Command (1) CREATE creates a database table CREATE TABLE table_name ( column_name1 column_type1, column_name2 column_type2, ... column_nameN column_typeN ); Note: To create a database use the statement CREATE db_name;
  • 26. The CREATE Command (2) Specifying primary keys CREATE TABLE table_name ( column_name1 column_type1 NOT NULL DEFAULT '0', column_name2 column_type2, ... column_nameN column_typeN, PRIMARY KEY (column_name1) );
  • 27. The CREATE Command (3) autoincrement primary integer keys CREATE TABLE table_name ( column_name1 column_type1 PRIMARY KEY NOT NULL DEFAULT '0' AUTO_INCREMENT, column_name2 column_type2, ... column_nameN column_typeN, );
  • 28. The CREATE Command (4) Can also create UNIQUE keys. They are similar to PRIMARY KEYS but can have NULL values. Can also create INDEX fields.
  • 29. Conditional Creation Conditional database creation CREATE DATABASE IF NOT EXISTS db_name; Conditional table creation CREATE TABLE IF NOT EXISTS table_name;
  • 30. The DROP Command To delete databases and tables use the DROP command Examples DROP DATABASE db_name; DROP DATABASE IF EXISTS db_name; DROP TABLE table_name; DROP TABLE IF EXISTS table_name; Note: Don't confuse DROP with DELETE which deletes rows of a table.
  • 31. The INSERT Command Inserting rows into a table INSERT INTO table_name ( col_1, col_2, ..., col_N) VALUES ( val_1, val_2, ..., val_N); String values are enclosed in single quotes by default but double quotes are also allowed. Literal quotes need to be escaped using \' and \&quot;
  • 32. The SELECT Command (1) Selecting rows from a table Simplest form: select all columns Select specified columns Conditional selection of rows SELECT column_list FROM table_name; SELECT * FROM table_name; SELECT column_list FROM table_name WHERE condition;
  • 33. The SELECT Command (2) Specifying ascending row ordering Specifying descending row ordering SELECT column_list FROM table_name WHERE condition ORDER by ASC; SELECT column_list FROM table_name WHERE condition ORDER by DESC;
  • 34. The SELECT Command (3) There are many other variations of the select command. Example: finding the number of records in a table assuming a primary key called id : Can also perform searching using the WHERE option SELECT COUNT(id) FROM table_name
  • 35. The UPDATE Command Used to modify an existing record Conditional update version UPDATE table_name SET col_1 = 'new_value1', ..., col_n = 'new_value2'; UPDATE table_name SET col_1 = 'new_value1', ..., col_n = 'new_value2' WHERE condition;
  • 36. marks.sql (1) studentID first_name USE test; CREATE TABLE marks ( studentID SMALLINT AUTO_INCREMENT NOT NULL , first_name VARCHAR (20) NOT NULL , last_name VARCHAR (20) NOT NULL , mark SMALLINT DEFAULT 0 NOT NULL , PRIMARY KEY (studentID) ); marks table last_name mark
  • 37. marks.sql (2) -- Insert some rows into marks table INSERT INTO marks (first_name, last_name, mark) VALUES ('Fred', 'Jones', 78); INSERT INTO marks (first_name, last_name, mark) VALUES ('Bill', 'James', 67); INSERT INTO marks (first_name, last_name, mark) VALUES ('Carol', 'Smith', 82); INSERT INTO marks (first_name, last_name, mark) VALUES ('Bob', 'Duncan', 60); INSERT INTO marks (first_name, last_name, mark) VALUES ('Joan', 'Davis', 86);
  • 38. Executing The Script within MySQL use a command such as source c:/.........../marks.sql This adds the marks table to the test database
  • 39. The Marks Table Selecting the complete table SELECT * FROM marks; +-----------+------------+-----------+------+ | studentID | first_name | last_name | mark | +-----------+------------+-----------+------+ | 1 | Fred | Jones | 78 | | 2 | Bill | James | 67 | | 3 | Carol | Smith | 82 | | 4 | Bob | Duncan | 60 | | 5 | Joan | Davis | 86 | +-----------+------------+-----------+------+ 5 rows in set (0.00 sec)
  • 40. The WHERE Clause (1) Select rows according to some criterion SELECT * FROM marks WHERE studentID > 1 AND studentID < 5; +-----------+------------+-----------+------+ | studentID | first_name | last_name | mark | +-----------+------------+-----------+------+ | 2 | Bill | James | 67 | | 3 | Carol | Smith | 82 | | 4 | Bob | Duncan | 60 | +-----------+------------+-----------+------+ 3 rows in set (0.01 sec)
  • 41. The WHERE Clause (2) Select rows with marks >= 80 SELECT * FROM marks WHERE mark >= 80; +-----------+------------+-----------+------+ | studentID | first_name | last_name | mark | +-----------+------------+-----------+------+ | 3 | Carol | Smith | 82 | | 5 | Joan | Davis | 86 | +-----------+------------+-----------+------+ 2 rows in set (0.00 sec)
  • 42. The ORDER BY Clause Select rows according to some criterion SELECT * FROM marks ORDER BY mark DESC ; +-----------+------------+-----------+------+ | studentID | first_name | last_name | mark | +-----------+------------+-----------+------+ | 5 | Joan | Davis | 86 | | 3 | Carol | Smith | 82 | | 1 | Fred | Jones | 78 | | 2 | Bill | James | 67 | | 4 | Bob | Duncan | 60 | +-----------+------------+-----------+------+ 5 rows in set (0.00 sec)
  • 43. Searching Using LIKE (1) LIKE is used to search a table for values containing a search string: There are two wild-card characters used to specifiy patterns: _ matches a single character % matches zero or more characters Can also use NOT LIKE Searching is case insensitive
  • 44. Searching Using LIKE (2) Example: last names in marks table that begin with J Example: first names that have 3 letters SELECT * FROM marks WHERE last_name LIKE 'J%' ; SELECT * FROM marks WHERE first_name LIKE '_ _ _' ;
  • 45. Quoting strings If a string contains a single quote it must be backquoted (escaped) before it can be used in a query Example: find records containing O'Reilly in the last_name field. SELECT * FROM marks WHERE last_name = 'O\'Reilly' ;
  • 46. Limiting number of rows LIMIT can be used to specify the maximum number of rows that are to be returned by a select query. Example SELECT * FROM marks LIMIT 3; This query will return only the first 3 rows from the marks table To return 15 rows beginning at row 5 use SELECT * FROM marks LIMIT 4, 15;
  • 47. MySQL Functions (1) How many rows are there ? Can use COUNT (marks) instead of COUNT (*) SELECT COUNT (*) FROM marks; +----------+ | COUNT(*) | +----------+ | 5 | +----------+ 1 row in set (0.00 sec)
  • 48. MySQL Functions (2) What is the sum of all the marks? SELECT SUM (mark) FROM marks; +-----------+ | SUM(mark) | +-----------+ | 373 | +-----------+ 1 row in set (0.00 sec)
  • 49. MySQL Functions (3) What is the average mark? SELECT AVG (mark) FROM marks; +-----------+ | AVG(mark) | +-----------+ | 74.6000 | +-----------+ 1 row in set (0.00 sec)
  • 50. MySQL Functions (4) What is the minimum mark? SELECT MIN (mark) FROM marks; +-----------+ | MIN(mark) | +-----------+ | 60 | +-----------+ 1 row in set (0.00 sec)
  • 51. MySQL Functions (5) What is the maximum mark? SELECT MAX (mark) FROM marks; +-----------+ | MAX(mark) | +-----------+ | 86 | +-----------+ 1 row in set (0.00 sec)
  • 52. books.sql (1) USE web_db; CREATE TABLE books ( isbn CHAR (15) PRIMARY KEY NOT NULL , title VARCHAR (100) NOT NULL , author VARCHAR (100) NOT NULL , pub VARCHAR (20) NOT NULL , year YEAR NOT NULL, price DECIMAL (9,2) DEFAULT NULL ); books table this is a simple design isbn title author pub year price
  • 53. books.sql (2) -- Insert some books into books table INSERT INTO books VALUES ( '0-672-31784-2' , 'PHP and MySQL Web Development' , 'Luke Welling, Laura Thomson' , 'Sams' , 2001, 74.95 ); INSERT INTO books VALUES ( '1-861003-02-1' , 'Professional Apache' , 'Peter Wainwright' , 'Wrox Press Ltd' , 1999, 74.95 );
  • 54. Executing The Script within MySQL use a command such as source c:/.........../books.sql This adds the books table to the web_db database
  • 55. employee_db.sql (1) CREATE DATABASE IF NOT EXISTS employee_db; USE employee_db; DROP TABLE IF EXISTS employees; DROP TABLE IF EXITS jobs; employees table jobs table employeeID name position address employeeID hours
  • 56. employee_db.sql (1) CREATE TABLE employees ( employeeID SMALLINT NOT NULL , name VARCHAR (20) NOT NULL , position VARCHAR (20) NOT NULL , address VARCHAR (40) NOT NULL , PRIMARY KEY (employeeID) ); INSERT INTO employees VALUES (1001, 'Fred', 'programmer', '13 Windle St'); INSERT INTO employees VALUES (1002, 'Joan', 'programmer', '23 Rock St'); INSERT INTO employees VALUES (1003, 'Bill', 'manager', '37 Front St');
  • 57. employee_db.sql (2) CREATE TABLE jobs ( employeeID SMALLINT NOT NULL , hours DECIMAL (5,2) NOT NULL , ); INSERT INTO jobs VALUES (1001, 13.5); INSERT INTO jobs VALUES (1002, 2); INSERT INTO jobs VALUES (1002, 6.25); INSERT INTO jobs VALUES (1003, 4); INSERT INTO jobs VALUES (1001, 1); INSERT INTO jobs VALUES (1003, 7); INSERT INTO jobs VALUES (1003, 9.5);
  • 58. Executing The Script within MySQL use a command such as source c:/......./employee_db.sql This creates the employee_db database and adds the employees and jobs tables to it
  • 59. Select Queries With Joins (1) Cartesian product query SELECT * FROM employees, jobs; +------------+------+------------+--------------+------------+-------+ | employeeID | name | position | address | employeeID | hours | +------------+------+------------+--------------+------------+-------+ | 1001 | Fred | programmer | 13 Windle St | 1001 | 13.50 | | 1002 | Joan | programmer | 23 Rock St | 1001 | 13.50 | | 1003 | Bill | manager | 37 Front St | 1001 | 13.50 | | 1001 | Fred | programmer | 13 Windle St | 1002 | 2.00 | | 1002 | Joan | programmer | 23 Rock St | 1002 | 2.00 | | 1003 | Bill | manager | 37 Front St | 1002 | 2.00 | | 1001 | Fred | programmer | 13 Windle St | 1002 | 6.25 | | 1002 | Joan | programmer | 23 Rock St | 1002 | 6.25 | | 1003 | Bill | manager | 37 Front St | 1002 | 6.25 |
  • 60. Select Queries With Joins (2) Cartesian product query (continued) | 1001 | Fred | programmer | 13 Windle St | 1003 | 4.00 | | 1002 | Joan | programmer | 23 Rock St | 1003 | 4.00 | | 1003 | Bill | manager | 37 Front St | 1003 | 4.00 | | 1001 | Fred | programmer | 13 Windle St | 1001 | 1.00 | | 1002 | Joan | programmer | 23 Rock St | 1001 | 1.00 | | 1003 | Bill | manager | 37 Front St | 1001 | 1.00 | | 1001 | Fred | programmer | 13 Windle St | 1003 | 7.00 | | 1002 | Joan | programmer | 23 Rock St | 1003 | 7.00 | | 1003 | Bill | manager | 37 Front St | 1003 | 7.00 | | 1001 | Fred | programmer | 13 Windle St | 1003 | 9.50 | | 1002 | Joan | programmer | 23 Rock St | 1003 | 9.50 | | 1003 | Bill | manager | 37 Front St | 1003 | 9.50 | +------------+------+------------+--------------+------------+-------+ 21 rows in set (0.01 sec) The cartesian product query is rarely what we want.
  • 61. Select Queries With Joins (3) Substitution +------+-------+ | name | hours | +------+-------+ | Fred | 13.50 | | Joan | 2.00 | | Joan | 6.25 | | Bill | 4.00 | | Fred | 1.00 | | Bill | 7.00 | | Bill | 9.50 | +------+-------+ 7 rows in set (0.00 sec) Here we are replacing the employeeID numbers in the jobs table by the employee's name SELECT name, hours FROM employees, jobs WHERE employees.employeeID = jobs.employeeID;
  • 62. Select Queries With Joins (4) Entries only for Fred +------+-------+ | name | hours | +------+-------+ | Fred | 13.50 | | Fred | 1.00 | +------+-------+ 2 rows in set (0.00 sec) SELECT name, hours FROM employees, jobs WHERE employees.employeeID = jobs.employeeID AND name = 'Fred';
  • 63. Select Queries With Joins (5) Total hours worked for each person +------+------------+ | name | SUM(hours) | +------+------------+ | Bill | 20.50 | | Fred | 14.50 | | Joan | 8.25 | +------+------------+ 3 rows in set (0.00 sec) SELECT name, SUM(hours) FROM employees, jobs WHERE employees.employeeID = jobs.employeeID GROUP BY name;
  • 64. Select Queries With Joins (6) Total hours worked, for Fred +------+------------+ | name | SUM(hours) | +------+------------+ | Fred | 14.50 | +------+------------+ 1 row in set (0.00 sec) SELECT name, SUM(hours) FROM employees, jobs WHERE employees.employeeID = jobs.employeeID AND name = 'Fred' GROUP BY name;