> They do not scale well — You do not have random access to data in flat file data bases. You have only sequential access. This means that any search function has to scan each line in the file, one by one, to look for specific information. As the size of the database grows, access times increase and performance decreases.
> Flat file databases are unsuitable for multi-user environments — Depending on
how the database is set up, it either enables only one user to access it at a time or allows two users to make changes simultaneously, making changes that could end up overwriting each other and causing data loss.
These limitations obviously make the flat file database unsuitable for any kind of serious work in even a small business — much less in an enterprise environment. Relational databases, or relational database management systems (RDBMSs), to give them their full name, are good at finding the relationships between individual pieces of data. An RDBMS stores data in tables with fields much like those in spreadsheets, making the data searchable and sortable. RDBMSs are the focus of this chapter.
Oracle, DB2, Microsoft SQL Server, and the freely available PostgreSQL and MySQL are all examples of RDBMSs. The following sections discuss how relational databases work and provide a closer look at some of the basic processes involved in administering and using databases. You will also learn about SQL, the standard language used to store, retrieve, and manipulate database data.
How Relational Databases Work
An RDBMS stores data in tables, which you can visualize as spreadsheets. Each column in the table is a field; for example, a column might contain a name or an address. Each row in the table is an individual record. The table itself has a name you use to refer to that table when you want to get data out of it or put data into it. Figure 18.1 shows an example of a simple relational database that stores name and address information.

FIGURE 18.1 In this visualization of how an RDBMS stores data, the database stores four records (rows) that include name and address information, divided into seven fields (columns) of data.
In the example shown in Figure 18.1, the database contains only a single table. Most RDBMS setups are much more complex than this, with a single database containing multiple tables. Figure 18.2 shows an example of a database named sample_database that contains two tables.
In the sample_database
example, the phonebook
table contains four records (rows) and each record hold three fields (columns) of data. The cd_collection
table holds eight records, divided into five fields of data.
If you are thinking that there is no logical relationship between the phonebook
table and the cd_collection
table in the sample_database
example, you are correct. In a relational database, users can store multiple tables of data in a single database — even if the data in one table is unrelated to the data in others.
For example, suppose that you run a small company that sells widgets and you have a computerized database of customers. In addition to storing each customer's name, address, and phone number, you want to be able to look up outstanding order and invoice information for any of your customers. You could use three related tables in an RDBMS to store and organize customer data for just those purposes. Figure 18.3 shows an example of such a database.

FIGURE 18.2 A single database can contain two tables — in this case, phonebook
and cd_collection
.
In the example in Figure 18.3, we have added a Customer ID
field to each customer record. This field holds a customer ID number that is the unique piece of information that can be used to link all other information for each customer to track orders and invoices. Each customer is given an ID unique to him; two customers might have the same data in their name fields, but their ID
field values will never be the same. The Customer ID
field data in the Orders
and Overdue
tables replaces the Last Name, First Name
, and Shipping Address
field information from the Customers
table. Now, when you want to run a search for any customer's order and invoice data, you can search based on one key rather than multiple keys. You get more accurate results in faster, easier-to-conduct data searches.

FIGURE 18.3 You can use three related tables to track customers, orders, and outstanding invoices.
Now that you have an idea of how data is stored in an RDBMS and how the RDBMS structure enables you to work with that data, you are ready to learn how to input and output data from the database. This is where SQL comes in.
Understanding SQL Basics
SQL (pronounced 'S-Q-L') is a database query language understood by virtually all RDBMSs available today. You use SQL statements to get data into and retrieve data from a database. As with statements in any language, SQL statements have a defined structure that determines their meanings and functions.
As a DBA, you should understand the basics of SQL, even if you will not be doing any of the actual programming yourself. Fortunately, SQL is similar to standard English, so learning the basics is simple.
As mentioned previously, an RDBMS stores data in tables that look similar to spread sheets. Of course, before you can store any data in a database, you need to create the necessary tables and columns to store the data. You do this by using the CREATE
statement. For example, the cd_collection
table from Figure 18.2 has five columns, or fields: id, title, artist, year
, and rating
.
SQL provides several column types for data that define what kind of data will be stored in the column. Some of the available types are INT, FLOAT, CHAR
, and VARCHAR
. Both CHAR
and VARCHAR
hold text strings, with the difference being that CHAR
holds a fixed-length string, whereas VARCHAR
holds a variable-length string.
There are also special column types, such as DATE, which takes data in only a date format, and ENUMs
(enumerations), which can be used to specify that only certain values are allowed. If, for example, you wanted to record the genres of your CDs, you could use an ENUM
column that accepts only the values POP, ROCK, EASY_LISTENING
, and so on. You will learn more about ENUM later in this chapter.
Looking at the cd_collection
table, you can see that three of the columns hold numerical data and the other two hold string data. In addition, the character strings are of variable length. Based on this information, you can discern that the best type to use for the text columns is type VARCHAR,
and the best type to use for the others is INT. You should notice something else about the cd_collection
table: One of the CDs is missing a rating, perhaps because we have not listened to it yet. This value, therefore, is optional; it starts empty and can be filled in later.
You are now ready to create a table. As mentioned before, you do this by using the CREATE
statement, which uses the following syntax:
CREATE TABLE
You should know the following about the CREATE statement: