SlideShare a Scribd company logo
DATABASE
PROGRAMMING
Henry Osborne
DATABASE

A comprehensive collection of
related data organized for
convenient access, generally in a
computer
RELATIONAL DATABASE
A relational database is a collection of data items
organized as a set of formally-described tables from
which data can be accessed or reassembled in
many different ways without having to reorganize the
database tables. The relational database was
invented by E. F. Codd at IBM in 1970.
http://searchsqlserver.techtarget.com/definition/relational-database
INDICES
• Make it possible to organize the data in a table according
to one or more columns.
• One of the cardinal elements of relational databases
• Can usually be created on one or more columns of a table
• Can also be declared as unique
• Primary keys are a special type of unique index that is used
to determine the “natural” method of uniquely identifying a
row in a table
RELATIONSHIPS
• One-to-one: at most, one row in the child table can
correspond to each row in the parent table
• One-to-many: an arbitrary number of rows in the child table
can correspond to any one row in the parent table
• Many-to-many: an arbitrary number of rows in the child
table can correspond to an arbitrary number of rows in the
parent table
DATA TYPES
int or integer
smallint
real
float
char
varchar

Signed integer number, 32 bits in length.
Signed integer number, 16 bits in length.
Signed floating-point number, 32 bits in length.
Signed floating-point number, 64 bits in length.
Fixed-length character string.
Variable-length character string.
CREATING DATABASES
CREATE DATABASE <dbname>;
CREATE SCHEMA <dbname>;
CREATE TABLES
CREATE TABLE <tablename> (
<col1name> <col1type> [<col1attributes>],
[...
<colnname> <colntype> [<colnattributes>]]
);
CREATE TABLES
CREATE TABLE book (
id INT NOT NULL PRIMARY KEY,
isbn VARCHAR(13),
title VARCHAR(255),
author VARCHAR(255),
publisher VARCHAR(255)

);
CREATING INDICES AND
RELATIONSHIPS
CREATE INDEX <indexname>
ON <tablename> (<column1>[, ..., <columnn>]);

CREATE INDEX book_isbn ON book (isbn);
CREATING INDICES AND
RELATIONSHIPS
CREATE TABLE book_chapter (
isbn VARCHAR(13) REFERENCES book (id),
chapter_number INT NOT NULL,
chapter_title VARCHAR(255)

);
DROPPING OBJECTS
DROP TABLE book_chapter;

DROP SCHEMA my_book_database;
ADDING/MANIPULATING DATA
INSERT INTO <tablename> VALUES
(<field1value>[, ..., <fieldnvalue>]);
OR
INSERT INTO <tablename>
(<field1>[, ..., <fieldn>])
VALUES
(<field1value>[, ..., <fieldnvalue>]);
ADDING/MANIPULATING DATA
INSERT INTO book (isbn, title, author)
VALUES (’0812550706’, ’Ender’s Game’, ’Orson Scott Card’);
ADDING/MANIPULATING DATA
To update records, you can use the UPDATE statement.
UPDATE book
SET publisher = ’Tor Science Fiction’, author = ’Orson S. Card’
WHERE isbn = ’0812550706’;
REMOVE DATA
DELETE FROM book;

DELETE FROM book WHERE isbn = ’0812550706’;
RETRIEVE DATA
SELECT * FROM book;
SELECT * FROM book WHERE author = ’Ray Bradbury’;

SELECT * FROM book
WHERE author = ’Ray Bradbury’ OR author = ’George Orwell’;
SELECT * FROM book
WHERE author = ’Ray Bradbury’ AND publisher LIKE ’%Del Ray’;
SQL JOINS

SELECT *
FROM book INNER JOIN book_chapter
ON book.isbn = book_chapter.isbn;
OUTER JOINS
SELECT book.title, author.last_name
FROM author
LEFT JOIN book ON book.author_id = author.id;
OUTER JOINS
SELECT book.title, author.last_name
FROM author
RIGHT JOIN book ON book.author_id =
author.id;
PHP DATA OBJECTS (PDO)
• The standard distribution of PHP 5.1 and greater includes
PDO and the drivers for SQLite by default
• There are many other database drivers for PDO, including:
• Microsoft SQL Server
• Firebird
• MySQL
• Oracle
• PostgreSQL, and
• ODBC
PHP DATA OBJECTS (PDO)
• Once installed, the process for using each driver is,
for the most part, the same because PDO provides a
unified data access layer to each of these
database engines.
• There is no longer a need for separate mysql_query()
or pg_query() functions.
• PDO provides a single object-oriented interface to
these databases.
DATABASE CONNECTION
$dsn = ’mysql:host=localhost;dbname=library’;
$dbh = new PDO($dsn, ’dbuser’, ’dbpass’);
ERROR HANDLING
try {
$dsn = ’mysql:host=localhost;dbname=library’;
$dbh = new PDO($dsn, ’dbuser’, ’dbpass’);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, TRUE);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// All other database calls go here

}
catch (PDOException $e)
{
echo ’Failed: ’ . $e->getMessage();
}
DATABASE QUERY WITH PDO
$author = ’’;
if (ctype_alpha($_GET[’author’])){
$author = $_GET[’author’];
}
// Escape the value of $author with quote()
$sql = ’SELECT author.*, book.* FROM author LEFT JOIN book ON author.id =
book.author_id WHERE author.last_name = ’ . $dbh->quote($author);
// Execute the statement and echo the results
$results = $dbh->query($sql);
foreach ($results as $row)

{
echo "{$row[’title’]}, {$row[’last_name’]}n";
}
DATABASE QUERY WITH PDO
$results = $dbh->query($sql);
$results->setFetchMode(PDO::FETCH_OBJ);
foreach ($results as $row)
{
echo "{$row->title}, {$row->last_name}n";
}
DATABASE QUERY WITH PDO
$sql = "INSERT INTO book (isbn, title, author_id,
publisher_id)
VALUES (’0395974682’, ’The Lord of the Rings’, 1,
3)";
$affected = $dbh->exec($sql);
echo "Records affected: {$affected}";
DATABASE
PROGRAMMING

More Related Content

What's hot (20)

XML and Databases
XML and DatabasesXML and Databases
XML and Databases
Cittrex
 
Sql commands
Sql commandsSql commands
Sql commands
Balakumaran Arunachalam
 
Sql commands
Sql commandsSql commands
Sql commands
Prof. Dr. K. Adisesha
 
introduction to NOSQL Database
introduction to NOSQL Databaseintroduction to NOSQL Database
introduction to NOSQL Database
nehabsairam
 
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
FUNCTION DEPENDENCY  AND TYPES & EXAMPLEFUNCTION DEPENDENCY  AND TYPES & EXAMPLE
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
Vraj Patel
 
Transaction management in DBMS
Transaction management in DBMSTransaction management in DBMS
Transaction management in DBMS
Megha Sharma
 
Schema
SchemaSchema
Schema
Pragya Srivastava
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
Stewart Rogers
 
Purpose of DBMS and users of DBMS
Purpose of DBMS and users of DBMSPurpose of DBMS and users of DBMS
Purpose of DBMS and users of DBMS
DharmamSavani
 
Acid properties
Acid propertiesAcid properties
Acid properties
Abhilasha Lahigude
 
Xml namespace
Xml namespaceXml namespace
Xml namespace
GayathriS578276
 
Entity Relationship Diagrams
Entity Relationship DiagramsEntity Relationship Diagrams
Entity Relationship Diagrams
sadique_ghitm
 
11. Storage and File Structure in DBMS
11. Storage and File Structure in DBMS11. Storage and File Structure in DBMS
11. Storage and File Structure in DBMS
koolkampus
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
Validation Controls in asp.net
Validation Controls in asp.netValidation Controls in asp.net
Validation Controls in asp.net
Deep Patel
 
File organization 1
File organization 1File organization 1
File organization 1
Rupali Rana
 
Entity Relationship Diagram
Entity Relationship DiagramEntity Relationship Diagram
Entity Relationship Diagram
Shakila Mahjabin
 
Integrity Constraints
Integrity ConstraintsIntegrity Constraints
Integrity Constraints
Megha yadav
 
Data integrity
Data integrityData integrity
Data integrity
Rahul Gupta
 
SQL Views
SQL ViewsSQL Views
SQL Views
baabtra.com - No. 1 supplier of quality freshers
 

Similar to Database Programming (20)

My sql
My sqlMy sql
My sql
Muhammad Umar
 
php databse handling
php databse handlingphp databse handling
php databse handling
kunj desai
 
Exalead managing terrabytes
Exalead   managing terrabytesExalead   managing terrabytes
Exalead managing terrabytes
Jérémie BORDIER
 
Introduction to my_sql
Introduction to my_sqlIntroduction to my_sql
Introduction to my_sql
Basavaraj Hampali
 
MYSQL-Database
MYSQL-DatabaseMYSQL-Database
MYSQL-Database
V.V.Vanniaperumal College for Women
 
Chapter 4 Structured Query Language
Chapter 4 Structured Query LanguageChapter 4 Structured Query Language
Chapter 4 Structured Query Language
Eddyzulham Mahluzydde
 
Session 2 - "MySQL Basics & Schema Design"
Session 2 - "MySQL Basics & Schema Design"Session 2 - "MySQL Basics & Schema Design"
Session 2 - "MySQL Basics & Schema Design"
LogaRajeshwaranKarth
 
Sql
SqlSql
Sql
YUCHENG HU
 
DATA MANAGEMENT computer science class 12 unit - 3 notes.pdf
DATA MANAGEMENT computer science class 12 unit - 3 notes.pdfDATA MANAGEMENT computer science class 12 unit - 3 notes.pdf
DATA MANAGEMENT computer science class 12 unit - 3 notes.pdf
NeetuPrasad16
 
Structured Query Language (SQL) _ Edu4Sure Training.pptx
Structured Query Language (SQL) _ Edu4Sure Training.pptxStructured Query Language (SQL) _ Edu4Sure Training.pptx
Structured Query Language (SQL) _ Edu4Sure Training.pptx
Edu4Sure
 
SQL
SQL SQL
SQL
Dr. C.V. Suresh Babu
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistence
Hugo Hamon
 
MySQL for beginners
MySQL for beginnersMySQL for beginners
MySQL for beginners
Saeid Zebardast
 
Introduction to sq lite
Introduction to sq liteIntroduction to sq lite
Introduction to sq lite
punu_82
 
2018 02 20_biological_databases_part2_v_upload
2018 02 20_biological_databases_part2_v_upload2018 02 20_biological_databases_part2_v_upload
2018 02 20_biological_databases_part2_v_upload
Prof. Wim Van Criekinge
 
Assignment 5Understanding SQL100 points (Questions 1 to 7 eac.docx
Assignment 5Understanding SQL100 points (Questions 1 to 7 eac.docxAssignment 5Understanding SQL100 points (Questions 1 to 7 eac.docx
Assignment 5Understanding SQL100 points (Questions 1 to 7 eac.docx
ssuser562afc1
 
New lecturer for computer science and it.ppt
New lecturer for computer science and it.pptNew lecturer for computer science and it.ppt
New lecturer for computer science and it.ppt
Farhat991731
 
Unit 3 rdbms study_materials-converted
Unit 3  rdbms study_materials-convertedUnit 3  rdbms study_materials-converted
Unit 3 rdbms study_materials-converted
gayaramesh
 
2016 02 23_biological_databases_part2
2016 02 23_biological_databases_part22016 02 23_biological_databases_part2
2016 02 23_biological_databases_part2
Prof. Wim Van Criekinge
 
Lecture 15 - MySQL- PHP 1.ppt
Lecture 15 - MySQL- PHP 1.pptLecture 15 - MySQL- PHP 1.ppt
Lecture 15 - MySQL- PHP 1.ppt
TempMail233488
 
php databse handling
php databse handlingphp databse handling
php databse handling
kunj desai
 
Session 2 - "MySQL Basics & Schema Design"
Session 2 - "MySQL Basics & Schema Design"Session 2 - "MySQL Basics & Schema Design"
Session 2 - "MySQL Basics & Schema Design"
LogaRajeshwaranKarth
 
DATA MANAGEMENT computer science class 12 unit - 3 notes.pdf
DATA MANAGEMENT computer science class 12 unit - 3 notes.pdfDATA MANAGEMENT computer science class 12 unit - 3 notes.pdf
DATA MANAGEMENT computer science class 12 unit - 3 notes.pdf
NeetuPrasad16
 
Structured Query Language (SQL) _ Edu4Sure Training.pptx
Structured Query Language (SQL) _ Edu4Sure Training.pptxStructured Query Language (SQL) _ Edu4Sure Training.pptx
Structured Query Language (SQL) _ Edu4Sure Training.pptx
Edu4Sure
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistence
Hugo Hamon
 
Introduction to sq lite
Introduction to sq liteIntroduction to sq lite
Introduction to sq lite
punu_82
 
2018 02 20_biological_databases_part2_v_upload
2018 02 20_biological_databases_part2_v_upload2018 02 20_biological_databases_part2_v_upload
2018 02 20_biological_databases_part2_v_upload
Prof. Wim Van Criekinge
 
Assignment 5Understanding SQL100 points (Questions 1 to 7 eac.docx
Assignment 5Understanding SQL100 points (Questions 1 to 7 eac.docxAssignment 5Understanding SQL100 points (Questions 1 to 7 eac.docx
Assignment 5Understanding SQL100 points (Questions 1 to 7 eac.docx
ssuser562afc1
 
New lecturer for computer science and it.ppt
New lecturer for computer science and it.pptNew lecturer for computer science and it.ppt
New lecturer for computer science and it.ppt
Farhat991731
 
Unit 3 rdbms study_materials-converted
Unit 3  rdbms study_materials-convertedUnit 3  rdbms study_materials-converted
Unit 3 rdbms study_materials-converted
gayaramesh
 
Lecture 15 - MySQL- PHP 1.ppt
Lecture 15 - MySQL- PHP 1.pptLecture 15 - MySQL- PHP 1.ppt
Lecture 15 - MySQL- PHP 1.ppt
TempMail233488
 
Ad

More from Henry Osborne (20)

Android Fundamentals
Android FundamentalsAndroid Fundamentals
Android Fundamentals
Henry Osborne
 
Open Source Education
Open Source EducationOpen Source Education
Open Source Education
Henry Osborne
 
Security Concepts - Linux
Security Concepts - LinuxSecurity Concepts - Linux
Security Concepts - Linux
Henry Osborne
 
Networking Basics with Linux
Networking Basics with LinuxNetworking Basics with Linux
Networking Basics with Linux
Henry Osborne
 
Disk and File System Management in Linux
Disk and File System Management in LinuxDisk and File System Management in Linux
Disk and File System Management in Linux
Henry Osborne
 
Drawing with the HTML5 Canvas
Drawing with the HTML5 CanvasDrawing with the HTML5 Canvas
Drawing with the HTML5 Canvas
Henry Osborne
 
HTML5 Multimedia Support
HTML5 Multimedia SupportHTML5 Multimedia Support
HTML5 Multimedia Support
Henry Osborne
 
Information Architecture
Information ArchitectureInformation Architecture
Information Architecture
Henry Osborne
 
Interface Design
Interface DesignInterface Design
Interface Design
Henry Osborne
 
Universal Usability
Universal UsabilityUniversal Usability
Universal Usability
Henry Osborne
 
Website Security
Website SecurityWebsite Security
Website Security
Henry Osborne
 
XML and Web Services
XML and Web ServicesXML and Web Services
XML and Web Services
Henry Osborne
 
Elements of Object-oriented Design
Elements of Object-oriented DesignElements of Object-oriented Design
Elements of Object-oriented Design
Henry Osborne
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
Henry Osborne
 
Web Programming
Web ProgrammingWeb Programming
Web Programming
Henry Osborne
 
PHP Strings and Patterns
PHP Strings and PatternsPHP Strings and Patterns
PHP Strings and Patterns
Henry Osborne
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
Henry Osborne
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
Henry Osborne
 
Activities, Fragments, and Events
Activities, Fragments, and EventsActivities, Fragments, and Events
Activities, Fragments, and Events
Henry Osborne
 
Establishing a Web Presence
Establishing a Web PresenceEstablishing a Web Presence
Establishing a Web Presence
Henry Osborne
 
Android Fundamentals
Android FundamentalsAndroid Fundamentals
Android Fundamentals
Henry Osborne
 
Open Source Education
Open Source EducationOpen Source Education
Open Source Education
Henry Osborne
 
Security Concepts - Linux
Security Concepts - LinuxSecurity Concepts - Linux
Security Concepts - Linux
Henry Osborne
 
Networking Basics with Linux
Networking Basics with LinuxNetworking Basics with Linux
Networking Basics with Linux
Henry Osborne
 
Disk and File System Management in Linux
Disk and File System Management in LinuxDisk and File System Management in Linux
Disk and File System Management in Linux
Henry Osborne
 
Drawing with the HTML5 Canvas
Drawing with the HTML5 CanvasDrawing with the HTML5 Canvas
Drawing with the HTML5 Canvas
Henry Osborne
 
HTML5 Multimedia Support
HTML5 Multimedia SupportHTML5 Multimedia Support
HTML5 Multimedia Support
Henry Osborne
 
Information Architecture
Information ArchitectureInformation Architecture
Information Architecture
Henry Osborne
 
XML and Web Services
XML and Web ServicesXML and Web Services
XML and Web Services
Henry Osborne
 
Elements of Object-oriented Design
Elements of Object-oriented DesignElements of Object-oriented Design
Elements of Object-oriented Design
Henry Osborne
 
PHP Strings and Patterns
PHP Strings and PatternsPHP Strings and Patterns
PHP Strings and Patterns
Henry Osborne
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
Henry Osborne
 
Activities, Fragments, and Events
Activities, Fragments, and EventsActivities, Fragments, and Events
Activities, Fragments, and Events
Henry Osborne
 
Establishing a Web Presence
Establishing a Web PresenceEstablishing a Web Presence
Establishing a Web Presence
Henry Osborne
 
Ad

Recently uploaded (20)

Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptxRai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
National Information Standards Organization (NISO)
 
LDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad UpdatesLDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad Updates
LDM & Mia eStudios
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
Gibson "Secrets to Changing Behaviour in Scholarly Communication: A 2025 NISO...
National Information Standards Organization (NISO)
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
Quiz Club of PSG College of Arts & Science
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptxCapitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
LDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad UpdatesLDMMIA Reiki Yoga Next Week Grad Updates
LDMMIA Reiki Yoga Next Week Grad Updates
LDM & Mia eStudios
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
IDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptxIDF 30min presentation - December 2, 2024.pptx
IDF 30min presentation - December 2, 2024.pptx
ArneeAgligar
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptxCapitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 

Database Programming

  • 2. DATABASE A comprehensive collection of related data organized for convenient access, generally in a computer
  • 3. RELATIONAL DATABASE A relational database is a collection of data items organized as a set of formally-described tables from which data can be accessed or reassembled in many different ways without having to reorganize the database tables. The relational database was invented by E. F. Codd at IBM in 1970. http://searchsqlserver.techtarget.com/definition/relational-database
  • 4. INDICES • Make it possible to organize the data in a table according to one or more columns. • One of the cardinal elements of relational databases • Can usually be created on one or more columns of a table • Can also be declared as unique • Primary keys are a special type of unique index that is used to determine the “natural” method of uniquely identifying a row in a table
  • 5. RELATIONSHIPS • One-to-one: at most, one row in the child table can correspond to each row in the parent table • One-to-many: an arbitrary number of rows in the child table can correspond to any one row in the parent table • Many-to-many: an arbitrary number of rows in the child table can correspond to an arbitrary number of rows in the parent table
  • 6. DATA TYPES int or integer smallint real float char varchar Signed integer number, 32 bits in length. Signed integer number, 16 bits in length. Signed floating-point number, 32 bits in length. Signed floating-point number, 64 bits in length. Fixed-length character string. Variable-length character string.
  • 7. CREATING DATABASES CREATE DATABASE <dbname>; CREATE SCHEMA <dbname>;
  • 8. CREATE TABLES CREATE TABLE <tablename> ( <col1name> <col1type> [<col1attributes>], [... <colnname> <colntype> [<colnattributes>]] );
  • 9. CREATE TABLES CREATE TABLE book ( id INT NOT NULL PRIMARY KEY, isbn VARCHAR(13), title VARCHAR(255), author VARCHAR(255), publisher VARCHAR(255) );
  • 10. CREATING INDICES AND RELATIONSHIPS CREATE INDEX <indexname> ON <tablename> (<column1>[, ..., <columnn>]); CREATE INDEX book_isbn ON book (isbn);
  • 11. CREATING INDICES AND RELATIONSHIPS CREATE TABLE book_chapter ( isbn VARCHAR(13) REFERENCES book (id), chapter_number INT NOT NULL, chapter_title VARCHAR(255) );
  • 12. DROPPING OBJECTS DROP TABLE book_chapter; DROP SCHEMA my_book_database;
  • 13. ADDING/MANIPULATING DATA INSERT INTO <tablename> VALUES (<field1value>[, ..., <fieldnvalue>]); OR INSERT INTO <tablename> (<field1>[, ..., <fieldn>]) VALUES (<field1value>[, ..., <fieldnvalue>]);
  • 14. ADDING/MANIPULATING DATA INSERT INTO book (isbn, title, author) VALUES (’0812550706’, ’Ender’s Game’, ’Orson Scott Card’);
  • 15. ADDING/MANIPULATING DATA To update records, you can use the UPDATE statement. UPDATE book SET publisher = ’Tor Science Fiction’, author = ’Orson S. Card’ WHERE isbn = ’0812550706’;
  • 16. REMOVE DATA DELETE FROM book; DELETE FROM book WHERE isbn = ’0812550706’;
  • 17. RETRIEVE DATA SELECT * FROM book; SELECT * FROM book WHERE author = ’Ray Bradbury’; SELECT * FROM book WHERE author = ’Ray Bradbury’ OR author = ’George Orwell’; SELECT * FROM book WHERE author = ’Ray Bradbury’ AND publisher LIKE ’%Del Ray’;
  • 18. SQL JOINS SELECT * FROM book INNER JOIN book_chapter ON book.isbn = book_chapter.isbn;
  • 19. OUTER JOINS SELECT book.title, author.last_name FROM author LEFT JOIN book ON book.author_id = author.id;
  • 20. OUTER JOINS SELECT book.title, author.last_name FROM author RIGHT JOIN book ON book.author_id = author.id;
  • 21. PHP DATA OBJECTS (PDO) • The standard distribution of PHP 5.1 and greater includes PDO and the drivers for SQLite by default • There are many other database drivers for PDO, including: • Microsoft SQL Server • Firebird • MySQL • Oracle • PostgreSQL, and • ODBC
  • 22. PHP DATA OBJECTS (PDO) • Once installed, the process for using each driver is, for the most part, the same because PDO provides a unified data access layer to each of these database engines. • There is no longer a need for separate mysql_query() or pg_query() functions. • PDO provides a single object-oriented interface to these databases.
  • 23. DATABASE CONNECTION $dsn = ’mysql:host=localhost;dbname=library’; $dbh = new PDO($dsn, ’dbuser’, ’dbpass’);
  • 24. ERROR HANDLING try { $dsn = ’mysql:host=localhost;dbname=library’; $dbh = new PDO($dsn, ’dbuser’, ’dbpass’); $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, TRUE); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // All other database calls go here } catch (PDOException $e) { echo ’Failed: ’ . $e->getMessage(); }
  • 25. DATABASE QUERY WITH PDO $author = ’’; if (ctype_alpha($_GET[’author’])){ $author = $_GET[’author’]; } // Escape the value of $author with quote() $sql = ’SELECT author.*, book.* FROM author LEFT JOIN book ON author.id = book.author_id WHERE author.last_name = ’ . $dbh->quote($author); // Execute the statement and echo the results $results = $dbh->query($sql); foreach ($results as $row) { echo "{$row[’title’]}, {$row[’last_name’]}n"; }
  • 26. DATABASE QUERY WITH PDO $results = $dbh->query($sql); $results->setFetchMode(PDO::FETCH_OBJ); foreach ($results as $row) { echo "{$row->title}, {$row->last_name}n"; }
  • 27. DATABASE QUERY WITH PDO $sql = "INSERT INTO book (isbn, title, author_id, publisher_id) VALUES (’0395974682’, ’The Lord of the Rings’, 1, 3)"; $affected = $dbh->exec($sql); echo "Records affected: {$affected}";

Editor's Notes

  • #7: SQL supports a number of data types, which provide a greater degree of flexibility than PHP in how the data is stored and representedchar string will always have a fixed length, regardless of how many characters it contains (the string is usually padded with spaces to the column’s length). In both cases, however, a string column must be given a length (usually between 1 and 255 characters, al-though some database systems do not follow this rule), which means that any string coming from PHP, where it can have an arbitrary length, can be truncated, usually without even a warning, thus resulting in the loss of data.Most database systems also define an arbitrary-length character data type (usually called text) that most closely resembles PHP’s strings. However, this data type usually comes with a number of strings attached (such as a maximum allowed length and severe limitations on search and indexing capabilities). Therefore, you will still be forced to use char and (more likely) varchar, with all of their limitations.
  • #8: The formal definition of database schema is a set of formulas (sentences) called integrity constraints imposed on a database. These integrity constraints ensure compatibility between parts of the schema. All constraints are expressible in the same language. ~http://en.wikipedia.org/wiki/Database_schema
  • #11: Indices can be created (as was the example with the primary key above) while you are creating a table; alternatively, you can create them separately at a later point in time
  • #12: Foreign-key relationships are created either when a table is created, or at a later date with an altering statement. For example, suppose we wanted to add a table that contains a list of all of the chapter titles for every bookThis code creates a one-to-many relationship between the parent table book and the child table book_chapter based on the isbn field. Once this table is created, you can only add a row to it if the ISBN you specify exists in book.
  • #13: The act of deleting an object from a schema—be it a table, an index, or even the schema itself—is called dropping. It is performed by a variant of the DROP statementA good database system that supports referential integrity will not allow you to drop a table if doing so would break the consistency of your data. Thus, deleting the book table cannot take place until book_chapter is dropped first.The same technique can be used to drop an entire schema
  • #14: The first form of the INSERT statement is used when you want to provide values for every column in your table—in this case, the column values must be specified in the same order in which they appear in the table declaration.In its second form, the INSERT statement consists of three main parts. The first part tells the database engine into which table to insert the data. The second part indicates the columns for which we’re providing a value; finally, the third part contains the actual data to insert.
  • #17: DELETE FROM book;This simple statement will remove all records from the book table, leaving behind an empty table.
  • #18: To retrieve data from any SQL database engine, you must use a SELECT statement; SELECT statements range from very simple to incredibly complex, depending on your needsSELECT * FROM bookWHERE author = ’Ray Bradbury’ OR author = ’George Orwell’;SELECT * FROM bookWHERE author = ’Ray Bradbury’ AND publisher LIKE ’%Del Ray’;The first example statement contains an OR clause and, thus, broadens the results to return all books by each author, while the second statement further restricts the results with an AND clause to all books by the author that were also published by a specific publisher. Note, here, the use of the LIKE operator, which provides a case-insensitive match and allows the use of the % wild character to indicate an arbi-trary number of characters. Thus, the expression AND publisher LIKE ’%Del Ray’ will match any publisher that ends in the string del ray, regardless of case.
  • #19: Joins combine data from multiple tables to create a single recordset.There are two basic types of joins: inner joins and outer joins. In both cases, joins create a link between two tables based on a common set of columns (keys).An inner join returns rows from both tables only if keys from both tables can be found that satisfies the join conditions.
  • #20: Outer joins return all records from one table, while restricting the other table to matching records, which means that some of the columns in the results will contain NULL values.Left joins are a type of outer join in which every record in the left table that matches the WHERE clause (if there is one) will be returned regardless of a match made in the ON clause of the right table.
  • #21: Right joins are analogous to left joins—only reversed: instead of returning all results from the “left” side, the right join returns all results from the “right” side, restricting results from the “left” side to matches of the ON clause.Here, the table on the left is still the author table, and the right table is still the book table, but, this time, the results returned will include all records from the book table and only those from the author table that match the ON clause where book.author_id = author.id
  • #24: To connect to a database, PDO requires at least a Data Source Name, or DSN, format-ted according to the driver used. Detailed DSN formatting documentation exists on the PHP Web site for each driver. Additionally, if your database requires a username or password, PDO will need these to access the database.
  • #25: This is not only a best practice, but it is very useful in debugging. Note that the default error mode for PDO is PDO::ERRMODE_SILENT, which means that it will not emit any warnings or error messages. For the examples the error mode is set to PDO::ERRMODE_EXECEPTION. This causes PDO to throw a PDOExecption when an error occurs. This exception can be caught and displayed for debugging purposes.
  • #26: To retrieve a result set from a database using PDO, use the PDO::query() method.To escape a value included in a query (e.g. from $_GET, $_POST, $_COOKIE, etc.) use the PDO::quote() method. PDO will ensure that the string is quoted properly for the database used.The method PDO::query() returns a PDOStatement object.
  • #27: By default, the fetch mode for a PDOStatement is PDO::FETCH_BOTH, which means that it will return an array containing both associative and numeric indexes. It is possible to change the PDOStatement object to return, for example, an object instead of an array so that each column in the result set may be accessed as properties of an object instead of array indices.
  • #28: To execute an INSERT, UPDATE, or DELETE statement against a database, PDO provides the PDO::exec() method. The PDO::exec() method executes an SQL statement and returns the number of rows affected.