Sie sind auf Seite 1von 33

Basic Database

using MySQL
Prof. Alexander B. Pahayahay

REVIEW
What is DATABASE?
Purpose of Database
Store
Retrieve
Update
MySQL
(pronounced My Ess Que Ell)
is a simple, yet powerful Open
Source Software relational
database management system
that uses SQL.
(http://topwebhosting1.com/terminology.htm)

FEATURES
Speed
Reliability
Security
Scalability and Portability
Ease of use
Open Source
LIST OF COMMERCIAL
DATABASE
Oracle
IBM DB 2
Microsoft SQL Server
LIST OF FREE / OPEN
SOURCE DATABASE
MySQL
PostgresSQL
SQLite
MySQL GRAPHS


The Results are
1. Of the five databases we tested, only
Oracle9i and MySQL were able to run our
Nile application as originally written for 8
hours without problems.
2. The Oracle and MySQL drivers had the
best combination of a complete JDBC
feature set and stability.
3. SQL Server and MySQL were the
easiest to tune, and Oracle9i was the
most difficult because it has so many
separate memory caches that can be
adjusted.
source: http://www.mysql.com/why-mysql/benchmarks/eweek.html

mysql prompt
Note:
The prompt tells you that mysql is
ready for you to enter commands.
mysql> Ready for new command.
-> Waiting for next line of multiple-
line command.
'> Waiting for next line, collecting
a string that begins
with a single quote ().
"> Waiting for next line, collecting
a string that begins
with a double quote ().
Prompt Meaning
Create Database
mysql> create database myDB;
CREATE a table:
mysql> CREATE TABLE people (
lastname varchar(20) not null,
firstname varchar(20) not null,
county varchar(18) not null,
birth date,
death date
);
Creating Table
SHOW retrieve information about the
structure of a database or table.
CREATE create a table or database.
USE to select/use existing database.
DESCRIBE reveal the structure of a
table.
INSERT add a row to a table.
UPDATE modify a database entry.
DELETE delete rows from a table.
DROP delete entire tables or databases.
MySQL Commands
SHOW the tables in your
database:
mysql> SHOW tables;

DESCRIBE a table database:
mysql> DESCRIBE people;
MySQL Commands
INSERT data into the table:
mysql> INSERT INTO people
VALUES
('Smith',
'John',
'Dublin',
'1956-10-12',
' ');
MySQL Commands
SELECT retrieve information from a
database.
Syntax: SELECT sql command
Example #1: SELECT version();
#2: SELECT current_date;
#3: SELECT user(), curdate();
Syntax: SELECT column1, column2,.
FROM tablename
Example #1: SELECT * FROM myTB;
#2: SELECT num, name, bdate FROM myTB;
MySQL Commands
SELECT data from a table:
mysql> SELECT * FROM people;

INSERT some more data:
INSERT INTO people VALUES (
'Burke', 'Pat', 'Galway', '1980-02-15','
'
);
MySQL Commands
INSERT INTO people VALUES (
'Lynch',
'Alan',
'galway',
'1970-10-06',
'2001-01-07');

INSERT INTO people VALUES (
'Baker', 'Pat', 'Mayo', '1965-01-07', ' '
);
MySQL Commands
Use SELECT again to view data:
mysql> SELECT * from people;

Use WHERE with select to get
specific data:
mysql> SELECT * from people
WHERE county='Galway';
MySQL Commands
Return specific fields with SELECT:
mysql> SELECT firstname,
lastname from people;

Use ORDER BY to sort the data
and LIMIT to
set the number of results required:
mysql> SELECT lastname, birth from
people ORDER BY birth DESC
LIMIT 1;
MySQL Commands
Use GROUP BY to group data and
count() to count the number of
results.

mysql> SELECT county, count(*) AS
number from people GROUP BY
county ORDER BY number DESC;
MySQL Commands
Remove data using DELETE:
Mys ql> DELETE from people
WHERE lastname= 'Smith';

Use UPDATE to change values:
mysql> UPDATE people SET
county='Donegal' WHERE
county='Mayo';
Modifying
Using DROP to remove tables
(be careful):
DROP TABLE table_name;
Dropping Table
SELECT name, id_number,
grade FROM students, test
WHERE test.id_number =
student.id_number;
Multiple Tables
Functions in MySQL are very useful:
NOW()
Returns the system date and time
TO_DAYS()
Converts a date to a number of days
RAND()
Returns a random number
COUNT()
Returns the number of matching
entries
SUM()
Returns the sum of matching values
Functions
COUNT FUNCTION
SUM FUNCTION
AVERAGE FUNCTION
MINIMUM FUNCTION
MAXIMUM FUNCTION

Das könnte Ihnen auch gefallen