Database Programming
Post on 10-May-2015
1009 Views
Preview:
DESCRIPTION
Transcript
DATABASE PROGRAMMINGHenry 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
Signed integer number, 32 bits in length.
smallint Signed integer number, 16 bits in length.real Signed floating-point number, 32 bits in
length.float Signed floating-point number, 64 bits in
length.char Fixed-length character string.varchar 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 bookSET 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_chapterON book.isbn = book_chapter.isbn;
OUTER JOINS
SELECT book.title, author.last_nameFROM authorLEFT JOIN book ON book.author_id = author.id;
OUTER JOINS
SELECT book.title, author.last_nameFROM authorRIGHT 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
top related