Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
PHP Programming With MySQL Second Edition.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
43.07 Mб
Скачать

Introduction to Databases

A common use of Web pages is to gather information stored in a

database on a Web server. Most server-side scripting languages,

Including php, allow you to create Web pages that can read and write

data to and from databases. In this chapter, you will take a break from

PHP to learn how to work with MySQL databases. Your goal is to

learn the basics of database manipulation. Then, in Chapter 8, you

will apply many of the techniques from this chapter to PHP scripts

that manipulate MySQL databases.

MySQL is an open source database originally developed by MySQL

AB and owned by Sun Microsystems (http://www.mysql.com/). Many

people mistakenly believe that MySQL is part of PHP. Even though

MySQL is probably the database used most often with PHP, it is just

one of many databases that PHP can manipulate directly or through

Open Database Connectivity (ODBC). As its name implies, MySQL

uses Structured Query Language, or SQL, as its data manipula-

tion language. MySQL is primarily used for Web applications and is

extremely popular for several reasons; first and foremost, it’s open

source and free.

381

Introduction to Databases

Formally defined, a database is an ordered collection of information

that a computer program can quickly access. You can probably think

of many databases that you work with in everyday life. For example,

your address book is a database. So is the card file of recipes in a

kitchen. Other examples of databases include a company’s employee

directory and a file cabinet of client information. Essentially, any

Information that can be organized into ordered sets of data, and

then quickly retrieved, can be considered a database. A collection

of hundreds of baseball cards thrown into a shoebox is not a data-

base because an individual card cannot be quickly or easily retrieved

(except by luck). However, if the baseball card collection was orga-

nized in binders by team, and then further organized according to

each player’s field position or batting average, it could be considered a

database because you could quickly locate a specific card.

The information stored in computer databases is actually stored in

tables similar to spreadsheets. Each row in a database table is called

a record. A record in a database is a single, complete set of related

Information. Each recipe in a recipe database, for instance, is a single

database record. Each column in a database table is called a field.

Fields are the individual categories of information stored in a record.

Examples of fields in a recipe database might include ingredients,

cooking time, cooking temperature, and so on.

CHAPTER 7

Working with Databases and MySQL

382

To summarize, you can think of databases as consisting of tables,

which consist of records, which consist of fields. Figure 7-1 shows an

example of an employee directory for programmers at an applica-

tion development company. The database consists of five records,

one for each employee. Each record consists of six fields: last_name,

first_name, address, city, state, and zip.

Figure 7-1

Employee directory database

Two other

types of

database

systems you

might

encounter are hierarchical

databases and network

databases.

The database in Figure 7-1 is an example of a flat-file database, one

of the simplest types of databases. A flat-file database stores infor-

mation in a single table, and it is usually adequate for simple collec-

tions of information. However, with large and complex collections of

information, a better solution is a relational database, which stores

information across multiple related tables. Although you will not

work with a relational database in this chapter, understanding how

they work is helpful because relational databases are among the most

common in use today.

Understanding Table Relationships

Relational databases consist of one or more related tables. In fact,

large relational databases can consist of hundreds or thousands of

related tables. Regardless of the number of tables, however, you cre-

ate relationships within the database by working with two tables at

a time. One table in a relationship is always considered to be the

primary table, and the other table is considered the related table.

A primary table (also called a parent table) is the main table in

a relationship that is referenced by another table. A related table

(also called a child table) references a primary table in a relational

database.


Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]