Collection Contents Index Database computing concepts PART 2.  Getting Results with SQL Anywhere Studio pdf/chap2.pdf

First Guide to SQL Anywhere Studio
   PART 1. First Steps
     CHAPTER 2. Database Terms and Concepts       

Relational database concepts


A relational database management system (RDBMS) is a system for storing and retrieving data, in which the data is represented in tables. A relational database consists of a collection of tables that store interrelated data.

This section introduces some of the terms and concepts that are important in talking about relational databases.

Top of page  Database tables

In a relational database, all data is held in tables, which are made up of rows and columns.

Each table has one or more columns, and each column is assigned a specific data type, such as an integer number, a sequence of characters (for text), or a date. Each row in the table has a value for each column.

A typical fragment of a table containing employee information may look as follows:

emp_ID

emp_lname

emp_fname

emp_phone

10057

Huong

Zhang

1096

10693

Donaldson

Anne

7821

Characteristics of relational tables 

The tables of a relational database have some important characteristics:

The following table lists some of the formal and informal relational database terms describing tables and their contents, together with their equivalent in other nonrelational databases. This manual uses the informal terms.

Formal relational term

Informal relational term

Equivalent nonrelational term

Relation

Table

File

Attribute

Column

Field

Tuple

Row

Record

What do you keep in each table? 

When you are designing your database, you should make sure that each table in the database holds information about a specific thing, such as employees, products, or customers.

By designing a database this way you can set up a structure that eliminates redundancy and the possible inconsistencies caused by redundancy. For example, both the sales and accounts payable departments might enter and look up information about customers. In a relational database, the information about customers is entered only once, in a table that both departments can access.

A relational database is not a set of unrelated tables. You use primary and foreign keys to describe relationships between the information in different tables.

Top of page  Primary and foreign keys

Primary and foreign keys enable each row in the database tables to be identified, and define the relationships between the tables to be defined. These keys define the relational structure of a database.

Top of page  Tables have a primary key

All tables in a relational database should have a primary key. The primary key is a column, or set of columns, that allows each row in the table to be uniquely identified. No two rows in a table with a primary key may have the same value of a primary key.

If no primary key is assigned, all the columns together become the primary key. It is good practice to keep your primary key for each table as compact as possible.

Examples 

In a table holding information about employees, the primary key may be an ID number assigned to each employee.

In the sample database, the table of sales order items has the following columns:

In order to identify a particular item, both the order number and the line number are required. The primary key is made up of both these columns.

Top of page  Tables are related by foreign keys

The information in one table is related to that in other tables by foreign keys.

Example 

The sample database has one table holding employee information and one table holding department information. The department table has the following columns:

To find the name of a particular employee's department, there is no need to put the name of the employee's department into the employee table. Instead, the employee table contains a column holding the department ID of the employee's department. This is called a foreign key to the department table. A foreign key references a particular row in the table containing the corresponding primary key.

In this example, the employee table (which contains the foreign key in the relationship) is called the foreign table or referencing table. The department table (which contains the referenced primary key) is called the primary table or the referenced table.

Top of page  Other database objects

A relational database holds more than a set of related tables. Among the other objects that make up a relational database are:

Top of page  Queries

The "Q" in "SQL" stands for query. You query (retrieve) data from a database with the SELECT statement. The basic query operations in a relational system are projection, restriction, and join. The SELECT statement implements all of them.

Projections and restrictions 

A projection is a subset of the columns in a table. A restriction (also called selection) is a subset of the rows in a table, based on some conditions.

For example, the following SELECT statement retrieves the names and prices of all products that cost more than $15:

SELECT name, unit_price
FROM product
WHERE unit_price > 15 

This query uses both a restriction (WHERE unit_price > 15) and a projection (SELECT name, unit_price)

Joins 

A join links the rows in two or more tables by comparing the values in key columns and returning rows that have matching values. For example, you might want to select the order item identification numbers and product names for all order items that were shipped more than a dozen items:

SELECT sales_order_items.id, product.name
FROM product KEY JOIN sales_order_items
WHERE sales_order_items.quantity > 12

The product table and the sales_order_items table are joined together based on the foreign key relationships between them.

Top of page  Other SQL statements

You can do more with SQL than just query. SQL includes statements that create tables, views, and other database objects. It also includes statements that modify tables (the INSERT, UPDATE, and DELETE statements), and commands that perform many other database tasks discussed in this manual.

Top of page  The system tables

Every database contains a set of system tables, these are special tables that the system uses to manage data and the system. These tables are also sometimes called the data dictionary or the system catalog.

System tables contain information about the database. You never alter the system tables directly in the way that you can alter other tables. The system tables hold information about the tables in a database, the users of a database, the columns in each table, and so on. This information is data about data, or metadata.

Top of page  

Collection Contents Index Database computing concepts PART 2.  Getting Results with SQL Anywhere Studio pdf/chap2.pdf