Let’s work with Database (MySql)
MySql & RDBMS
• MySQL is a Relational Database Management
• server can manage many databases at the same
• In fact many people might have different databases
managed by a single MySQL server.
• Each database consists of a structure to hold the
data and the data itself.
• A database can exist without data.
• Data in a database is stored in one or more
• First you create the empty database.
• Then you add empty tables to the database.
• Table is organized with in rows and columns.
• Each row represents an entity in the database
• Each column contains an item of information about
• The place where a particular row and column
intersect, the individual cell of the table, is called a
• Tables in databases can be related.
• Often a row in one table is related to several
rows in another table.
• You include a column in one table to hold data
that matches data in the column of another
Communicating with DataBase
• All our interaction with the database is
accomplished by passing messages to the MySQL
• It must be able to understand the instructions that
you send it.
• So we communicate using Structured Query
Language (SQL), which is a standard computer
language understood by most database
• To make a request that MySQL can understand,
you build an SQL query and send it to the
Building SQL queries
• The first word of each query is its name name, which is an
action word (a verb) that tells MySQL what we want to do.
• Queries we discuss here:
• The query name is followed by words and
phrases — some required and some optional —
that tell MySQL how to perform the action.
• For insntace we have to tell the table name to
which we want to insert the data.
• SQL language words in all caps.
• SQL words must be separated by one or more
• Put the string within double quote.
Sending SQL query
• We can send sql query to do what we desire to do
▫ The mysql client - A text-based mysql client is
automatically installed when we install MYSQL.
▫ Administration software - Separate software
packages that are available can provide a more user-
friendly interface for interacting with MySQL than the
mysql client does.
▫ PHP built in function –we communicate with a
MySQL database from PHP scripts by using PHP built-
in functions designed specifically for this purpose.
Using the mysql client
• To send SQL queries to MySQL from the mysql client,
follow these steps:
A. Locate the mysql client(it is in bin of your mysql
directory) we can start it fom Command prompt of
B. To start command is (mysql -u root -p)
C. If you are in network then you need command (mysql -h
mysqlhost.mycompany.com -u root -p)
D. At the mysql prompt, type your SQL query followed by a
semicolon (;) and then press Enter.
E. To leave the mysql client, type quit at the prompt and
then press Enter.
Using Administration software
• To work with mySql using admin software We need
these steps as follow:
A. Open your browser and access the phpMyAdmin
B. Click the down arrow in the databases field in the
left pane of the phpMyAdmin page. Click the name
of the database you want to open.
C. Click the SQL link near the top center of the page.
D. Type your SQL query in the text area
E. Click Go below the query text area.
Protect Ur DB before hacked
• MySQL accounts: No one can access the data in
your database without an account. Sp protect ur
database with a strong password.
• Permissions: MySQL uses account permissions
to specify who can do what. Differentiate the
tasks using different user name and password
for different tasks.
• The individual cell in which a particular row and
column intersect is called a field.
Better to be organized
• The table name should clearly identify the
objects that it contains with a descriptive word
• Column name must speaks about hat will be
filled in it.
• Name of column and table is case sensitive.
• Identify the primary key
• To cretae table we need CREATE TABLE
command as follows.
CREATE TABLE table_name ( column_name NOT NULL PRIMARY
column_name VARCHAR(37) NOT NULL,
column_name VARCHAR(2) NULL );
• To fetch / read data we need SELECT command.
• We will search the record and show them using
SELECT * from employee;
• To search something specific we need WHERE
clause. SELECT * from employee WHERE
• ORDER BY clause is to ensure that the result set produced by
the query is returned in the specified sequence. Simply,
ORDER BY sorts the results.
• Following the ORDER BY keywords is at least one column
with an optional ASC (ascending) or DESC (descending)
keyword; if neither is specified, ascending is the default.
ORDER BY column [ASC | DESC]
[, column [ASC | DESC]] ⋮
• The INSERT statement inserts one or more
INSERT INTO table name (colm1, colm2, colm3..)
• The UPDATE DML statement is similar to the ALTER DDL
statement in that it produces a change.
• The difference is that, whereas ALTER changes the
structure of a table, UPDATE changes the data contained
within a table, while the table’s structure remains the same.
UPDATE table_name SET column_name = ‘value’;
UPDATE table_name SET column_name = ‘value’
• The DELETE DML statement is similar to the DROP DDL
statement, in that it removes objects from the database. The
difference is that DROP removes a table from the
database, while DELETE removes entire rows of data
from a table, but the table continues to exist.
DELETE FROM table_name WHERE
column_name = ‘value’;