You are on page 1of 36

MYSQL

QUICK REVIEW
• Open Source Databases

– Either as outgrowths of earlier academic research


(PostgreSQL)

– developments of lightweight storage tools for websites


(MySQL)

– open-sourced commercial products (InterBase)

– Entwined with the success of LINUX


What is MySQL?

MySQL is a database.
The data in MySQL is stored in database
objects called tables.
A table is a collection of related data entries
and it consists of columns and rows.
Databases are useful when storing information
categorically. A company may have a database
with the following tables: "Employees",
"Products", "Customers" and "Orders".
Database Tables

A database most often contains one or more


tables. Each table is identified by a name
(e.g. "Customers" or "Orders"). Tables
contain records (rows) with data.
Queries

A query is a question or a request.


With MySQL, we can query a database for
specific information and have a recordset
returned. LastName
Hansen
Look at the following query: Svendson
SELECT LastName FROM Persons Pettersen
The query above selects all the data in the
"LastName" column from the "Persons" table,
and will return a recordset like this:
Facts About MySQL Database

One great thing about MySQL is that it can be


scaled down to support embedded database
applications. Perhaps it is because of this
reputation that many people believe that
MySQL can only handle small to medium-sized
systems.
The truth is that MySQL is the de-facto
standard database for web sites that support
huge volumes of both data and end users (like
Friendster, Yahoo, Google).
Create a Connection to a MySQL
Database

Before you can access data in a database,


you must create a connection to the
database.
In PHP, this is done with the
mysql_connect() function.
Syntax

mysql_connect(servername,username,pass
word);
Closing a Connection

The connection will be closed automatically


when the script ends. To close the
connection before, use the mysql_close()
function:
Create a Database

The CREATE DATABASE statement is used


to create a database in MySQL.
Syntax
CREATE DATABASE database_name
Create a Table

The CREATE TABLE statement is used to


create a table in MySQL.
Syntax
CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
....
)
Primary Keys and Auto
Increment Fields
Each table should have a primary key field.
A primary key is used to uniquely identify the rows in
a table. Each primary key value must be unique
within the table. Furthermore, the primary key field
cannot be null because the database engine
requires a value to locate the record.
The following example sets the personID field as the
primary key field. The primary key field is often an ID
number, and is often used with the
AUTO_INCREMENT setting. AUTO_INCREMENT
automatically increases the value of the field by 1
each time a new record is added. To ensure that the
primary key field cannot be null, we must add the
NOT NULL setting to the field.
MySQL
• Go to the Command Window
– Start\Run
– Type command

• Go to the MySQL path


C:\Progra~1\xampp\mysql\bin\mysql –u root –p

C:\Progra~1\xampp\mysql\bin\mysql –u root
• Enter password if there is any.
• Type commands in MySQL prompt which looks like this:
mysql >
Basic Commands
mysql > \c

mysql > status

mysql > \s

mysql > \h

mysql > ?

mysql > \?

mysql > help


Basic Query Commands
mysql > select version();

mysql > select user();

mysql > select current_date();

mysql > select now()


Basic Query Commands
mysql > \q

mysql > quit

mysql > exit


Basic Query Commands
mysql> create database <databasename>;

mysql> Use Student;

Note: It should display Query OK, 1 row affected as a result.


mysql> show databases;

mysql > DESCRIBE <tablename>;

mysql> show tables;


This time you can show
additional information of a table
mysql > show columns from candy;

mysql > show table status

mysql > show table status \G

mysql > show table status like ‘candy’ \G

mysql > show create table candy \G

Try it!!!
Adding Values
- > Insert Into <tblname>
- > Values (value1, value2, value3…);

- > Insert Into studentID (


- > Values (1, ‘Brad’);

- > Insert Into studentID (name)


- > Values (‘Jolie’);

- > Insert Into studentID


- > Values (3.5,‘Binay’);
Adding Values
- > Insert Into studentID
- > Values (4,’Binay’);

- > Insert Into studentID (idnum,name)


- > Values (4,’Binay’);

- > Insert Into studentID (name,idnum)


- > Values (’Lapid’,7);

- > Select * from studentID;


- > create table keyTable
- > (id int not null auto_increment,
- > name varchar(30),
- > key (id));

- > Insert Into keyTable


- > Values (1,’Mimosa’);

- > Insert Into keyTable


- > Values (1,’Mimosa’);
- > create table primaryTable
- > (id int not null auto_increment,
- > name varchar(30),
- > primary key (id));

- > Insert Into primaryTable


- > Values (2,’Edwin’);

- > Insert Into primaryTable


- > Values (2,’Edwin’);
- > show columns from keyTable;

- > show columns from primaryTable;


- > create table primaryTable
- > (id int not null auto_increment,
- > name varchar(30),
- > primary key (id));

- > create table keyTable


- > (id int not null auto_increment,
- > name varchar(30),
- > key (id));

- > create table uniqueTable


- > (id int not null auto_increment,
- > name varchar(30),
- > unique key (id));
To Delete a Table:
mysql > DROP TABLE <tablename>;

You remove a table from the database when it is


not required. You use the DROP TABLE statement
to remove a table. When you remove a table, all
the data from the table is also deleted.
Maintaining Databases
To Copy Data Into Another Table
SELECT column_expression
INTO target_table
FROM source_table
WHERE conditions;
To Update Data in a Table
UPDATE table_name
SET column_name = value [, column_name…]
[WHERE condition];
To Delete Data in a Table
DELETE [FROM] table_name
[WHERE conditions];
To Alter Table
ALTER [IGNORE] TABLE tbl_name
alter_specification [, alter_specification] ...

alter table STUDENT rename EMPLOYEE;

alter table EMPLOYEE


add column cellphone varchar(14) not null;
To Alter Table
alter table EMPLOYEE
add column address varchar(14) not null
first;

alter table EMPLOYEE add column


GENDER varchar(14) not null after SID;
alter table EMPLOYEE drop column GENDER;

alter table EMPLOYEE change ADDRESS


ADDR VARCHAR(54) not null;

alter table EMPLOYEE change SNAME


NAME char(25) null;

Alter table EMPLOYEE modify ADDR char(25);


Create a database name it DB1, then create
a table named EmployeeTbl
Table description should be as follows:

Add another column before HIREDATE


name it MCode (int)
Populate the table
EmployeeTbl
EMPNO ENAME JOB MCod HIREDATE SAL COM DEPT
(Int) (Text) (Text) e
(Date) (Double) (Double) NO
Primary Key (Int)
(Int)

1000 Dizon Manager 1002 11/11/87 5000 0 10

1002 Blake Analyst 1003 09/08/78 9000 1400 30

1003 Reyes Clerk 2000 02/03/98 2000 0 10

1004 Santos Salesman 1000 02/09/99 2500 300 12

1005 Miller Clerk 1002 11/12/89 1400 6 20


Exercise: Using EmployeeTbl, create SQL statements for the following
requirements:
1. Dizon just got married. Change name to Cuevas.
2. The manager feels to be generous this year. All employees will receive a 5% increase.
3. Employee Cuevas will be reassigned to DeptNo 12.
4. Employee Miller has retired from work. Permanently remove his records from the table.
EmployeeTbl
EMPNO ENAME JOB MGR HIREDATE SAL COM DEPTNO
(Int) (Text) (Text) (Int) (Date) (Double) (Double) (Int)
Primary Key

1000 Cuevas Manager 1002 11/11/87 5250 0 12

1002 Blake Analyst 1003 09/08/78 9450 1400 30

1003 Reyes Clerk 2000 02/03/98 2100 0 10

1004 Santos Salesman 1000 02/09/99 2625 300 12

You might also like