Top Banner
Database Using MySQL Creation, Manipulation, and Management Jerome Locson Developer, Ariba Tech Solutions [email protected]
35

Database Basics and MySQL

May 25, 2015

Download

Technology

Jerome Locson

Database Basics and MySQL
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: Database Basics and MySQL

Database Using MySQL

Creation, Manipulation, and Management

Jerome LocsonDeveloper, Ariba Tech [email protected]

Page 2: Database Basics and MySQL

Graduate from ADZU, BSCS PhilNITS FE Passer GTUG PH, Zamboanga Chapter Community

Manager Technopreneur, Ariba Tech Solutions Blog: www.jeromelocson.com

About me

Page 3: Database Basics and MySQL

Relational Database Concept Structure Query Language (SQL)

◦ DDL, DML and DCL Database Normalization ACID Property Relational Database Management Systems

(RDBMS), MySQL Example of MySQL Queries (PhpMyAdmin) SQL Injection Prevention

Outline

Page 4: Database Basics and MySQL

◦ Used by all major commercial database systems◦ Very simple model◦ Query with high-level languages: simple yet

expressive◦ Efficient implementations

Relational Databases

Page 5: Database Basics and MySQL

The Relational Model

Schema = structural description of relations in database Instance = actual contents at given point in time

Page 6: Database Basics and MySQL

The Relational Model Database = set of named relations (or tables) Each relation has a set of named attributes (or columns) Each tuple (or row) has a value for each attribute Each attribute has a type (or domain)

Page 7: Database Basics and MySQL

The Relational Model

NULL – special value for “unknown” or “undefined”

Page 8: Database Basics and MySQL

The Relational Model Key – attribute whose value is unique in each tuple Or set of attributes whose combined values are unique

Page 9: Database Basics and MySQL

◦ Used by all major commercial database systems◦ Very simple model◦ Query with high-level languages: simple yet

expressive◦ Efficient implementations

Relational Databases

Page 10: Database Basics and MySQL

Standard/dominant language for accessing databases and processing

Steady improvements in reliability, performance and security

Applicable to many database systems

Structure Query Language (SQL)

Page 11: Database Basics and MySQL

It can define a database (DDL) It can manipulate database (DML) and, can control a database (DCL)

What can SQL do?

Page 12: Database Basics and MySQL

defining the structure and contents of a relational database

Metadata (data about the data) defines the mapping of database to physical

hardware and devices Basic Syntax:

◦ Create Table, Alter Table, Drop Table, Create View, Create User, etc.

Data Definition Language (DDL)

Page 13: Database Basics and MySQL

specifies how queries and updates are to be done

Basic Syntax:◦ Select, Insert, Delete, Update

Data Manipulation Language (DML)

Page 14: Database Basics and MySQL

involves configuring and controlling the database - permission, roles, and referential integrity

Basic Syntax:◦ Grant, Check, Constraint, Primary Key, Foreign

Key

Data Control Language (DCL)

Page 15: Database Basics and MySQL

Process of efficiently organizing data in a database (1NF-5NF)

Too: Entity-Relationship Diagram (ERD) Goals:

◦ eliminating redundant data◦ ensuring data dependencies make sense

Forms:◦ 1NF (First Normal Form)◦ 2NF (Second Normal Form)◦ 3NF (Third Normal Form)

Database Normalization

Page 16: Database Basics and MySQL

First normal form (1NF) sets the very basic rules for an organized database:

◦ Eliminate duplicative columns from the same table.

◦ Create separate tables for each group of related data and identify each row with a unique column or set of columns (the primary key).

First Normal Form (1NF)

Page 17: Database Basics and MySQL

Second normal form (2NF) further addresses the concept of removing duplicative data:

◦ Meet all the requirements of the first normal form.◦ Remove subsets of data that apply to multiple

rows of a table and place them in separate tables.◦ Create relationships between these new tables

and their predecessors through the use of foreign keys.

Second Normal Form (2NF)

Page 18: Database Basics and MySQL

Third normal form (3NF) goes one large step further:◦ Meet all the requirements of the second normal

form.◦ Remove columns that are not dependent upon the

primary key.

Third Normal Form (3NF)

Page 19: Database Basics and MySQL

Example of Normalization

Page 20: Database Basics and MySQL

Example of Normalization

Page 21: Database Basics and MySQL

Example of Normalization

Page 22: Database Basics and MySQL

Stands for atomicity, consistency, isolation, durability

set of properties that guarantee database transactions are processed reliably

ACID Property

Page 23: Database Basics and MySQL

Atomicity (atomic)◦ requires that database modifications must follow

an "all or nothing" rule◦ If one part of the transaction fails, the entire

transaction fails and the database state is left unchanged.

ACID Property

Page 24: Database Basics and MySQL

Consistency ◦ ensures that any transaction the database

performs will take it from one consistent state to another

◦ only consistent (valid according to all the rules defined) data will be written to the database

◦ whatever rows will be affected by the transaction will remain consistent with each and every rule

ACID Property

Page 25: Database Basics and MySQL

Isolation◦ no transaction should be able to interfere with

another transaction at all◦ use a serial model where no two transactions can

occur on the same data at the same time

ACID Property

Page 26: Database Basics and MySQL

Durability◦ once a transaction has been committed, it will

remain so◦ every committed transaction is protected against

power loss/crash/errors and cannot be lost by the system and can thus be guaranteed to be completed

ACID Property

Page 27: Database Basics and MySQL

RDBMS is the basis for SQL, and for all modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.

MySQL database is the world's most popular open source database for the Web

Supports different languages,environment, etc.

www.mysql.com

Relational Database Management Systems (RDBMS), MySQL

Page 28: Database Basics and MySQL

• mysql_connect(host, username [,password]);◦ Connects to a MySQL server on the specified host

using the given username and/or password. Returns a MySQL link identifier on success, or FALSE on failure.

• mysql_select_db(db_name [,resource])◦ Selects a database from the database server.

Useful PHP Functions for MySQL

Page 29: Database Basics and MySQL

• mysql_query(SQL, resource); ◦ Sends the specified SQL query to the database

specified by the resource identifier. The retrieved data are returned by the function as a MySQL result set.

• mysql_result(result, row [,field]); ◦ Returns the contents of one cell from a MySQL result

set. The field argument can be the field name or the field’s offset.

• mysql_fetch_array(result [,result_type])◦ Fetch a result row as an associative array, a numeric

array, or both. The result type can take the constants MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH.

Useful PHP Functions for MySQL

Page 30: Database Basics and MySQL

• mysql_free_result(result)◦ Frees the result set

• mysql_close(resource)◦ Closes the connection to the database.

Useful PHP Functions for MySQL

Page 31: Database Basics and MySQL

If there is error in the database connection, you can terminate the current script by using the die function.

For example: $db = mysql_connect("localhost", "root“, “”)

or die("Could not connect : " . mysql_error());

mysql_select_db("my_database") or die("Could not select database");

$result = mysql_query($query) or die("Query failed");

Error Handling

Page 32: Database Basics and MySQL

Example: Looping through the Cells<?php/* Connecting, selecting database */$link = mysql_connect("mysql_host", "mysql_user", mysql_password") or die("Could not connect : " . mysql_error());echo "Connected successfully";mysql_select_db("my_database") or die("Could not select database");

/* Performing SQL query */$query = "SELECT * FROM my_table";$result = mysql_query($query) or die("Query failed : " . mysql_error());

/* Printing results in HTML */echo "<table>\n";while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n";}echo "</table>\n";

Loop through each row of the result set

Loop through each element in a row

Page 33: Database Basics and MySQL

/* Free resultset */mysql_free_result($result);

/* Closing connection */mysql_close($link);?>

Example: Looping through the Cells

Page 34: Database Basics and MySQL

Using mysql_real_escape_string() Using of string replace method for SQL

keywords like SELECT, INSERT, DELETE and UPDATE

SQL Injection Prevention

Page 35: Database Basics and MySQL

Thank you!Twitter: @jeromelocson

Email: [email protected]: www.jeromelocson.com