Delphi - Database Design Primer

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Surviving Client/Server:

Database Design Primer


by Steve Troxell

D esigning tables for a client/


server database can present
new challenges to traditional file
possible is whether your system
will be an open or closed architec-
ture. Nearly all client/server RDBM
you’ll still need to consider some
issues of user permissions to ta-
bles and other objects, your users
server oriented developers. There systems are designed to allow an have no need to know what the
are many more features and issues open architecture, but that doesn’t tables are, their layout, what the
in the database server world that mean you must have one. What I fields are, the relationships be-
the developer should to be aware mean by open architecture is the tween tables, etc. But for a mere
of. Nearly any reference book will database can be accessed not only technicality, this is still essentially
tell you what the additional fea- by the proprietary applications a closed system. But if your users
tures are, but you also need to now you develop, but also by off the will be designing their own ad hoc
how to employ them effectively. shelf third party productivity tools reports or queries, then you’ll need
I get some of my best pearls of such as spreadsheets, data extrac- to consider an open architecture.
wisdom from pop culture. About tors, word processors and report The real value of an open archi-
ten years ago I saw a comedy skit generators. This is generally tecture is allowing at least some
that pretty much summed up my achieved by using a database cross section of the user base di-
opinion of a lot of people in soft- driver such as ODBC to connect the rect access to at least some of the
ware development. The skit in- application to the database server. data beyond a prefabricated frame-
volved a quasi-prehistoric tribe of Third-party applications can be work. Generally, this access is pro-
people out on a hunt, trying to for- written to conform to the standard vided in the form of ad hoc
mulate a plan for felling some ani- ODBC interface and the ODBC querying for data analysis and re-
mal, a buffalo let’s say. The leader driver in turn is written to access a porting. This is a very valuable
suggested a head-on assault and particular database server’s API. quality for a database to have as it
commented that 3 or 4 of their In this way the same application opens up selected bits of data to
party would probably be killed, but can access different vendor’s data- more users so they can get what
they would have food for the tribe bases by substituting an ODBC they need and how they need it in
for a whole month. A younger mem- driver appropriate for that a timely manner. This means you
ber of the hunting party came up database. may make different database de-
with an idea. “What if we all spread Many file server databases, such sign choices than you would if the
out and waved our arms and as Paradox or dBase, provide for design was the sole domain of
shouted and drove the animal over open architecture as well, so this software developers.
that cliff over there? Then we can isn’t the exclusive domain of cli-
simply walk down to the base of the ent/server. However, most devel- User Security
cliff and drag the carcass back to opers are accustomed to closed Open architectures are the princi-
camp.” The leader of the hunting architectures where they are not pal reason why all client/server da-
party thought for a while and said, concerned about anything but tabases provide user login
“A very interesting plan. But your strict proprietary application ac- accounts. A typical closed data-
idea is new and we fear new things. cess to the database. It’s important base system might not be con-
Therefore it must be rejected.” that developers and customers cerned with exactly who was doing
People entering client/server both understand the role of third what in the application. Or if it was,
development are faced with a vari- party software in the system as it might employ a simple Users ta-
ety of new capabilities as well. It early as possible, because many ble within itself. Some true cli-
surprises me to find that some de- aspects of the database design can ent/server applications that are
velopers still shy away from such be an aid or hindrance to an open unconcerned with user account-
things apparently simply because architecture. ability and external access to data
they are unfamiliar with them. The If the extent of your system’s simply create a single fixed ac-
following is meant to shed some need to provide external access to count in the database which is
light on some of the issues facing a third-party programs is simply used by all applications to gain ac-
client/server developer. allowing your users to run prefab- cess to the data. The account user-
ricated reports through a commer- name and password are encoded
Open Architecture cial report writer, then you really directly into the applications and
The most important issue you don’t have much of an open archi- there is no login dialog for the user
must come to grips with as early as tecture to worry about. While to complete.

June 1997 The Delphi Magazine 41


However, user accounts in on hold? Many database develop- Declarative Constraints
client/server databases are not ers don’t think twice about using a If your open architecture allows
primarily concerned with tracking numeric sequence to encode val- data modifications from third-
who’s connected and who’s not. ues for a column. But the values party programs, then you’ll defi-
Their chief purpose is in control- carry no meaning in and of them- nitely want to consider using some
ling who has access to which parts selves. You are forced to make a form of automatic processing to
of the database. Remember, third translation from a completely ab- perform data validation and ensure
party tools connect to the data- stract value by referring to a data data integrity. With client/server
base independently, and the whole encoding sheet or joining to a databases, you have two choices:
database is laid bare for all to see. lookup table. declarative constraints or triggers.
Would you want marketing people, Now look at Figure 2. The same Declarative constraints are defined
whilst creating ad hoc reports on data has been encoded differently. at the time you create the table.
product sales, to see the payroll Sure, you can say a mental transla- They include nullability, defaults,
data for the whole company? On tion of the code value OP to the term uniqueness and check constraints.
the other hand, you do want the “open order” is still required, but Listing 3 shows a few examples.
accounting personnel to be able to the value is now a mnemonic rather The exact syntax varies from ven-
get to the payroll data? When all than an abstract value. It carries dor to vendor so be sure to check
access is running through proprie- meaning by itself. your manual before trying this with
tary applications, you can easily To get the same level of meaning your database server.
control who gets to what. But with in a result set from the abstract Any field may contain a null
an open architecture, you have to data shown in Figure 1 would value. A null indicates the actual
rely on the built-in security avail- require a join to a lookup table. value is not available or not known.
able via the user accounts in the Several columns employing ab- For example, if an employee does
RDBMS you’re using. stract encoding could lead to not wish to divulge their birthday,
This means granting or revoking several joined tables and you can we may store a null in the DateOf-
permissions to individual users to see how quickly ad hoc querying Birth field. Nulls are available to all
read, insert, update or delete data can become complicated and datatypes and save the developer
(or any combination) for a given inefficient. from having to contrive a special
table, as shown in Listing 1. So, no
permissions would be granted on
➤ Listing 1
payroll tables to any user that
wasn’t in the accounting depart- /* Give John read-only access to the employees table */
ment (see Listing 2). Many RDBM GRANT SELECT ON Employees TO JohnA
systems let you create groups of
users in just this manner to make it
easier to manage user permissions.
➤ Listing 2

Actionable Information /*Deny all access to the payroll table */


Database design involves striking a REVOKE ALL ON Payroll FROM Public
balance between performance, GRANT ALL ON Payroll TO Accounting

functionality, and size. With an


open architecture there is also the
need to optimize the amount of “ac-
➤ Figure 1
tionable information” within the
data. This means data that carries OrderNum CustNo Date Status Clerk Total
meaning in and of itself, minimizing
871182 113 6/2/97 0 78 $507.92
the requirement to decode, trans-
late or interpret the data. Software 871183 291 6/2/97 0 78 $122.45
developers are accustomed to de- 871184 88 6/2/97 1 54 $1,209.00
coding, translating and interpret-
ing data and design their databases 871185 195 6/2/97 3 99 $45.22
accordingly, to achieve compact-
ness and because there’s been no
➤ Figure 2
compelling reason not to. With an
open architecture you have to keep OrderNum CustNo Date Status Clerk Total
in mind that users other than soft-
871182 113 6/2/97 OP 78 $507.92
ware developers will be working
with the data. 871183 291 6/2/97 OP 78 $122.45
Take a look at the data shown in 871184 88 6/2/97 HD 54 $1,209.00
Figure 1. Which rows are for open
orders? Which rows are for orders 871185 195 6/2/97 VD 99 $45.22

42 The Delphi Magazine Issue 22


code value to serve the same pur- allow any number of rows to have name of the constraint being vio-
pose. However, nulls introduce a null value in the unique column, lated should be present in the error
some complexities to query logic. following the rule that no null value message text. In this way, client
You can disallow nulls in any col- can be equal to any other null value apps can respond to particular er-
umn by using the NOT NULL con- (strict interpretation of null). rors, parse the error text for con-
straint. We will explore nulls more In the case of our badge number straint names, and provide a
closely later in the article. column, our business rule is that cleaner, more meaningful error
When a column value is omitted not every employee is required to message to the user.
from an SQL INSERT statement, then have a badge number, but those
the values for that column in the who do cannot have duplicate Triggers
rows being inserted will be null by badge numbers. The only way we Declarative constraints only go so
default unless you supply your own can enforce this with declarative far. For example, how do we en-
using the DEFAULT constraint. If constraints is to define the column force the rule that employees in
you’ve declared a column NOT NULL, unique and allow any number of departments 012, 014 and 155 re-
then you should supply a default null values within the unique col- quire badges? For more powerful
value unless there is no logical de- umn. If we chose to use a special automatic data processing, you
fault (like the DeptNo field in the non-null code value for employees have to turn to triggers. Triggers
example). Defaults can be literal without badges, we could not use contain static SQL code that fires
values or functions built into the the unique constraint. whenever a data modification op-
database. The HireDate field in this Check constraints are the most eration is performed, regardless of
example uses the hypothetical flexible declarative constraints. the application that initiated the
built in function CurrentDate to Check constraints allow you to de- operation. Within a trigger you can
post today’s date as a default value. fine an expression that must evalu- generally use more complex logic
Some columns within a table re- ate to true. In Listing 3 we’ve used than would be available in a decla-
quire non duplicating values, like check constraints to ensure that rative constraint. Separate triggers
employee badge numbers. We can the Salary field is a positive num- can be placed specifically for in-
enforce this rule by using the ber and that the BadgeNo field is a sert, update or delete operations
UNIQUE constraint, which simply null or a string of one uppercase on a given table. Some databases
places a unique index on the col- letter followed by two digits. allow many triggers to exist for the
umn. Databases vary in how they Nearly all declarative con- same operation on the table, each
handle null values on unique col- straints can be given a name, like firing in sequence.
umns. Some require that there be our check constraint for the Salary Listing 4 shows a trigger we
no more than one row with a null field. When a constraint is violated might use to enforce the BadgeNo
value in the column (strict inter- by an insert or update operation, rule. Again, exact syntax varies
pretation of uniqueness). Others the server raises an error and the from database to database. Trig-
gers provide a mechanism for ex-
amining the values of the row being
➤ Listing 3
affected. In this case, the New vari-
able gives us access to the values
CREATE TABLE Employees(
EmpNo integer NOT NULL, being written to the table.
FirstName varchar(15) NOT NULL DEFAULT ’’, A common use of triggers is auto-
LastName varchar(20) NOT NULL DEFAULT ’’,
DeptNo char(3) NOT NULL,
matic stamping of username, date
PhoneExt char(3) NOT NULL DEFAULT ’’, or time information in sensitive ta-
HireDate date NOT NULL DEFAULT CurrentDate, bles. Also, cascading deletes can
DateOfBirth date NULL,
SSN char(11) NOT NULL DEFAULT ’<unknown>’, be implemented with triggers,
Salary double NOT NULL DEFAULT 0 where deleting a record in a master
CONSTRAINT SalaryPositive
CHECK (Salary >= 0), table automatically deletes associ-
BadgeNo char(3) NULL UNIQUE ated records in one or more detail
CHECK (BadgeNo IS NULL or
BadgeNo LIKE ’[A-Z][0-9][0-9]’)
tables (see Listing 5).
); You’ll want to be judicious in
your use of triggers and con-
straints since there is a bit of over-
head in their execution, and they
➤ Listing 4
will always execute for every data
CREATE TRIGGER BadgeRule ON Employees
modification they are associated
FOR INSERT, UPDATE with.
AS
BEGIN
IF (New.DeptNo IN (’012’, ’014’, ’155’)) AND Domains
(New.BadgeNo IS NULL) THEN Domains are essentially user-
EXCEPTION BadgeNoReqd;
END defined datatypes in SQL. Domains
are very helpful for ensuring the

June 1997 The Delphi Magazine 43


consistency of data declarations atomic value. That is, a single value used together, which is nearly al-
across tables in a non-trivial data- cannot be further broken down ways the case. You should only be
base. For example, people’s names into useful values. For example, for concerned about this if your sys-
might appear in several different a table of mailing addresses, it is tem has some functional need to
tables: customer contacts, sales- not uncommon for designers to isolate the area code, for example
people, employees, vendor con- group the elements city, state and a requirement to produce a list of
tacts, etc. If a design decision has postal code into a single field of the contacts sorted by area code.
been made that all name fields al- form <city>, <state> <postal This reasoning also applies to
low up to 30 characters, then it is code> since that is how they need people’s names. Many designers
much simpler to define a TPerson- to displayed when printed on a create a single field for a name,
Name domain as VARCHAR(30) and mailing label. However, this single such as Steve Troxell. If there is
make all your table column defini- value can be broken down into absolutely no requirement to oper-
tions with the domain name rather three useful elements and should ate on the individual elements of
than the direct data type as shown be stored in three separate col- the name, this may be fine. For
in Listing 6. umns. The values in the three example, a contact name for a
Domains can be valuable for columns can easily be combined to customer or vendor record.
keeping consistent definitions and produce the desired format for the However, there usually is much
making it much easier to change mailing label. value in splitting name fields into
definitions across the entire data- Having the values separated out first name and last name compo-
base. A frequently overlooked permits you to manipulate the data nents that may not be immediately
value of domains is their use in at a finer level. For example, most obvious. Is it important to you that
stored procedure parameter bulk mailings can gain a postage the appearance of the name be con-
datatypes. While there may be only discount if the addresses are sistent? If you have a single name
a few fields in table definitions sorted by postal code. How are you field how are you going to prevent
sharing the same domain, there going to sort the records when all problems like user A always enters
may be many different stored pro- three elements are pushed to- Steve Troxell but user B always
cedures with parameters of that gether in a single field. Also you enters Troxell, Steve. If you sort
same data type. Once again, ensur- may want to stagger mailings to by the name are you making an
ing the consistency of the data type different states. You will need to assumption that all data entry will
definitions within stored proce- select all addresses for state A for be lastname, firstname? If there is
dure parameters and their associ- the first mailing and all addresses the possibility that these issues
ated table columns is much easier for state B for the second mailing. may arise in your system, it’s bet-
with a domain name. It would be extremely difficult to ter to split the name field apart; you
filter records on state if it’s not can always assemble the complete
Normalization broken out into its own field. name in any form you need.
Normalization generally refers to You shouldn’t get carried away
eliminating redundancies in tables with atomizing values though. For First Normal Form:
by splitting them apart into smaller example, given a phone number No Repeating Groups
tables with a relationship defined with area code, you could argue An extension of the requirement of
between them. Typically several that the area code must be sepa- atomic values is that there be no
narrow (fewer columns) tables are rated from the actual phone num- repeating groups of information.
better than one wide (lots of col- ber to be in first normal form. That is, a single column cannot be
umns) table. The narrow tables Generally it is not necessary to go an array, list, or multi-field struc-
usually result in more compact to this extreme if the phone num- ture. Most RDBM systems enforce
data because omissions and vari- ber and area code will always be this by simply not providing
able quantities can be handled
more efficiently. Also, a narrow ta-
➤ Listing 5
ble means more rows are packed
into a data page and the server can CREATE TRIGGER MasterCascade ON MasterTable
scan more rows per I/O operation, FOR DELETE
AS BEGIN
improving performance. There are DELETE FROM DetailTable WHERE KeyNo = Old.KeyNo;
five main rules of normalization. In END
practice the first three are usually
sufficient for most systems.
➤ Listing 6
First Normal Form:
Atomic Values CREATE DOMAIN TPersonName AS varchar(30);
The point where a column and row CREATE TABLE Customers(
CustNo integer;
intersect in a table we’ll call a cell. Company varchar(30);
First normal form requires that Contact TPersonName);
each cell in a table contain an

44 The Delphi Magazine Issue 22


non-scalar datatypes. However, de- three authors for any given book. table contains as many rows for a
velopers still try to buck the sys- So do we simply refuse to accept a book as there are authors for that
tem by designing tables like that book with four or more authors? book. You can see how it would be
shown in Figure 3 . Most likely, the book will get en- much simpler to search for all oc-
This is in first normal form by tered but only the first three currences of a single author in the
technicality only. Each field is an authors will be credited. Second, if BookAuthors table and then link in
atomic value. But the layout still I want to see a list of books by any the book title from Books. Tables
breaks the spirit of first normal given author, I must query all three such as this go by one of several
form and should be redesigned. AuthorId fields to guarantee cover- names: relationship, association or
The AuthorID fields presumably age of all possible positions of the junction tables. They describe a
link to a separate Authors table con- value I’m looking for. This can lead relationship between two other
taining each author’s name and to some hideous code workar- tables and don’t necessarily con-
other info. This approach intro- ounds to effectively implement a tain any useful independent data
duces several problems. First, it filter on author. themselves.
imposes an artificial business rule A solution to these problems is
that there can be no more than shown in Figure 4. The BookAuthors Second Normal Form
Second normal form means that if
a table’s primary key is composed
➤ Figure 3: Books table
of more than one column then no
Title ISBN PublisherID AuthorID1 AuthorID2 AuthorID3 column in the table should be de-
Delphi 1-57169-019-0 SAMS 32 45 18 pendent on just part of the primary
How-To key. This is easier to see with an
Delphi 0-672-30499-6 WAITE 16 <null> <null>
example. Figure 5 shows a project
Unleased table assigning employees to pro-
jects. The primary key for the table
is both the project identifier and
the employee number. As you can
➤ Figure 4
see the employee’s name is only
Books dependent on part of the key for
Title ISBN PublisherID the row (employee number) and
should be eliminated from this ta-
Delphi How-To 1-57169-019-0 SAMS
ble. The BillRate column is the
Delphi Unleased 0-672-30499-6 WAITE hourly rate that employee charges
for this project. An employee work-
BookAuthors ing on multiple projects might
ISBN AuthorID
charge different rates for each pro-
ject. Therefore, the BillRate col-
0-672-30499-6 16 umn is dependent on the entire
1-57169-019-0 18 primary key and is appropriate for
1-57169-019-0 32 this table.
1-57169-019-0 45
The problem with a table that
fails to meet second normal form is
that if we change a single fact about
the database (in this case, an
➤ Figure 5
employee’s name), then we must
ProjectID EmpID EmpName BillRate change that fact in several different
X1104 102 Bob Smith 65.00 places (in this case, every
occurrence in the ProjEmp table).
X1104 76 Stan Stevens 35.00
X1256 102 Bob Smith 90.00 Third Normal Form
X1256 88 Roy Rogers 75.00 Third normal form extends this de-
J3880 88 Roy Rogers 75.00 pendency logic by stating that all
non-key columns must depend on
the primary key and not depend
solely on another non-key column.
➤ Figure 6
In Figure 6 it is sufficient to stop
EmpNo EmpName DeptNo DeptName DeptManager with the DeptNo column as DeptName
112 Sue Bennet 80 Shipping Bob Smith and DeptManager are not directly
relevant to the employee, but in-
115 Roy Robinson 14 Payroll Carly Johnson
stead should be broken out into a
121 John James 80 Shipping Bob Smith separate table keyed by DeptNo.

June 1997 The Delphi Magazine 45


There may be occasions where OrderNo Subtotal Tax Freight Total
you deliberately break third nor-
80221 114.50 6.87 10.00 131.37
mal form. Consider the table
shown in Figure 7. You might notice 80222 505.75 30.35 25.00 561.10
that the Total column is redundant,
being a computation of the Subto-
➤ Figure 7
tal, Tax and Freight columns. How- Nulls create confusion when us-
ever, if you have an open ers fail to account for them. For
architecture and users may be example, a user might select all em-
querying records based on the or- ployees with a salary greater than you’d have to contrive an unlikely
der total, it is easier and more reli- 50,000 and find 10 qualifying rows. though legitimate date, like
able to provide the total as a He might then run a query of all 1/1/2100, to represent no date.
pre-computed value. Or, whether employees with salary less than or Although most problems with
you have an open architecture or equal to 50,000 and find 17 qualify- nulls are the result of users or de-
not, if there is frequent need to ing rows. He might come to the velopers failing to account for
filter orders by total amount (all conclusion that there were 27 total them, they are best avoided unless
orders over $1000, for example), employees in the company. This serving some purpose. You should
providing an explicit column for would be incorrect if there were define all column tables as NOT NULL
the value allows you to index it for some employees with null salaries and only allow nulls in specific
faster retrieval. If you elect to do (hourly wage employees perhaps). cases that improve your design.
this, you may want to consider How nulls sort varies from ven-
having triggers on the table to dor to vendor, but they sort out Conclusion
calculate the Total column. either before all other values or Client/server systems offer more
after all other values. This implies facilities to have the database it-
Null Values that nulls are either less than or self, rather than client applica-
There is perhaps no greater con- greater than all other values and tions, aid in managing data.
troversy in relational databases may be misleading to users. Developers new to client/server
than the use of null values. E F A null present in any term of a may be unaware or unfamiliar with
Codd, widely regarded as the mathematical expression results in these features and hopefully this
“father” of relational databases, de- the entire expression evaluating to brief introduction entices you to
creed that a proper relational data- null. If any part of the expression is explore further. Like many things,
base system have something like unknown, how can any legitimate features should not be embraced
null to represent missing or inap- result be obtained? However, with or shunned by rote rules alone. It’s
plicable data. In fact, Codd wanted a special value like zero instead of much better to understand what
two values: one for incomplete data null, you’d get a valid result but it each can do and what possible
to be filled in later and one for inap- would be wholly inaccurate. Aggre- problems may arise in its use. Then
plicable data that would never be gate functions in SQL like AVG and select the appropriate tool for the
filled in. On the other hand, C J SUM skip null values for their com- problem at hand.
Date, a leading expert on relational putations and produce a result that
databases, insists that nulls are includes all the non-null values. In
evil and should be done away with. this respect, nulls might be helpful Steve Troxell is a Senior Software
Nulls do create problems, but because you can obtain values for Engineer with TurboPower
like most things, they serve a pur- the data that is known. If you had Software. He can be reached by
pose. Nulls shouldn’t be shunned used special codes instead of nulls, email at [email protected]
outright “because they are new and AVG and SUM would be useless. or on CompuServe at 74071,2207
we fear new things.” Nulls can Going back to our example in
cause more problems than they Listing 3, how would you represent
solve, so become familiar with a missing birthday in a date field?
their uses and pitfalls and use them The system would most likely re-
appropriately. ject an invalid date like 99/99/99, so

46 The Delphi Magazine Issue 22

You might also like