04 - Tables Overview
04 - Tables Overview
You’ve
been introduced to the concept of a table within a database by now and you have
also reviewed a few examples of tables. And you’ve gained a basic understanding
of what a primary key does in a table.
The main objective of this reading is to examine tables in more depth in terms of
the structure of a table, data types, primary and foreign keys and the role they play
in a table, as well as table constraints.
To refresh your memory, a table is the most basic type of database object in
relational databases. It is responsible for storing data in the database. Like any
other table, a database table also consists of rows and columns.
Just like in a spreadsheet of data, there are rows that run horizontally. These rows
represent each record. Rows in turn span multiple columns.
Columns run vertically. They are like the definition of each field. Each column has a
name that describes the data that is stored in it. Examples of column names could
include FirstName, LastName, ProductID, Price and so forth.
Where the row intersects with a column is where a cell is located. A cell is where
you store an item of data.
For example, here are some of the data types that are available:
student ID,
first name,
last name,
date of birth,
home address,
and faculty.
These are the table's columns.
There are also six rows within this table; one for each student. In other words, the
table contains the records of six students.
Each cell in a row or record contains a piece of data such as student ID = 1, first
name = Emily, last name = Williams and so on.
The student ID would probably have a data type of INT, for example. First name
and last name would have a data type of VARCHAR and date of birth would have a
data type of DATE.
Each table or relation in a database has its own schema. Schema simply means the
structure. The structure includes:
In some cases, the primary key can comprise more than one column or field. This
happens when a single column cannot make a record in a table uniquely
identifiable. For example, in the table below, the EMP_ID values aren’t unique, so
the column is not unique by itself. Thus, this column alone cannot be used as the
primary key of this table. However, the EMP_ID and DEPT_ID columns together can
make a record unique. Therefore, the primary key of this table is EMP_ID and
DEPT_ID. This is also known as a composite primary key.
1. Key constraints
2. Domain constraints
In this reading, you learned more about tables in a relational database as you
explored the table in terms of its structure, data types, constraints, and the role of
primary and foreign keys.