HOW TO DESIGN A DATABASE (How To Design A Data Structure (The 6 basic…
HOW TO DESIGN A DATABASE
How To Normalize A Data Structure
When And How To Denormalize A Data Structure
denormalization when necessary
. It is
better to adhere to the normal forms unless it is clear that performance will be improved by denormalizing
result in larger tables, redundant data, and reduced performance.
to some extent,
usually to the third normal form (3NF)
that work with tables
that are normalized to the fourth normal form (4NF) and beyond
typically more difficult to code and debug
Data structures that are normalized to the fourth normal form and beyond
typically require more joins than tables normalized to the third normal form and can therefore be less efficient
When To Denormalize
Include columns with derived values when those values are used frequently in search conditions
. If you do that,
you need to be sure that the column value is always synchronized with the value of the columns it's derived from
If a table is updated infrequently
, you should
consider denormalizing it to improve efficiency.
Because that data remains relatively constant, you
don't have to worry about data redundancy errors once the initial data is entered and verified.
When a column from a joined table is used repeatedly in search criteria
, you should
consider moving that column to the primary key table if it will eliminate the need for a join
How To Apply The Third Normal Form
If a column doesn't depend only on the primary key, it implies that the column is assigned to the wrong table or that it can be computed from other columns in the table.
A column that can be computed from other columns contains
For a table to be in third normal form,
every non-key column must depend
on the primary key
How To Apply The Second Normal Form
Second normal form
helps remove redundant row data,
which can save storage space, make maintenance easier, and reduce the chance of storing inconsistent data.
To apply second normal form, you
move columns that don't depend on the entire primary key to another table
then establish a relationship between the two tables.
For a table to be in second normal form,
every non-key column must depend on the entire primary key
If a column doesn't depend on the entire key, it indicates that the table contains information for more than one entity
. This is reflected by the table's composite key.
How To Apply The First Normal Form
A table in first normal form often has repeating values in its rows
This can be resolved by applying the second normal form
For a table to be in first normal form (1NF),
its columns must not contain repeating values.
each column must contain a single, scalar value
. In addition,
the table must no contain repeating columns that represent a set of values.
The 7 Normal Forms
A database is typically considered to be normalized if it's in third normal form (3NF).
The other four forms are not commonly used.
Each normal form assumes that the design is already in the previous normal form
The Benefits Of Normalization
Data redundancy is minimized,
simplifies maintenance and reduces storage
Each table has fewer indexes, which makes insert, update, and delete operations more efficient
b. Since each table contains information about a single entity,
each index has fewer columns (usually one) and fewer rows. And that makes data retrieval and insert, update, and delete operations more efficient.
a. Since a normalized database has more tables than an unnormalized database, and since each table has an index on its primary key,
the database has more indexes. And that make data retrieval more efficient.
: Every constraint on the relationship is dependent only on key constraints and domain constraints, where a
is the set of allowable values for a column. This form prevents the insertion of any unacceptable data by enforcing constraints at the level of a relationship, rather than at the table of column level, DKNF is less a design model than an abstract "ultimate" normal form.
: The data structure is split into smaller and smaller tables until all redundancy has been eliminated. If further splitting would result in tables that couldn't be joined to recreate the original table, the structure is in fifth normal format.
: A non-key column can't be dependent on another non-key column. This prevents
, where column A depends on column C and column B depends on column C.Since both A and B depend on C, A and B should be moved into another table with C as the key.
: A table must not have more than one
, where the primary key has a one-to-many relationship to non-key columns. This form gets rid of
misleading many-to-many relationships
: Every non-key column must depend only on the primary key
: Every non-key column must depend on the entire primary key
: The value stored at the intersections of each row and column must be a scalar value, and a table must not contain any repeating columns.
How To Design A Data Structure
How To Identify The Columns To Be Indexed
: an index that consists of two or more columns.
: when a DBMS searches through the entire table, because no index exists.
: a structure that provides for locating one or more rows dierctly.
Because indexes must be updated each time you add, update, or delete a row,
you shouldn't create more indexes that you need
that include two or more columns
. You should
use this type of index when the columns in the index are updated infrequently
when the index will cover almost every search condition on the table
Indexes speed performance when searching and joining tables.
An index provides a way for a database management system to locate information more quickly
. When it uses an index,
the database management system can go directly to a specific row
rather than having to search through all the rows until it find it.
automatically creates an index for a primary key
WHEN TO CREATE AN INDEX
When the column is a foreign key
When the column is used frequently in search conditions or joins
When the column contains a large number of distinct values
When the column is updated infrequently
How Normalization Works
: are used to eliminate problems caused by
There are 7 types.
normalized data structure
: where each table contains info. about a single entity, and each piece of info. is stored in exactly on place.
unnormalized data structure
: a table that contains information about two or more entities, repeating columns, columns that contain repeating values, and data that's repeated in two or more rows.
: redundancy that can cause storage and maintenance problems.
: a formal process used to separate the data in a data structure into related tables.
To normalize a data structure, you
apply the normal forms in sequence
. Although there are a total of seven normal forms,
a data structure is typically considered normalized if the first three normal forms are applied
normalized data structure
each table contains information about single entity
each piece of information is stored in exactly on place
unnormalized data structure
a table can contain information about two or more entities
. It can also contain
, columns that contain repeating values, and
data that's repeated
in two or more rows.
a formal process you can use to separate the data in a data structure into related tables
, which can cause storage and maintenance problems.
How To Enforce The Relationships Between Tables
: rows in the foreign key table that no longer have a related row in the primary key table.
foreign key constraint
: indicates how the referential integrity between the tables is enforced
declarative referential integrity (DRI)
: is used to enforce relationships between tables and maintain their referential integrity.
: the relationship between tables are maintained correctly.
If referential integrity isn't enforced and
a row is deleted from the primary key table that has related rows in the foreign key table
, the rows in the foreign key table are
said to be
When you define foreign key constraints, you can
specify how referential integrity is enforced when a row is deleted from the primary key table
. The options are
a). to return an error,
b). to delete the related rows in the foreign key table
c), to set the foreign key values in the related rows to null
declarative referential integrity (DRI)
foreign key constraints
In Oracle, you can
enforce referential integrity by using declarative referential integrity
by defining triggers
the relationships between tables are maintained correctly.
--That means that
a table with a foreign key doesn't have rows with foreign key values that don't have matching primary key values in the related table
Operations that can violate referential integrity
Update the value of a foreign key
The new foreign key value doesn't have a matching primary key value in the related table
Update the value of a primary key
The foreign key table contains one or more rows related to the row that's changed
Insert a row in the foreign key table
The foreign key value doesn't have a matching primary key value in the related table
Delete a row form the primary key table.
The foreign key table contains one or more rows related to the deleted row
How To Identify The Primary And Foreign Keys
: a table that links two many-to-many tables to relate them.
If two tables have
should be related by their primary keys
. This type of relationship is
typically used to improve performance
columns with large amounts of data can be stored in a separate table
If two tables have a many-to-many relationship, you'll need to
to relate them
each of the tables in the many-to-many relationship will have a one-to-many relationship with the linking table
. The linking table doesn't usually have a primary key.
If two tables have a one-to-many relationship, you may need to
add a foreign key column to the table on the "many" side
foreign key column must have the same data types as the primary key column
it's related to.
If a suitable column doesn't exist for a primary key, you can
create an ID column that's incremented by one for each new row as the primary key.
values of the primary key should seldom
, if ever,
. The values also
be short and easy to enter correctly
Each table should have a primary key
that uniquely identifies each row.
, you should
set an existing column for the primary key
How To Identify The Tables And Assign Columns
As you assign the elements to entities, you should
omit elements that aren't needed
, and you should
add any additional elements that ARE needed
If a data element relates to more than one entity, you can include it under all of the entities it relates to. Then,
when you normalize the database, you may be able to remove the duplicate elements
After you subdivide
all of the data elements
for a database,
you should group them by the entities with which they're associated
. These entities will later become the tables of the DB, and the elements will become the columns.
How To Subdivide The Data Elements
When you subdivide a data element, you
can easily rebuild it when necessary by concatenating the individual components
The extent to which you subdivide a data element depends on how it will be used. Because it's difficult to predict all future uses for the data,
most designers subdivide data elements as much as possible
If a data element contains two or more components, you should consider
subdividing the element each time you use it
How To Identify The Data Elements
As you identify the data elements of a system, you should begin thinking about the
that those elements are associated with. That will help you identify the tables of the DB later on.
An invoice document can be used to help you identify the data elements in a real-world system.
You can identify data elements in a variety of ways, including
existing systems, and
: a row the stores one occurrence of the entity.
: a column in a table that stores one item of information for the entity
: a table represented by one object.
To model a database and the relationships between its tables after a real-world system, you can use a technique called
entity-relationship (ER) modeling
In a relational database, a table typically represents an object, or
in the real world. Each column of a table is used to store an
associated with the entity, and each row represents on ** of the entity
A Relational database system should model the real-world environment where it's used. It is the designers job to analyze the real-world system and them map it onto a relational database system.
: a model of the database rather than the database itself
The 6 basic steps for designing a data structure
: Identify the indexes
: Review whether the data structure is normalized
: Identify the primary and foreign keys.
: Identify the tables and assign solumns
: Subdivide each element into its smallest useful components.
: Identify the data elements
A database system is modeled after a real_world system
Notes taken while studying "Murach's Oracle SQL And PL/SQL For Developers"