MySQL Database Development Course Guide
MySQL Database Development Course Guide
Information Systems
INF303E
Data Base
DR. Amr Muhammad Abd-Elwahed
The course offers hands-on experience in developing client/server database applications
using a major database management system. Students learn how to create and
manipulate database objects, including tables, views, and sequences; develop program
units using SQL; and implement client applications such as forms and reports. The course
provides students with first- hand experience developing prototyping client/server
applications. Topics include the investigation and applications of advanced concepts such
as integrity, security, concurrency and recovery, object-oriented database design, current
developments and trends in DBMS. The course introduces the student to the process of
database development, including data modeling, database design, and database
implementation. Students learn basic interactive SQL for both data definition and
queries. Students practice design skills by developing a small database project.
Course Outlines
❑ MySQL Database
• CREATE - INSERT INTO
• SELECT FROM
• UPDATE – DELETE
• ALTER
• If statement- Case statement
• NULL functions - Comment - View
• IMPORT/EXPORT data
• Tables Relationships
• CREATE TABLE from other table/tables in the same/different database
• INSERT/UPDATE DATA from other table/tables in the same/different database
❑ PHP => MySQL database
❑ Python => MySQL database
❑ Python <=> PHP
❑ Database Security (PHP & Python)
• Regular Regression • SSL Certification
• Password • SQL injection attack
• Hash function • XSS attack
• Public-Key cryptography • POST/GET methods
• Symmetrical cryptography • MySQL Default Account (root)
• Digital Signature • Hints on other Security Topics
Chapter 1: MySQL Database
Explain The effect of the different clauses and functions on the table(s)
6
Introduction
& XAMPP Installing
Introduction
Database
A database is an organized collection of structured information, or data, typically stored
electronically in a computer system. A database is usually controlled by a Database
Management System (DBMS).
Database Management System (DBMS)
DBMS is software that serves as an interface between an end-user and a database, allowing
users to create, read, update, delete, manage, and manipulate data in the database.
Relational Database (RDB)
RDB is a way of structuring information in tables, rows, and columns. An RDB has the ability to
establish links—or relationships–between information by joining tables, which makes it easy
to understand and gain insights about the relationship between various data points.
Step-1:
Download and Install XAMPP from the next link.
[Link]
It is preferred to install XAMPP on a drive rather than drive C because of windows security
restrictions with some MySQL commands.
Step-2:
Search on XAMPP Control Panel on your Computer. Then, click on it.
Step-3:
When XAMPP is openend, you will find a service (or server) called “Apache”. It works be
default on port number 80. May be, some application on your PC work with this poryt
number. So, we should this port number of Apache service.
On the window of XAMPP Control Panel, click of “Config”, then select “Apache ([Link])”
Step-4:
After the file of “Apache ([Link])” is openend, change the port number from the default
value 80 to another one. The popular free ports are 88 and 5000. We will Scroll the contenets
of the file down to find Listen 80. Then, change 80 to 88. Then, save the file.
Step-5:
on the window of XAMPP Control Panel, Start Apache and MySQL.
If the “Stop” button is reflected to be “Start” gain, try to change the port number again
to another value.
Step-6:
on the address bar of your browser, write: [Link]
Where 88 is our new port number of Apache. More details in this link:
[Link]
Step-7:
On “phpMyAdmin” Page, click in “SQL” tab:
Step-8:
Write your MySQL statements. Then Click on “GO” button.
Then Click on
“GO” button.
Example:
1st statement
USE MUST;
SELECT * FROM Registration; 2nd statement
Click on
If there is an error to RUN the MySQL statements, then, the error will be shown below:
If there is no error:
The Result
CREAT & INSERT
Create a Database
The syntax used to create a database:
CREATE DATABASE database_name;
Example:
Database name
Each statement must be ended by “;”
Hint: to delete the database, we can use:
DROP DATABASE INF405;
statement is correct
statement
Result
If we create a database that created before, we will get an error as follows:
You can Create your table by GUI window from the next Link:
انشاء قاعدة بياناتmysql باستخدامXampp → [Link]
to see all the tables names in the database, we can use:
SHOW TABLES;
1st statement
Result
If we create a table that created before, we will get an error as follows:
If You avoid this error, and use “IF NOT EXISTS” as follows:
MySQL will create the table if it does not exist. MySQL does not
override on the existing table.
As shown in the example, we change the “Name” column to be
“Address” column. MySQL will consider the old table without
change.
Insert data (called records) into the table
In the database, the columns are called “Fields” and the rows are called “Records”. Thus, in a
table, data are stored in fields and records. Here, we will insert records for one filed or more.
The syntax used to insert records in the fields:
INSERT INTO table_name VALUES();
Code Name Email Tele
Example: To insert the next data:
97909 Fawzy Khaled Mazen Fawzy@[Link] +201011000100
USE INF405;
INSERT INTO StudentInf
VALUES ('97909', 'Fawzy Khaled Mazen', 'Fawzy@[Link]', '+201011000100');
“VALUES” line can be written in the same line of “INSERT INTO” as follows:
INSERT INTO StudentInf VALUES ('97909', 'Fawzy Khaled Mazen', 'Fawzy@[Link]', '+201011000100');
or in different line as shown in the example. So, we do not add “;” except with “VALUES” line.
Although we remember the columns titles, we forget the order of them when we insert
the records, we can use specify the order of the records according to the order that we
specified in the MySQL sentence as follows:
The columns (or fields) titles
USE INF405;
INSERT INTO StudentInf (Email, Code, Tele, Name)
VALUES ('Fawzy@[Link]', '97909', '+201011000100', 'Fawzy Khaled Mazen');
These data (or records) are ordered according to the orders of columns
Code Name Email Tele
97909 Fawzy Khaled Mazen Fawzy@[Link] +201011000100
In MySQL:
If we want to enter the record (or data) for one column or more:
The columns (or fields)
USE INF405;
INSERT INTO StudentInf (Code, Name)
VALUES ('97909', 'Fawzy Khaled Mazen');
We will talk about the first three datatypes. For more details about datatypes, in this link:
[Link]
Numeric Datatypes
a) Integers Values
Range
Type Description Digits
From To
Signed -2147483648 2147483647
INT Integer 11
Unsigned 0 4294967295
Signed -128 127
TINYINT Very small integer 4
Unsigned 0 255
Signed -32768 32767
SMALLINT Small integer 5
Unsigned 0 65535
Signed -8388608 8388607
MEDIUMINT Medium-sized integer 9
Unsigned 0 16777215
Signed -9223372036854775808 9223372036854775807
BIGINT Large integer 20
Unsigned 0 18446744073709551615
Example:
USE INF405;
CREATE TABLE doctors(
RoomNo TINYINT,
Salary INT,
TotalPatient BIGINT
);
So, maximum number of digits in the “RoomNo” column is 4 (e.g., 4512) because its datatype
is TINYINT as mentioned before in the last table. But we can limit this digits to any number of
digits less than the maximum number of digits, 4. This problem is for any datatype in MySQL.
For example:
RoomNo TINYINT(2),
In this case, the user cannot exceed than 2 digits (e.g., 62). If the user enters more that 2
digits, MySQL will return an error. This problem is for any datatype in MySQL.
b) Decimal Values
They cannot be unsigned. They take two parameters: M and D where M is total number of
digits including decimal while D is number of decimals. For example: FLOAT(5, 3) means that
3 is the number of decimals and 5 is the total number of digits (including decimals).
Decimal
Type Description Synonym Default
precision
FLOAT(M,D) A floating-point number. FLOAT(10,2) 24 places
Here, we selected datatype: “varchar”. It means that the data type of this column is String
and you can insert a record in each cell in this column from 0 character into 9 characters. If
you insert a record in this cell and the length is more than 9 characters, you will return an
error from MySQL and this record will not be added to the table.
b) BLOB
BLOBs are "Binary Large Objects" and are used to store large amounts of binary data, such
as images or other types of files. With BLOB, You do not specify a length.
Type Length
BLOB 65535 characters
TINYBLOB 255 characters
MEDIUMBLOB 16777215 characters
LONGBLOB 4294967295 characters
c) TEXT
With TEXT, You do not specify a length.
Type Length
TEXT 65535 characters
TINYTEXT 255 characters
MEDIUMTEXT 16777215 characters
LONGTEXT 4294967295 characters
d) ENUM
ENUM means enumeration التعداد. When defining an ENUM, you are creating a list of items
from which the value must be selected (or it can be NULL).
For example, if you wanted your field to contain "A" or "B" or "C", you would define your
ENUM as ENUM ('A', 'B', 'C') and only those values (or NULL) could ever populate that field.
Date and Time Datatypes
USE INF405;
CREATE TABLE StudentInf(
code varchar(10),
Name varchar(50),
Email varchar(30),
Tele varchar(15),
UNIQUE (code)
);
Or you can assign Unique constraint in the same line of the “code” column as follows:
USE INF405;
CREATE TABLE StudentInf(
code varchar(10) UNIQUE,
Name varchar(50),
Email varchar(30),
Tele varchar(15)
);
Right now, we can insert our data in the table as follows:
USE INF405;
INSERT INTO StudentInf (Code, Name, Email, Tele)
VALUES
('87987', 'Khaled Hady Wamy', 'Khaled@[Link]', '+201567576565'),
('547657', 'Kazem Amir Madian', 'Kazem@[Link]', '+201278969796'),
('986799', 'Wessam Omar Mazen', 'Wessam@[Link]', '+201087568758');
If we try to insert a code that is inserted before, MySQL will consider an error in the statement.
USE INF405;
INSERT INTO StudentInf (Code, Name, Email, Tele)
VALUES
('547657', 'Marwan Jack Wafy', 'Marwan@[Link]', '+201193105219’);
We can assign more than one column:
USE INF405;
CREATE TABLE StudentInf(
code varchar(10),
Name varchar(50),
Email varchar(30),
Tele varchar(15),
UNIQUE (code, Email)
);
We can assign a name for Unique constraint. Here, we assign “Cairo” for Unique constraint.
USE INF405;
CREATE TABLE StudentInf(
code varchar(10),
Name varchar(50),
Email varchar(30),
Tele varchar(15),
CONSTRAINT Cairo UNIQUE (code, Email)
);
AUTO INCREMENT constraint
Auto-increment allows a unique number to be generated automatically when a new record is
inserted into a table started by 1.
STNum Code Name Email Tele
1 97909 Fawzy Khaled Mazen Fawzy@[Link] +201011000100
2 67920 Hammd Tawfeek bazzar Lanwar NULL NULL
3 Khaled Hady Wamy Khaled@[Link] NULL
We do not insert any data for “STNum” column, but MySQL increment the cells starting from 1.
Also, from the last example, we understand how we can insert many rows at the same time.
We can rewrite the last example by another way. We do not need to write the columns names
but for the incremented column, we will insert NULL. MySQL will remove this NULL value by
the incremented value as follows:
We do not write the columns names here
USE INF405;
INSERT INTO StudentInf
VALUES
(NULL, '87987', 'Khaled Hady Wamy', 'Khaled@[Link]', '+201567576565'),
(NULL, '547657', 'Kazem Amir Madian', 'Kazem@[Link]', '+201278969796'),
(NULL, '986799', 'Wessam Omar Mazen', 'Wessam@[Link]', '+201087568758');
USE INF405;
INSERT INTO StudentInf
VALUES
('Khaled', 71, '012896986');
To understand SELECT Statement, Let us create a database. Then, inside the database, we will
create a table:
To create a database :
CREATE DATABASE employee;
Let us assume the next table:
To create a table :
USE employee;
CREATE TABLE Persons(
Person_ID int(1) AUTO_INCREMENT,
Person_Name varchar(10),
Region varchar(20) DEFAULT 'Unknown Region',
Age int(2) NOT NULL,
Female int(1),
Male int(1),
Salary int(5),
PRIMARY KEY (Person_ID),
UNIQUE (Region),
CHECK (Salary>2000 and Age>20)
);
Insert the values:
USE employee;
INSERT INTO Persons
VALUES
(NULL, 'Kazem', 'Brazil', 21, 1, 0, 3000),
(NULL, 'Mazen', 'Japan', 29, 1, 1, 5375),
(NULL, 'Habib', 'France', 25, 2, 0, 6000),
(NULL, 'Yasser', 'Marco', 31, 0, 3, 32000),
(NULL, 'Shady', 'Canada', 23, 1, 2, 7200);
To do a query, we will depend on Select statement. Its syntax is as follows:
SELECT Column_names FROM table_name;
For example:
To select all columns from the table: “Persons”
USE employee;
SELECT * FROM Persons;
RESULT:
Report
To select specific columns from the table: “Persons”
USE employee;
SELECT Region, Salary FROM Persons;
RESULT:
Report
So, we depend on SQL language to send a query to MySQL Query االستعالم
database. MySQL database will respond by a report.