Top Banner
1 Internet Programming Database(SQL and MySQL)
28

PHP - Getting good with MySQL part I

Jan 20, 2015

Download

Education

Firdaus Adib

Getting good with MySQL part I
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: PHP - Getting good with MySQL part I

1

Internet Programming

Database(SQL and MySQL)

Page 2: PHP - Getting good with MySQL part I

2

Objectives

To understand the relational database model.queries database using SQL (Structured Query Language). To understand the MySQL database server.To learn various database interfaces.

Page 3: PHP - Getting good with MySQL part I

3

IntroductionDatabase An integrated collection of data

Database Management System (DBMS) Provides mechanism for storing and

organizing data in a manner that is consistent with the database’s format.

The most popular database systems are relational database Use a language called SQL (Structured Query

Language)

Page 4: PHP - Getting good with MySQL part I

4

Introduction

Popular database systems Microsoft SQL Server, OracleTM,

SybaseTM InformixTM and MySQLTM.

An interface Software that facilitates

communication between a database management system and a program.

Examples: Perl DBI for Perl, DB-API for Python and dbx module for PHP.

Page 5: PHP - Getting good with MySQL part I

5

Relational Database Model

A logical presentation of data. Allows relationships among items of the data.Does not concern about the physical structure of the data.Is composed of tables Tables are composed of rows and

columns Has primary key for referencing data

Page 6: PHP - Getting good with MySQL part I

6

Relational Database Model

DatabaseStudent_Record

Table : Student_Details

Table : Score

Table : Medical

Table : Activities

Page 7: PHP - Getting good with MySQL part I

7

Relational Database Model

stud_ID stud_name

age cgpa

1111 Lily 19 3.40

2222 Kim 18 2.75

3333 Ali 21 3.00

Database Name: Student_RecordTable Name: Score

Primary key

A major strength of the relational model:supports simple, powerful querying of data.

Page 8: PHP - Getting good with MySQL part I

8

SQL (Structured Query Language)

Developed by IBM in the 1970s.Two Categories of SQL Statement.1. Data manipulation• SELECT, INSERT, DELETE

2.Data definition• CREATE DATABASE, DROP DATABASE • CREATE TABLE, DROP TABLE

Page 9: PHP - Getting good with MySQL part I

9

Basic SELECT QuerySELECT column_name

FROM table_name

WHERE condition;

Select all columns from a table SELECT * FROM table_name SELECT * FROM Score

Select specific column(s) from a table SELECT student_name FROM Score SELECT student_name, age FROM Score

Page 10: PHP - Getting good with MySQL part I

10

Basic SELECT Query

Specify the selection criteria for the query. SELECT student_id, cgpa

FROM Score

WHERE age > 18;

Use pattern matching to search for similar strings SELECT student_id, cgpa

FROM Score

WHERE student_name LIKE ‘K*’;

Page 11: PHP - Getting good with MySQL part I

11

Basic SELECT Query

Use pattern matching to search for strings in which exactly ONE character takes the selected character(?) place SELECT student_id, cgpa FROM Score WHERE student_name LIKE ‘?i*’; 2nd letterArrange in ascending or descending order SELECT column_name1, column_name2,… FROM table_name ORDER BY column_name ASC;

Page 12: PHP - Getting good with MySQL part I

12

Basic SELECT Query

Arrange in ascending or descending order SELECT column_name1, column_name2,…

FROM table_name

ORDER BY column_name DESC; SELECT student_ID, student_name, cgpa

FROM Score

ORDER BY student_name DESC;

Page 13: PHP - Getting good with MySQL part I

13

Basic SELECT QueryArrange rows in ascending order by multiple columns SELECT column_name1, column_name2,...

FROM table_name

ORDER BY column_name1 SortingOrder1, column_name2 SortingOrder2,... ;

SELECT student_ID, student_name, cgpa

FROM Score

ORDER BY student_name, age

Page 14: PHP - Getting good with MySQL part I

14

Use SELECT to join tables

Use an INNER JOIN to merge rows from two or more tables by testing for matching values.

SELECT column_name1, column_name2,...

FROM table_name1

INNER JOIN table_name2

ON table_name1.column_name = table_name2.column_name;

Page 15: PHP - Getting good with MySQL part I

15

Use SELECT to join tables SELECT student_name, blood_group,...

FROM Score

INNER JOIN Medical

ON Score.student_ID = Medical.student_ID

ORDER BY student_name;

The query combines the student_name and cgpa columns from table Score and column blood_group from table Medical, sorting the result in ascending order by student_name

Page 16: PHP - Getting good with MySQL part I

16

Use SELECT to join tables

SELECT table_name1.colx, table_name2.coly...

FROM table_name1, table_name2

WHERE condition;SELECT Score.student_name, Score.cgpa,

Medical.blood_group

FROM Score, Medical

WHERE Score.student_ID = Medical.student_ID;

Page 17: PHP - Getting good with MySQL part I

17

SQL Statement: INSERTINSERT INTO table_name

(col1, col2, col3, ...)

VALUES (‘text1’,’text2’...,num1,…);INSERT INTO Score (student_id, cgpa)

VALUES(4444, 3.5);

INSERT INTO Score

VALUES(4444,’John’,19,3.5);

Page 18: PHP - Getting good with MySQL part I

18

SQL Statement: DELETE

DELETE FROM table_name

WHERE condition; DELETE FROM Score

WHERE student_name=‘Lily’;

Page 19: PHP - Getting good with MySQL part I

19

SQL Statement: UPDATEUPDATE table_name

SET column_name1 = value1,column_name2 = value2,…

WHERE criteria;

UPDATE ScoreSET student_name = ‘Kimchi’, age = 20,WHERE student_ID = 3333;

Page 20: PHP - Getting good with MySQL part I

20

MySQLMultiuser, multithreaded RDBMS serverUses SQL to interact with and manipulate dataFew important features Enable multiple tasks concurrently – requesting

process is more efficient Support various programming language Available for all common platforms Full support of functions and operators to manipulate

data Accessing tables from different database using a

single query Able to handle large database

Page 21: PHP - Getting good with MySQL part I

21

MySQL

Page 22: PHP - Getting good with MySQL part I

22

Add table

Table Name

Page 23: PHP - Getting good with MySQL part I

TAB1033 - Internet Programming 23

Create Table fields

Page 24: PHP - Getting good with MySQL part I

TAB1033 - Internet Programming 24

Table in MySQL

Page 25: PHP - Getting good with MySQL part I

25

Introduction to Database Interface

Perl Database Interface (DBI) Enables user to access relational database

from Perl program. Database independent – allows migration

among DBMS Uses object-oriented interface – handles

PHP dbx module An XHTML-embedded scripting language Database interface that does not interact

with database directly It interacts with one of several database-

specific module

Page 26: PHP - Getting good with MySQL part I

26

Introduction to DBIPhyton DB-API Database Application Programming

Interface Consists of :

Connection data object – access the database

Cursor data object – manipulate and retrieve data

Portable across several databases

Page 27: PHP - Getting good with MySQL part I

27

ADO.NET Object Model

Provides an API for accessing database systems programmatically.Was created for .NET frameworkWas designed to interact with Microsoft’s Component Object ModelTM (COM) framework.

Further reading : Textbook, page 736

Page 28: PHP - Getting good with MySQL part I

28

SummaryWhat is database?The most popular database – relational databaseSQL for database queriesDatabase consists table(s)Two Categories of SQL Statement.1. Data manipulation

• SELECT, INSERT, DELETE,UPDATE

2. Data definition

DBI – Perl DBI, PHP dbx module, Python DB-APIADO.NET object Model