Lecture Guide
Lecture Guide
TE
SS
E
N
SI
BU
F
O T)
E B
G C
E (I
LL
COY
N
A OG
IM OL
N
2
USING MYSQL CREATE DATABASE STATEMENT
To create a new database in MySQL, you use the CREATE DATABASE
statement as follows:
CREATE DATABASE tenisdb5;
It’ll return the following:
After that, use the SHOW CREATE DATABASE command to review the
SHOW database:
created CREATE DATABASE
tenisdb5;
MySQL returns the database name and the character set and collation of
the database:
3
Finally, select the newly created database to work with by using
the USE statement:
USE tenisdb5;
Output:
Database changed
Now, you can start creating tables and other databases objects within
the tenisdb
database.
To quit the mysql program, type exit command:
4
CREATING TABLES IN THE MYSQL
DATABASE
The CREATE TABLE statement allows you to create a new table in a database.
The following statement creates a new table named PLAYERS
Output:
6
USING SHOW DATABASES
COMMAND TO GET LIST OF
DATABASES
Run the following query to show list of databases:
SHOW DATABASES;
Output:
7
SELECT THE DATABSE TO BE USE
select the database to work with by using the USE statement
USE tenisdb;
Output:
Database changed
8
USING MYSQL INSERT COMMAND
TO INSERT VALUES TO ALL
COLUMNS
This command can be used in order to add values to all columns. For example, add
values to the players table. Keep in mind that the order of values should match
the order of columns in the table.
INSERT INTO players
VALUES ('001', 'Ibrahim Usman', 'IUS', '1991\12\12', 'M', '019',
'Sardauna Crecent', '109', '7727', 'WUSE', '09099999999',
'24','2023/11/09');
Output:
9
USING MYSQL INSERT COMMAND
TO INSERT MULTIPLE ROWS VALUES
TOyou
To insert multiple rows into a table, A use
TABLE
the following form of the INSERT
INSERT INTO
statement
players(Player_no, pname, initials, birth_date, sex,
joined, street, house_no, post_code, town, phone_no,
league_no, created_at)
VALUES
('002', 'Maryam Habib', 'MH', '1991\12\12', 'F', '013', 'Awolowo
Street', '111', '7727', 'GARKI', '08088888888',
'17','2023/03/09'),
('003', 'Haris Muhd', 'HM', '1991\12\12', 'M', '012', 'Zoo Road',
'109', '7727', 'KANO', '09044444444', '24','2023/11/09'),
('004', 'Binta Ali', 'BAL', '1991\12\12', 'F', '011', 'MALALI', '109',
'7727', 'KADUNA', '09022222222', '21','2023/01/02'),
('005', 'John Abraham', 'JAB', '1993\12\12', 'M', '022', 'LOCOST',
'109', '7727', 'BAYELSA', '09033333333', '22','2023/11/09');
Output:
10
USING MYSQL SELECT STATEMENT
TO QUERY DATA FROM THE TABLE
The following example uses the SELECT statement to get all information from
players table
SELECT * FROM
players
utput:
11
Use Select Statement to select Player Name, Street and Player House Number
SELECT
pname, street,
house_no
FROM
players;
Output:
12
USING MYSQL SELECT STATEMENT
WITH ORDER BY CLAUSE
Use Select Statement to select Player Name, Street and Player House
Number, the should be presented in ascending order
SELECT
Player_no, pname, street,
Here you can see
house_no
that the result is
FROM
presented in an
players
ascending order,
ORDER BY
whereby Binta Ali
pname;
become the first
Output: player in the list.
13
USING MYSQL SELECT STATEMENT
WITH WHERE CLAUSE
The following query uses the WHERE clause to find Player whose street name is
Zoo Road:
SELECT
Player_no, pname, street,
house_no
FROM
players
WHERE street = 'Zoo Road';
Output:
14
USING MYSQL SELECT STATEMENT
WITH WHERE CLAUSE AND AND
OPERATOR
The following statement uses the AND operator in WHERE clause to find a player
whose street name is Zoo Road and house number is 109.
SELECT
Player_no, pname, street,
house_no
FROM
players
WHERE street = 'Zoo Road'
AND house_no = 109;
Output:
15
USING MYSQL SELECT STATEMENT
WITH WHERE CLAUSE AND OR
OPERATOR
The following query uses the OR operator in the WHERE clause to select all
players with Player_no 2 and town BAYELSA .
SELECT
Player_no, pname, street, house_no,
phone_no, town
FROM
players
WHERE Player_no = '2'
OR
town = 'BAYELSA';
Output:
16
USING MYSQL SELECT STATEMENT
WITH
IN OPERATOR
The following example uses the IN operator to find those Players that lives in
KANO and KADUNA
SELECT
Player_no, pname, street, house_no,
phone_no, town
FROM
players
WHERE
town IN ('KANO' , 'KADUNA');
Output:
17
USING MYSQL SELECT STATEMENT
WITH
BETWEEN OPERATOR
The following example uses the BETWEEN operator to find players whose ID is
between 2 to 4
SELECT
Player_no, pname, street
FROM
players
WHERE
Player_no BETWEEN 2 AND 4;
Output:
18
USING MYSQL SELECT STATEMENT
WITH
LIKE OPERATOR
The following query uses the LIKE operator to find all players whose Phone
Number includes 080
SELECT
Player_no, pname, street, phone_no
FROM
players
WHERE
phone_no LIKE '%080%';
Output:
19
USING SQL STATEMENT TO CREATE LEAGUE
TABLE
The CREATE TABLE statement allows you to create a new table in a database.
The following statement creates a new table named LEAGUE
20
VIEW THE CREATED TABLE STRUCTURE
Once you execute the CREATE TABLE statement to create the
LEAGUE table, you can view its structure by using the DESCRIBE
statement as shown below:
DESCRIBE league;
21
USING MYSQL INSERT COMMAND
TO INSERT MULTIPLE ROWS VALUES
TO A TABLE
To insert multiple rows into a table, you use the following form of the
INSERT statement
INSERT INTO
league(league_no, tournament, mounth, city, country, continent, surface,
established, created_at)
VALUES
('24', 'Australian Open', 'January', 'Melbourne', 'Australia', 'Oceania', 'Hard(Blue)', '1905',
'2023/06/06'),
('17', 'French Open', 'May/June', 'Paris', 'France', 'Europe', 'Red Clay', '1891', '2023/06/06'),
('25', 'Australian Open', 'June/July', 'Brisbane', 'Australia', 'Oceania', 'Grass', '1877',
'2023/06/06'),
('21', 'Wimbledon', 'Augt/Sept', 'London', 'UK', 'Europe', 'Grass', '1908', '2023/06/06'),
('22', 'US Open', 'Jan/Feb', 'NewYorkCity', 'USA', 'USA', 'Hard (Blue)', '1881', '2023/06/06');
22
USING MYSQL SELECT STATEMENT
TO VIEW THE ENTIRE RECORD IN
THE LEAGUE TABLE
SELECT * FROM
league;
23
USING MYSQL SELECT STATEMENT
TO UPDATE OR CHANGE RECORD IN
A TABLE
To update or change record in the players table, we are going to use the
UPDATE and SET procedure below
UPDATE players
SET league_no = '25'
WHERE Player_no = 3;
Before
After
24
USING MYSQL SELECT STATEMENT
TO DELETE SINGLE RECORDS IN A
TABLE
To Delete a single records in the players table, we are going to use the DELETE
procedure below
DELETE FROM players
WHERE pname='Ibrahim
Usman';
Before
After
25
USING MYSQL SELECT STATEMENT
TO SELECT TOP CLAUSE
• The SELECT TOP clause is used to specify the number of records to
return.
• The SELECT TOP clause is useful on large tables with thousands of
records. Returning a large number of records can impact
SELECT * FROM players
performance.
LIMIT 3;
26
USING MYSQL SELECT STATEMENT TO
VIEW THE DATE OF BIRTH OF THE
OLDEST PLAYER
• The MIN() function returns the smallest value of the selected column.
• This function is more suitable when displaying the lowest salary in a salary table
SELECT MIN(birth_date) AS
Youngest_Player
FROM players;
27
USING MYSQL SELECT STATEMENT
TO VIEW THE DATE OF
BIRTH OF THE YOUNGEST PLAYER
• The MAX() function returns the Highest value of the selected
column.
• This function is more suitable when displaying the Highest salary in
a salary table
SELECT MAX(birth_date) AS
Youngest_Player
FROM players;
28
USING SQL ALIASES IN
MYSQL SELECT STATEMENT
• SQL aliases are used to give a table, or a column in a table, a temporary
name.
• Aliases are often used to make column names more readable.
• An alias only exists for the duration of that query.
• An alias is created with the AS keyword.
SELECT pname AS Player_Name, sex AS
Gender
FROM players;
29
SQL INNER JOIN KEYWORD
• The INNER JOIN keyword selects records that have matching values in both
tables.
• We are going to select Player Name, Player Gender and League Number all
from Players Table while Tournament and Country from League Table
• The result will display in a single table as below
SELECT players.pname, players.sex, players.league_no, league.tournament,
league.country
FROM league
INNER JOIN players ON players.league_no=league.league_no;
30
SQL DATABASE
• SQL Drop database
• SQL Drop Table
• SQL Truncate Table
• SQL Alter Table
• Alter table - Add column
• Alter table - Drop column
• Alter table - Rename column
• SQL Hosting
31
SQL DROP DATABASE STATEMENT
• The DROP DATABASE statement is used to drop an existing SQL database.
• Note: Be careful before dropping a database. Deleting a database will result
in loss of complete information stored in the database!
• Make sure you have admin privilege before dropping any database. Once a
database is dropped, you can check it in the list of databases with the
following SQL command: SHOW DATABASES;
• The following SQL statement drops the existing database "testdb":
SHOW
DATABASES;
Before After
32
THE SQL DROP TABLE STATEMENT
• The DROP TABLE statement is used to drop an existing table in a database.
• Note: Be careful before dropping a table. Deleting a table will result in loss of
complete information stored in the table!
• The following SQL statement drops the existing table “league":
33
SQL TRUNCATE TABLE STATEMENT
• The TRUNCATE TABLE statement is used to delete the data inside a
table, but not the table itself.
TRUNCATE TABLE
league2;
34
ALTER TABLE – Drop
Column
• To delete a column in a table, use the following syntax (notice that some
database systems don't allow deleting a column):
• The following SQL deletes the “address" column from the “team" table:
After
35
SQL ALTER TABLE STATEMENT
• The ALTER TABLE statement is used to add, delete, or modify columns in an
existing table.
• The ALTER TABLE statement is also used to add and drop various constraints
on an existing table.
ALTER TABLE - ADD
ALTER TABLE team
Column
To add a column in a table, use the following syntax:
ADD address varchar(255);
Before
After
36
ALTER TABLE – Rename
Column
• To rename a column City in a table (team), use the following syntax:
After
37
SQL HOSTING
• If you want your web site to be able to store and retrieve data from a database,
your web server should have access to a database-system that uses the SQL
language.
• If your web server is hosted by an Internet Service Provider (ISP), you will have
to look for SQL hosting plans.
• The most common SQL hosting databases are MS SQL Server, Oracle, MySQL,
and MS Access. Some Web husting
companies
• Bluehost - https://www.bluehost.com/
• Hostgator - https://www.hostgator.com/
• Hostinger - https://www.hostinger.com/
• Godaddy - https://www.godaddy.com/en-uk
• web.com - https://www.web.com/
• A2hosting - https://www.a2hosting.com/
• Nexcess - https://shop.nexcess.net/
• Networksolution - https://www.networksolutions.com/
38
THE MOST COMMON SQL HOSTING
DATABASES
• MS SQL Server
• Microsoft's SQL Server is a popular database software for database-driven web sites
with high traffic,
• SQL Server is a very powerful, robust and full featured SQL database system.
• Oracle
• Oracle is also a popular database software for database-driven web sites with high
traffic.
• Oracle is a very powerful, robust and full featured SQL database system.
• MySQL
• MySQL is also a popular database software for web sites.
• MySQL is a very powerful, robust and full featured SQL database system.
• MySQL is an inexpensive alternative to the expensive Microsoft and Oracle solutions.
• MS Access
• When a web site requires only a simple database, Microsoft Access can be a solution.
• MS Access is not well suited for very high-traffic, and not as powerful as MySQL, SQL
Server, or Oracle.
39