Chapter 2

SQL Fundamentals

IN THIS CHAPTER

Bullet Understanding SQL

Bullet Clearing up SQL misconceptions

Bullet Taking a look at the different SQL standards

Bullet Getting familiar with standard SQL commands and reserved words

Bullet Representing numbers, characters, dates, times, and other data types

Bullet Exploring null values and constraints

Bullet Putting SQL to work in a client/server system

Bullet Considering SQL on a network

SQL is a flexible language that you can use in a variety of ways. It’s the most widely used tool for communicating with a relational database. In this chapter, I explain what SQL is and isn’t — specifically, what distinguishes SQL from other types of computer languages. Then I introduce the commands and data types that standard SQL supports and I explain two key concepts: null values and constraints. Finally, I give an overview of how SQL fits into the client/server environment, as well as the Internet and organizational intranets.

What SQL Is and Isn’t

The first thing to understand about SQL is that SQL isn’t a procedural language, as are Python, C, C++, C#, and Java. To solve a problem in a procedural language, you write a procedure — a sequence of commands that performs one specific operation after another until the task is complete. The procedure may be a straightforward linear sequence or may loop back on itself, but in either case, the programmer specifies the order of execution.

SQL, on the other hand, is nonprocedural. To solve a problem using SQL, simply tell SQL what you want (as if you were talking to Aladdin’s genie) instead of telling the system how to get you what you want. The database management system (DBMS) decides the best way to get you what you request.

All right. I just told you that SQL is not a procedural language — and that’s essentially true. However, millions of programmers out there (and you’re probably one of them) are accustomed to solving problems in a procedural manner. So, in recent years, there has been a lot of pressure to add some procedural functionality to SQL — and SQL now incorporates features of a procedural language: BEGIN blocks, IF statements, functions, and (yes) procedures. With these facilities added, you can store programs at the server, where multiple clients can use your programs repeatedly.

To illustrate what I mean by “tell the system what you want,” suppose you have an EMPLOYEE table from which you want to retrieve the rows that correspond to all your senior people. You want to define a senior person as anyone older than age 40 or anyone earning more than $100,000 per year. You can make the desired retrieval by using the following query:

SELECT * FROM EMPLOYEE WHERE Age > 40 OR Salary > 100000 ;

This statement retrieves all rows from the EMPLOYEE table where either the value in the Age column is greater than 40 or the value in the Salary column is greater than 100,000. In SQL, you don't have to specify how the information is retrieved. The database engine examines the database and decides for itself how to fulfill your request. You need only specify what data you want to retrieve.

Remember A query is a question you ask the database. If any of the data in the database satisfies the conditions of your query, SQL retrieves that data.

Current SQL implementations lack many of the basic programming constructs that are fundamental to most other languages. Real-world applications usually require at least some of these programming constructs, which is why SQL is actually a data sublanguage. Even with the extensions that were added in 1999, 2003, 2005, 2008, and 2011, you still have to use SQL in combination with a procedural language (such as C++) to create a complete application.

You can extract information from a database in one of two ways:

  • Make an ad hoc query from your keyboard by just typing an SQL statement and reading the results from the screen. Queries from the keyboard are appropriate when you want a quick answer to a specific question. To meet an immediate need, you may require information that you never needed before from a database. You’re likely never to need that information again, either, but you need it now. Enter the appropriate SQL query statement from the keyboard, and in due time, the result appears on your screen.
  • Execute a program that collects information from the database and then reports on the information either onscreen or in a printed report. Incorporating an SQL query directly into a program is a good way to run a complex query that you’re likely to run again in the future. That way, you can formulate a query just once for use as often as you want. Chapter 16 explains how to incorporate SQL code into programs written in another programming language.

A (Very) Little History

SQL originated in one of IBM’s research laboratories, as did relational database theory. In the early 1970s, as IBM researchers developed early relational DBMS (or RDBMS) systems, they created a data sublanguage to operate on these systems. They named the pre-release version of this sublanguage SEQUEL (Structured English QUEry Language). However, when it came time to formally release their query language as a product, they found that another company had already trademarked the product name “Sequel.” Therefore, the marketing geniuses at IBM decided to give the released product a name that was different from SEQUEL but still recognizable as a member of the same family. So they named it SQL, pronounced ess-que-ell. Although the official pronunciation is ess-que-ell, people had become accustomed to pronouncing it “Sequel” in the early pre-release days and continued to do so. That practice has persisted to the present day; some people will say “Sequel” and others will say “S-Q-L,” but they are both talking about the same thing.

Technicalstuff The syntax of SQL is a form of structured English, which is where its original name came from. However, SQL is not a structured language in the sense that computer scientists understand that term. Thus, despite the assumptions of many people, SQL is not an acronym standing for “structured query language.” It is a sequence of three letters that don’t stand for anything, just like the name of the C language does not stand for anything.

IBM’s work with relational databases and SQL was well known in the industry even before IBM introduced its SQL/DS relational database (RDBMS) product in 1981. By that time, Relational Software, Inc. (now Oracle Corporation) had already released its first RDBMS. These early products immediately set the standard for a new class of database management systems. They incorporated SQL, which became the de facto standard for data sublanguages. Vendors of other relational database management systems came out with their own versions of SQL. Typically, these other implementations contained all the core functionality of the IBM products, extended in ways that took advantage of the particular strengths of their own RDBMS product. As a result, although nearly all vendors used some form of SQL, compatibility between platforms was poor.

Remember An implementation is a specific RDBMS running on a specific hardware platform.

Soon a movement began, to create a universally recognized SQL standard to which everyone could adhere. In 1986, ANSI (the American National Standards Institute) released a formal standard it named SQL-86. ANSI updated that standard in 1989 to SQL-89 and again in 1992 to SQL-92. As DBMS vendors proceed through new releases of their products, they try to bring their implementations ever closer to this standard. This effort has brought the goal of true SQL portability much closer to reality.

Remember The most recent full version of the SQL standard is SQL:2016 (ISO/IEC 9075-X:2016). In this book, I describe SQL as SQL:2016 defines the language. Every specific SQL implementation differs from the standard to a certain extent. Because the complete SQL standard is comprehensive, currently available implementations are unlikely to support it fully. However, DBMS vendors are working to support a core subset of the standard SQL language. The full ISO/IEC standard is available for purchase at www.iso.org/search.html?q=iso%209075, but you probably don’t want to buy it unless you intend to create your own ISO/IEC SQL standard database management system. The standard is highly technical and virtually incomprehensible to anyone other than a computer language scholar.

SQL Statements

The SQL command language consists of a limited number of statements that perform three functions of data handling: Some of them define data, some manipulate data, and others control data. I cover the data-definition statements and data-manipulation statements in Chapters 4 through 12; I detail the data-control statements in Chapter 14.

To comply with SQL:2016, an implementation must include a basic set of core features. It may also include extensions to the core set (which the SQL:2016 specification also describes). Table 2-1 lists the core plus the extended SQL:2016 statements. It’s quite a list. If you’re among those programmers who love to try out new capabilities, rejoice.

TABLE 2-1 SQL:2016 Statements

ADD

DEALLOCATE PREPARE

FREE LOCATOR

ALLOCATE CURSOR

DECLARE

GET DESCRIPTOR

ALLOCATE DESCRIPTOR

DECLARE LOCAL TEMPORARY TABLE

GET DIAGNOSTICS

ALTER DOMAIN

DELETE

GRANT PRIVILEGE

ALTER ROUTINE

DESCRIBE INPUT

GRANT ROLE

ALTER SEQUENCE GENERATOR

DESCRIBE OUTPUT

HOLD LOCATOR

ALTER TABLE

DISCONNECT

INSERT

ALTER TRANSFORM

DROP

MERGE

ALTER TYPE

DROP ASSERTION

OPEN

CALL

DROP ATTRIBUTE

PREPARE

CLOSE

DROP CAST

RELEASE SAVEPOINT

COMMIT

DROP CHARACTER SET

RETURN

CONNECT

DROP COLLATION

REVOKE

CREATE

DROP COLUMN

ROLLBACK

CREATE ASSERTION

DROP CONSTRAINT

SAVEPOINT

CREATE CAST

DROP DEFAULT

SELECT

CREATE CHARACTER SET

DROP DOMAIN

SET CATALOG

CREATE COLLATION

DROP METHOD

SET CONNECTION

CREATE DOMAIN

DROP ORDERING

SET CONSTRAINTS

CREATE FUNCTION

DROP ROLE

SET DESCRIPTOR

CREATE METHOD

DROP ROUTINE

SET NAMES

CREATE ORDERING

DROP SCHEMA

SET PATH

CREATE PROCEDURE

DROP SCOPE

SET ROLE

CREATE ROLE

DROP SEQUENCE

SET SCHEMA

CREATE SCHEMA

DROP TABLE

SET SESSION AUTHORIZATION

CREATE SEQUENCE

DROP TRANSFORM

SET SESSION CHARACTERISTICS

CREATE TABLE

DROP TRANSLATION

SET SESSION COLLATION

CREATE TRANSFORM

DROP TRIGGER

SET TIME ZONE

CREATE TRANSLATION

DROP TYPE

SET TRANSACTION

CREATE TRIGGER

DROP VIEW

SET TRANSFORM GROUP

CREATE TYPE

EXECUTE IMMEDIATE

START TRANSACTION

CREATE VIEW

FETCH

UPDATE

DEALLOCATE DESCRIPTOR

Reserved Words

In addition to the statements, a number of other words have a special significance within SQL. These words, along with the statements, are reserved for specific uses, so you can't use them as variable names or in any other way that differs from their intended use. You can easily see why tables, columns, and variables should not be given names that appear on the reserved word list. Imagine the confusion that a statement such as the following would cause:

SELECT SELECT FROM SELECT WHERE SELECT = WHERE ;

’Nuff said. A complete list of SQL reserved words appears in Appendix A.

Data Types

Depending on their histories, different SQL implementations support a variety of data types. The SQL specification recognizes seven predefined general types:

  • Numerics
  • Binary
  • Strings
  • Booleans
  • Datetimes
  • Intervals
  • XML

Within each of these general types may be several subtypes (exact numerics, approximate numerics, character strings, bit strings, large object strings). In addition to the built-in, predefined types, SQL supports collection types, constructed types, and user-defined types, all of which I discuss later in this chapter.

Tip If you use an SQL implementation that supports data types that aren’t described in the SQL specification, you can keep your database more portable by avoiding these undescribed data types. Before you decide to create and use a user-defined data type, make sure that any DBMS you may want to port to in the future also supports user-defined types.

Exact numerics

As you can probably guess from the name, the exact numeric data types enable you to express the value of a number exactly. Five data types fall into this category:

  • INTEGER
  • SMALLINT
  • BIGINT
  • NUMERIC
  • DECIMAL
  • DECFLOAT

INTEGER data type

Data of the INTEGER type has no fractional part, and its precision depends on the specific SQL implementation. As the database developer, you can't specify the precision.

Remember The precision of a number is the maximum number of significant digits the number can have.

SMALLINT data type

The SMALLINT data type is also for integers, but the precision of a SMALLINT in a specific implementation can't be any larger than the precision of an INTEGER on the same implementation. In many implementations, SMALLINT and INTEGER are the same.

If you're defining a database table column to hold integer data and you know that the range of values in the column won’t exceed the precision of SMALLINT data on your implementation, assign the column the SMALLINT type rather than the INTEGER type. This assignment may enable your DBMS to conserve storage space.

BIGINT data type

The BIGINT data type is defined as a type whose precision is at least as great as that of the INTEGER type (it may be greater). The exact precision of a BIGINT data type depends on the SQL implementation used.

NUMERIC data type

NUMERIC data can have a fractional component in addition to its integer component. You can specify both the precision and the scale of NUMERIC data. (Precision, remember, is the maximum number of significant digits possible.)

Remember The scale of a number is the number of digits in its fractional part. The scale of a number can't be negative or larger than that number’s precision.

If you specify the NUMERIC data type, your SQL implementation gives you exactly the precision and scale that you request. You may specify NUMERIC and get a default precision and scale, or NUMERIC (p) and get your specified precision and the default scale, or NUMERIC (p,s) and get both your specified precision and your specified scale. The parameters p and s are placeholders that would be replaced by actual values in a data declaration.

Say, for example, that the NUMERIC data type's default precision for your SQL implementation is 12 and the default scale is 6. If you specify a database column as having a NUMERIC data type, the column can hold numbers up to 999,999.999999. If, on the other hand, you specify a data type of NUMERIC (10) for a column, that column can hold only numbers with a maximum value of 9,999.999999. The parameter (10) specifies the maximum number of digits possible in the number. If you specify a data type of NUMERIC (10,2) for a column, that column can hold numbers with a maximum value of 99,999,999.99. In this case, you may still have ten total digits, but only two of those digits can fall to the right of the decimal point.

Tip NUMERIC data is used for values such as 595.72. That value has a precision of 5 (the total number of digits) and a scale of 2 (the number of digits to the right of the decimal point). A data type of NUMERIC (5,2) is appropriate for such numbers.

DECIMAL data type

The DECIMAL data type is similar to NUMERIC. This data type can have a fractional component, and you can specify its precision and scale. The difference is that your implementation may specify a precision greater than what you specify — if so, the implementation uses the greater precision. If you do not specify precision or scale, the implementation uses default values, as it does with the NUMERIC type.

An item that you specify as NUMERIC (5,2) can never contain a number with an absolute value greater than 999.99. An item that you specify as DECIMAL (5,2) can always hold values up to 999.99, but if your SQL implementation permits larger values, then the DBMS won't reject values larger than 999.99.

Tip Use the NUMERIC or DECIMAL type if your data has fractional positions, and use the INTEGER, SMALLINT, or BIGINT type if your data always consists of whole numbers. Use the NUMERIC type rather than the DECIMAL type if you want to maximize portability, because a value that you define as NUMERIC (5,2), for example, holds the same range of values on all systems.

DECFLOAT data type

The DECFLOAT data type is new in SQL: 2016. Unlike the REAL and DOUBLE PRECISION data types, which are approximate numeric types and only provide binary approximations of decimal data, DECFLOAT combines the accuracy of the exact numeric DECIMAL data type with the performance advantages of the FLOAT data type. This is important for business applications, where approximations are not acceptable.

Approximate numerics

Some quantities have such a large range of possible values (many orders of magnitude) that a computer with a given register size can't represent all the values exactly. (Examples of register sizes are 32 bits, 64 bits, and 128 bits.) Usually in such cases, exactness isn’t necessary, and a close approximation is acceptable. SQL defines three approximate NUMERIC data types to handle this kind of data: REAL, DOUBLE PRECISION, and FLOAT (as detailed in the next three subsections).

REAL data type

The REAL data type gives you a single-precision, floating-point number — the precision of which depends on the SQL implementation. In general, the hardware you use determines precision. A 64-bit machine, for example, gives you more precision than does a 32-bit machine.

Remember A floating-point number is a number that contains a decimal point. The decimal point can “float” to different locations in the number, depending on the number's value. Examples include 3.1, 3.14, and 3.14159 — and yes, all three can be used as values for Π — each with a different precision.

DOUBLE PRECISION data type

The DOUBLE PRECISION data type gives you a double-precision floating-point number, the precision of which again depends on the implementation. Surprisingly, the meaning of the word DOUBLE also depends on the implementation. Double-precision arithmetic is primarily employed by scientific users. Different scientific disciplines require different levels of precision. Some SQL implementations cater to one category of users, and other implementations cater to other categories of users.

In some systems, the DOUBLE PRECISION type has exactly twice the capacity of the REAL data type for both mantissa and exponent. (In case you've forgotten what you learned in high school, you can represent any number as a mantissa multiplied by ten raised to the power given by an exponent. You can write 6,626, for example, as 6.626E3. The number 6.626 is the mantissa, which you multiply by ten raised to the third power; in that case, 3 is the exponent.)

You gain no benefit by representing numbers that are fairly close to 1 (such as 6,626 or even 6,626,000) with an approximate NUMERIC data type. Exact numeric types work just as well — and after all, they’re exact. For numbers that are either very near 0 or much larger than 1, however, such as 6.626E-34 (a very small number), you must use an approximate NUMERIC type. Exact NUMERIC data types can't hold such numbers. On other systems, the DOUBLE PRECISION type gives you somewhat more than twice the mantissa capacity — and somewhat less than twice the exponent capacity as the REAL type. On yet another type of system, the DOUBLE PRECISION type gives double the mantissa capacity but the same exponent capacity as the REAL type. In this case, accuracy doubles, but range does not.

Remember The SQL specification doesn't try to dictate, arbitrate, or establish by fiat what DOUBLE PRECISION means. The specification requires only that the precision of a DOUBLE PRECISION number be greater than the precision of a REAL number. Although this constraint is rather weak, it's probably the best possible, given the great differences you encounter in hardware.

FLOAT data type

The FLOAT data type is most useful if you think that you may someday migrate your database to a hardware platform with register sizes different from those available on your current platform. By using the FLOAT data type, you can specify a precision — for example, FLOAT (5). If your hardware supports the specified precision with its single-precision circuitry, then your present system uses single-precision arithmetic. If, after you migrate your database, the specified precision requires double-precision arithmetic, then the system uses double-precision arithmetic.

Tip Using FLOAT rather than REAL or DOUBLE PRECISION makes moving your databases to other hardware easier. That's because the FLOAT data type enables you to specify precision and lets the hardware fuss over whether to use single- or double-precision arithmetic. (Remember, the precision of REAL and DOUBLE PRECISION numbers is hardware-dependent.)

If you aren't sure whether to use the exact NUMERIC data types (that is, NUMERIC and DECIMAL) or the approximate NUMERIC data types (that is, FLOAT and REAL), use the exact NUMERIC types. Exact data types demand fewer system resources — and, of course, give exact (rather than approximate) results. If the range of possible values of your data is large enough to require you to use approximate data types, you can probably determine this fact in advance.

Character strings

Databases store many types of data, including graphic images, sounds, and animations. I expect odors to come next. Can you imagine a three-dimensional 1920-x-1080, 24-bit color image of a large slice of pepperoni pizza on your screen, while an odor sample taken at DiFilippi's Pizza Grotto replays through your super-multimedia card? Such a setup may get frustrating — at least until you can afford to add taste-type data to your system as well. Alas, you can expect to wait a long time before odor and taste become standard SQL data types. These days, the data types that you use most commonly — after the NUMERIC types, of course — are the character-string types.

You have three main types of CHARACTER data:

  • Fixed character data (CHARACTER or CHAR)
  • Varying character data (CHARACTER VARYING or VARCHAR)
  • Character large-object data (CHARACTER LARGE OBJECT or CLOB)

You also have three variants of these types of character data:

  • NATIONAL CHARACTER
  • NATIONAL CHARACTER VARYING
  • NATIONAL CHARACTER LARGE OBJECT

Details coming right up.

CHARACTER data type

If you define the data type of a column as CHARACTER or CHAR, you can specify the number of characters the column holds by using the syntax CHAR (x), where x is the number of characters. If you specify a column's data type as CHAR (16), for example, the maximum length of any data you can enter in the column is 16 characters. If you don’t specify an argument (that is, you don’t provide a value in place of the x, SQL assumes a field length of one character. If you enter data into a CHARACTER field of a specified length and you enter fewer characters than the specified number, SQL fills the remaining character spaces with blanks.

CHARACTER VARYING data type

The CHARACTER VARYING data type is useful if entries in a column can vary in length but you don't want SQL to pad the field with blanks. This data type enables you to store exactly the number of characters that the user enters. No default value exists for this data type. To specify this data type, use the form CHARACTER VARYING (x) or VARCHAR (x), where x is the maximum number of characters permitted.

CHARACTER LARGE OBJECT data type

The CHARACTER LARGE OBJECT (CLOB) data type was introduced with SQL:1999. As its name implies, it's used with huge character strings that are too large for the CHARACTER type. CLOBs behave much like ordinary character strings, but there are restrictions on what you can do with them.

For one thing, a CLOB may not be used in a PRIMARY KEY, FOREIGN KEY, or UNIQUE predicate. Furthermore, it may not be used in a comparison other than one for either equality or inequality. Because of their large size, applications generally do not transfer CLOBs to or from a database. Instead, a special client-side data type called a CLOB locator is used to manipulate the CLOB data. It's a parameter whose value identifies a large character-string object.

Remember A predicate is a statement that may either be logically True or logically False.

NATIONAL CHARACTER, NATIONAL CHARACTER VARYING, and NATIONAL CHARACTER LARGE OBJECT data types

Various languages have some characters that differ from any characters in another language. For example, German has some special characters not present in the English-language character set. Some languages, such as Russian, have a very different character set from that of English. For example, if you specify the English character set as the default for your system, you can use alternative character sets because the NATIONAL CHARACTER, NATIONAL CHARACTER VARYING, and NATIONAL CHARACTER LARGE OBJECT data types function the same as the CHARACTER, CHARACTER VARYING, and CHARACTER LARGE OBJECT data types — the only difference is that the character set you're specifying is different from the default character set.

You can specify the character set as you define a table column. If you want, each column can use a different character set. The following example of a table-creation statement uses multiple character sets:

CREATE TABLE XLATE (

LANGUAGE_1 CHARACTER (40),

LANGUAGE_2 CHARACTER VARYING (40) CHARACTER SET GREEK,

LANGUAGE_3 NATIONAL CHARACTER (40),

LANGUAGE_4 CHARACTER (40) CHARACTER SET KANJI

) ;

Here the LANGUAGE_1 column contains characters in the implementation’s default character set. The LANGUAGE_3 column contains characters in the implementation's national character set. The LANGUAGE_2 column contains Greek characters. And the LANGUAGE_4 column contains Kanji characters. After a long absence, Asian character sets, such as Kanji, are now available in many DBMS products.

Binary strings

The BINARY string data types were introduced in SQL:2008. Considering that binary data has been fundamental to digital computers since the Atanasoff-Berry Computer of the 1930s, this recognition of the importance of binary data seems a little late in coming to SQL. (Better late than never, I suppose.) There are three different binary types, BINARY, BINARY VARYING, and BINARY LARGE OBJECT.

BINARY data type

If you define the data type of a column as BINARY, you can specify the number of bytes (octets) the column holds by using the syntax BINARY (x), where x is the number of bytes. If you specify a column's data type as BINARY (16), for example, the binary string must be 16 bytes in length. BINARY data must be entered as bytes, starting with byte one.

BINARY VARYING data type

Use the BINARY VARYING or VARBINARY type when the length of a binary string is a variable. To specify this data type, use the form BINARY VARYING (x) or VARBINARY (x), where x is the maximum number of bytes permitted. The minimum size of the string is zero and the maximum size is x.

BINARY LARGE OBJECT data type

The BINARY LARGE OBJECT (BLOB) data type is used with huge binary strings that are too large for the BINARY type. Graphical images and music files are examples of huge binary strings. BLOBs behave much like ordinary binary strings, but SQL puts some restrictions on what you can do with them.

For one thing, you can't use a BLOB in a PRIMARY KEY, FOREIGN KEY, or UNIQUE predicate. Furthermore, no BLOBs are allowed in comparisons other than those for equality or inequality. BLOBs are large, so applications generally don't transfer actual BLOBs to or from a database. Instead, they use a special client-side data type called a BLOB locator to manipulate the BLOB data. The locator is a parameter whose value identifies a binary large object.

Booleans

The BOOLEAN data type consists of the distinct truth values True and False, as well as Unknown. If either a Boolean True or False value is compared to a NULL or Unknown truth value, the result will have the Unknown value.

Datetimes

The SQL standard defines five data types that deal with dates and times; they're called datetime data types, or simply datetimes. Considerable overlap exists among these data types, so some implementations you encounter may not support all five.

Warning Implementations that do not fully support all five data types for dates and times may have problems with databases that you try to migrate from another implementation. If you have trouble with a migration, check the source and the destination implementations to see how they represent dates and times.

DATE data type

The DATE type stores year, month, and day values of a date, in that order. The year value is four digits long, and the month and day values are both two digits long. A DATE value can represent any date from the year 0001 to the year 9999. The length of a DATE is ten positions, as in 1957-08-14.

TIME WITHOUT TIME ZONE data type

The TIME WITHOUT TIME ZONE data type stores hour, minute, and second values of time. The hours and minutes occupy two digits. The seconds value may be only two digits but may also expand to include an optional fractional part. Therefore, this data type can represent a time such as (for example) 32 minutes and 58.436 seconds past 9:00 a.m. as 09:32:58.436.

The precision of the fractional part is implementation-dependent but is at least six digits long. A TIME WITHOUT TIME ZONE value takes up eight positions (including colons) when the value has no fractional part, or nine positions (including the decimal point) plus the number of fractional digits when the value does include a fractional part. You specify TIME WITHOUT TIME ZONE type data either as TIME, which gives you the default of no fractional digits, or as TIME WITHOUT TIME ZONE (p), where p is the number of digit positions to the right of the decimal. The example in the preceding paragraph represents a data type of TIME WITHOUT TIME ZONE (3).

TIMESTAMP WITHOUT TIME ZONE data type

TIMESTAMP WITHOUT TIME ZONE data includes both date and time information. The lengths and the restrictions on the values of the components of TIMESTAMP WITHOUT TIME ZONE data are the same as they are for DATE and TIME WITHOUT TIME ZONE data, except for one difference: The default length of the fractional part of the time component of a TIMESTAMP WITHOUT TIME ZONE is six digits rather than zero.

If the value has no fractional digits, the length of a TIMESTAMP WITHOUT TIME ZONE is 19 positions — ten date positions, one space as a separator, and eight time positions, in that order. If fractional digits are present (six digits is the default), the length is 20 positions plus the number of fractional digits. The 20th position is for the decimal point. You specify a field as TIMESTAMP WITHOUT TIME ZONE type by using either TIMESTAMP WITHOUT TIME ZONE or TIMESTAMP WITHOUT TIME ZONE (p), where p is the number of fractional digit positions. The value of p can't be negative, and the implementation determines its maximum value.

TIME WITH TIME ZONE data type

The TIME WITH TIME ZONE data type is the same as the TIME WITHOUT TIME ZONE data type except this type adds information about the offset from Universal Time (UTC, the successor of Greenwich Mean Time or GMT). The value of the offset may range anywhere from –12:59 to +13:00. This additional information takes up six additional digit positions following the time — a hyphen as a separator, a plus or minus sign, and then the offset in hours (two digits) and minutes (two digits) with a colon in between the hours and minutes. A TIME WITH TIME ZONE value with no fractional part (the default) is 14 positions long. If you specify a fractional part, the field length is 15 positions plus the number of fractional digits.

TIMESTAMP WITH TIME ZONE data type

The TIMESTAMP WITH TIME ZONE data type functions the same as the TIMESTAMP WITHOUT TIME ZONE data type except that this data type also adds information about the offset from Universal Time. The additional information takes up six additional digit positions following the timestamp. (See the preceding section for the form of the time-zone information.) Including time-zone data sets up 25 positions for a field with no fractional part and 26 positions (plus the number of fractional digits) for fields that do include a fractional part. (Six is the default number of fractional digits.)

Intervals

The interval data types relate closely to the datetime data types. An interval is the difference between two datetime values. In many applications that deal with dates, times, or both, you sometimes need to determine the interval between two dates or two times.

SQL recognizes two distinct types of intervals: the year-month interval and the day-time interval. A year-month interval is the number of years and months between two dates. A day-time interval is the number of days, hours, minutes, and seconds between two instants within a month. You can't mix calculations involving a year-month interval with calculations involving a day-time interval, because months come in varying lengths (28, 29, 30, or 31 days long).

XML type

XML is an acronym for eXtensible Markup Language, which defines a set of rules for adding markup to data. The markup structures the data in a way that conveys what the data means. XML enables the sharing of data between very different platforms.

The XML data type has a tree structure, so a root node may have child nodes, which may, in turn, have children of their own. First introduced in SQL:2003, the XML type was fleshed out in SQL/XML:2005, and further augmented in SQL:2008. The 2005 edition defined five parameterized subtypes, while retaining the original plain-vanilla XML type. XML values can exist as instances of two or even more types, because some of the subtypes are subtypes of other subtypes. (Maybe I should call them sub-subtypes, or even sub-sub-subtypes. Fortunately, SQL:2008 defined a standard way of referring to subtypes.)

The primary modifiers of the XML type are SEQUENCE, CONTENT, and DOCUMENT. The secondary modifiers are UNTYPED, ANY, and XMLSCHEMA. Figure 2-1 shows the tree-like structure illustrating the hierarchical relationships among the subtypes.

Illustration of a tree-like structure depicting the hierarchical relationships of the XML subtypes.

FIGURE 2-1: The relationships of the XML subtypes.

The following list is a rundown of the XML types you should be familiar with. Don't freak out if it looks like Greek (or worse yet, Linear A) to you. I give a more detailed explanation of these types in Chapter 18. I’ve organized the list to begin with the most basic types and end with the most complicated:

  • XML(SEQUENCE): Every value in XML is either an SQL NULL value or an XQuery sequence. That way, every XML value is an instance of the XML(SEQUENCE) type. XQuery is a query language specifically designed to extract information from XML data. This is the most basic XML type.

    Remember XML(SEQUENCE) is the least restrictive of the XML types. It can accept values that are not well-formed XML values. The other XML types, on the other hand, aren't quite so forgiving.

  • XML(CONTENT(ANY)): This is a slightly more restrictive type than XML(SEQUENCE). Every XML value that is either a NULL value or an XQuery document node (or a child of that document node) is an instance of this type. Every instance of XML(CONTENT(ANY)) is also an instance of XML(SEQUENCE). XML values of the XML(CONTENT(ANY)) type are not necessarily well formed, either. Such values may be intermediate results in a query that are later reduced to well-formed values.
  • XML(CONTENT(UNTYPED)): This is more restrictive than XML(ANY CONTENT), and thus any value of the XML(CONTENT(UNTYPED)) type is also an instance of the XML(CONTENT(ANY)) type and the XML(SEQUENCE) type. Every XML value that is either the null value or a non-null value of type XML(CONTENT(ANY)) is an XQuery document node D, such that the following is true for every XQuery element node contained in the XQuery tree T rooted in D:
    • The type-name property is xdt:untyped.
    • The nilled property is False.
    • For every XQuery attribute node contained in T, the type property is xdt:untypedAtomic.
    • For every XQuery attribute node contained in T, the type property is a value of type-name XML(CONTENT(UNTYPED)).
  • XML(CONTENT(XMLSCHEMA)): This is a second subtype of XML(CONTENT(ANY)) besides XML(CONTENT(UNTYPED)). As such it is also a subtype of XML(SEQUENCE). Every XML value that is either the null value or a non-null value of type XML(CONTENT(ANY)) and is also an XQuery document node D such that every XQuery element node that is contained in the XQuery tree T rooted in D:
    • Is valid according to the XML Schema S, or
    • Is valid according to an XML namespace N in an XML Schema S, or
    • Is valid according to a global element declaration schema component E in an XML schema S, or
    • Is a value of type XML(CONTENT(XMLSCHEMA)), whose type descriptor includes the registered XML Schema descriptor of S, and, if N is specified, the XML namespace URI of N, or if E is specified, the XML namespace URI of E and the XML NCName of E.
  • XML(DOCUMENT(ANY)): This is another subtype of the XML(CONTENT(ANY)) type with the added restriction that instances of XML(DOCUMENT(ANY)) are document nodes that have exactly one XQuery element node, zero or more XQuery comment nodes, and zero or more XQuery processing instruction nodes.
  • XML(DOCUMENT(UNTYPED)): Every value that is either the NULL value or a non-null value of type XML(CONTENT(UNTYPED)) that is an XQuery document node whose children property has exactly one XQuery element node, zero or more XQuery comment nodes, and zero or more XQuery processing instruction nodes is a value of type XML(DOCUMENT(UNTYPED)). All instances of XML(DOCUMENT(UNTYPED)) are also instances of XML(CONTENT(UNTYPED)). Furthermore, all instances of XML(DOCUMENT(UNTYPED)) are also instances of XML(DOCUMENT(ANY)). XML(DOCUMENT(UNTYPED)) is the most restrictive of the subtypes, sharing the restrictions of all the other subtypes. Any document that qualifies as an XML(DOCUMENT(UNTYPED)) is also an instance of all the other XML subtypes.

ROW types

The ROW data type was introduced with SQL:1999. It's not that easy to understand, and as a beginning to intermediate SQL programmer, you may never use it. After all, people got by without it just fine between 1986 and 1999.

One notable thing about the ROW data type is that it violates the rules of normalization that E. F. Codd declared in the early days of relational database theory. (I talk more about those rules in Chapter 5.) One of the defining characteristics of first normal form is that a field in a table row may not be multivalued. A field may contain one and only one value. However, the ROW data type allows you to declare an entire row of data to be contained within a single field in a single row of a table — in other words, a row nested within a row.

Remember The normal forms, first articulated by Dr. Codd, are defining characteristics of relational databases. Inclusion of the ROW type in the SQL standard was the first attempt to broaden SQL beyond the pure relational model.

Consider the following SQL statement, which defines a ROW type for a person's address information:

CREATE ROW TYPE addr_typ (

Street CHARACTER VARYING (25),

City CHARACTER VARYING(20),

State CHARACTER (2),

PostalCode CHARACTER VARYING (9)

) ;

After it’s defined, the new ROW type can be used in a table definition:

CREATE TABLE CUSTOMER (

CustID INTEGER PRIMARY KEY,

LastName CHARACTER VARYING (25),

FirstName CHARACTER VARYING (20),

Address addr_typ,

Phone CHARACTER VARYING (15)

) ;

The advantage here is that if you’re maintaining address information for multiple entities — such as customers, vendors, employees, and stockholders — you need define the details of the address specification only once: in the ROW type definition.

Collection types

After SQL broke out of the relational straightjacket with SQL:1999, data types that violate first normal form became possible. It became possible for a field to contain a whole collection of objects rather than just one. The ARRAY type was introduced in SQL:1999, and the MULTISET type was introduced in SQL:2003.

Two collections may be compared to each other only if they are both the same type, either ARRAY or MULTISET, and if their element types are comparable. Because arrays have a defined element order, corresponding elements from the arrays can be compared. Multisets have no defined element order, but you can compare them if (a) an enumeration exists for each multiset being compared and (b) the enumerations can be paired.

ARRAY type

The ARRAY data type violates first normal form (1NF), but in a different way than the way the ROW type violates 1NF. The ARRAY type, a collection type, is not a distinct type in the same sense that CHARACTER and NUMERIC are distinct data types. An ARRAY type merely allows one of the other types to have multiple values within a single field of a table. For example, say your organization needs to be able to contact customers whether they're at work, at home, or on the road. You want to maintain multiple telephone numbers for them. You can do this by declaring the Phone attribute as an array, as shown in the following code:

CREATE TABLE CUSTOMER (

CustID INTEGER PRIMARY KEY,

LastName CHARACTER VARYING (25),

FirstName CHARACTER VARYING (20),

Address addr_typ,

Phone CHARACTER VARYING (15) ARRAY [3]

) ;

The ARRAY [3] notation allows you to store up to three telephone numbers in the CUSTOMER table. The three telephone numbers represent an example of a repeating group. Repeating groups are a no-no according to classical relational database theory, but this is one of several examples of cases where SQL:1999 broke the rules. When Dr. Codd first specified the rules of normalization, he traded off functional flexibility for data integrity. SQL:1999 took back some of that functional flexibility, at the cost of some added structural complexity.

Remember The increased structural complexity could translate into compromised data integrity if you are not fully aware of all the effects of the actions you perform on your database. Arrays are ordered, in that each element in an array is associated with exactly one ordinal position in the array.

An array is an ordered collection of values, and the cardinality of an array is the number of elements in the array. An SQL array can have any cardinality from zero up to and including some declared maximum number of elements. This means that the cardinality of a column of the array type can vary from one row to the next. An array can be atomically null, in which case its cardinality would also be null. A null array is not the same as an empty array, whose cardinality would be zero. An array that has only null elements would have a cardinality greater than zero. For example, an array with five null elements would have a cardinality of five.

If an array has a cardinality that is less than the declared maximum, the unused cells in the array are considered to be nonexistent. They are not considered to contain null values; they just aren't there at all.

You can access individual elements in an array by enclosing their subscripts in square brackets. If you have an array named Phone, then Phone [3] would refer to the third element of the Phone array.

Since SQL:1999, it has been possible to find out the cardinality of an array by invoking the CARDINALITY function. SQL:2011 added the ability to discover the maximum cardinality of an array by using the ARRAY_MAX_CARDINALITY function. This is very useful because it enables you to write general-purpose routines that apply to arrays with different maximum cardinalities. Routines with hard-coded maximum cardinalities apply only to arrays that have a given maximum cardinality and would have to be rewritten for arrays of any other maximum cardinality.

Whereas SQL:1999 introduced the ARRAY data type and the ability to address individual elements within an array, it did not make any provision for removing elements from an array. That oversight was corrected in SQL:2011 with the introduction of the TRIM_ARRAY function, which enables you to remove elements from the end of an array.

MULTISET type

A multiset is an unordered collection. Specific elements of the multiset may not be referenced; usually that's because those elements are not assigned specific ordinal positions in the multiset.

REF types

REF types are not part of core SQL. This means that a DBMS may claim compliance with the SQL standard without implementing REF types at all. The REF type is not a distinct data type in the sense that CHARACTER and NUMERIC are. Instead, it's a pointer to a data item, a row type, or an abstract data type that resides in a row of a table (a site). Dereferencing the pointer can retrieve the value stored at the target site.

If you’re confused, don’t worry, because you’re not alone. Using the REF types requires a working knowledge of object-oriented programming (OOP) principles. This book refrains from wading too deeply into the murky waters of OOP. In fact, because the REF types are not a part of core SQL, you may be better off if you don't use them. If you want maximum portability across DBMS platforms, stick to core SQL.

User-defined types

User-defined types (UDTs) represent another example of features that arrived in SQL:1999 that come from the object-oriented programming world. As an SQL programmer, you are no longer restricted to the data types defined in the SQL specification. You can define your own data types, using the principles of abstract data types (ADTs) found in such object-oriented programming languages as C++.

One of the most important benefits of UDTs is the fact that you can use them to eliminate the impedance mismatch between SQL and the host language that is “wrapped around” the SQL. A long-standing problem with SQL has been the fact the SQL’s predefined data types do not match the data types of the host languages within which SQL statements are embedded. Now, with UDTs, a database programmer can create data types within SQL that match the data types of the host language.

A UDT has attributes and methods, which are encapsulated within the UDT. The outside world can see the attribute definitions and the results of the methods — but the specific implementations of the methods are hidden from view. Access to the attributes and methods of a UDT can be further restricted by specifying that they are public, private, or protected:

  • Public attributes or methods are available to all users of a UDT.
  • Private attributes or methods are available only to the UDT itself.
  • Protected attributes or methods are available only to the UDT itself or its subtypes.

You see from this that a UDT in SQL behaves much like a class in an object-oriented programming language. Two forms of user-defined types exist: distinct types and structured types.

Distinct types

Distinct types are the simpler of the two forms of user-defined types. A distinct type’s defining feature is that it’s expressed as a single data type. It is constructed from one of the predefined data types, called the source type. Multiple distinct types that are all based on a single source type are distinct from each other; thus, they are not directly comparable. For example, you can use distinct types to distinguish between different currencies. Consider the following type definition:

CREATE DISTINCT TYPE USdollar AS DECIMAL (9,2) ;

This definition creates a new data type for U.S. dollars (USdollar), based on the predefined DECIMAL data type. You can create another distinct type in a similar manner:

CREATE DISTINCT TYPE Euro AS DECIMAL (9,2) ;

You can now create tables that use these new types:

CREATE TABLE USInvoice (

InvID INTEGER PRIMARY KEY,

CustID INTEGER,

EmpID INTEGER,

TotalSale USdollar,

Tax USdollar,

Shipping USdollar,

GrandTotal USdollar

) ;

 

CREATE TABLE EuroInvoice (

InvID INTEGER PRIMARY KEY,

CustID INTEGER,

EmpID INTEGER,

TotalSale Euro,

Tax Euro,

Shipping Euro,

GrandTotal Euro

) ;

The USdollar type and the Euro type are both based on the DECIMAL type, but instances of one cannot be directly compared with instances of the other or with instances of the DECIMAL type. In SQL, as in the real world, it is possible to convert U.S. dollars into euros, but doing so requires a special operation (CAST). After conversion is complete, comparisons are possible.

Structured types

The second form of user-defined type — the structured type — is expressed as a list of attribute definitions and methods instead of being based on a single predefined source type.

CONSTRUCTORS

When you create a structured UDT, the DBMS automatically creates a constructor function for it, giving it the same name as the UDT. The constructor's job is to initialize the attributes of the UDT to their default values.

MUTATORS AND OBSERVERS

When you create a structured UDT, the DBMS automatically creates a mutator function and an observer function. A mutator, when invoked, changes the value of an attribute of a structured type. An observer function is the opposite of a mutator function; its job is to retrieve the value of an attribute of a structured type. You can include observer functions in SELECT statements to retrieve values from a database.

SUBTYPES AND SUPERTYPES

A hierarchical relationship can exist between two structured types. For example, a type named MusicCDudt has a subtype named RockCDudt and another subtype named ClassicalCDudt. MusicCDudt is the supertype of those two subtypes. RockCDudt is a proper subtype of MusicCDudt if there is no subtype of MusicCDudt that is a supertype of RockCDudt. If RockCDudt has a subtype named HeavyMetalCDudt, HeavyMetalCDudt is also a subtype of MusicCDudt, but it is not a proper subtype of MusicCDudt.

A structured type that has no supertype is called a maximal supertype, and a structured type that has no subtypes is called a leaf subtype.

EXAMPLE OF A STRUCTURED TYPE

You can create structured UDTs in the following way:

/* Create a UDT named MusicCDudt */

CREATE TYPE MusicCDudt AS

/* Specify attributes */

Title CHAR(40),

Cost DECIMAL(9,2),

SuggestedPrice DECIMAL(9,2)

/* Allow for subtypes */

NOT FINAL ;

 

CREATE TYPE RockCDudt UNDER MusicCDudt NOT FINAL ;

The subtype RockCDudt inherits the attributes of its supertype MusicCDudt.

CREATE TYPE HeavyMetalCDudt UNDER RockCDudt FINAL ;

Now that you have the types, you can create tables that use them. Here's an example:

CREATE TABLE METALSKU (

Album HeavyMetalCDudt,

SKU INTEGER) ;

Now you can add rows to the new table:

BEGIN

/* Declare a temporary variable a */

DECLARE a = HeavyMetalCDudt ;

/* Execute the constructor function */

SET a = HeavyMetalCDudt() ;

/* Execute first mutator function */

SET a = a.title('Edward the Great') ;

/* Execute second mutator function */

SET a = a.cost(7.50) ;

/* Execute third mutator function */

SET a = a.suggestedprice(15.99) ;

INSERT INTO METALSKU VALUES (a, 31415926) ;

END

User-defined types sourced from collection types

In the earlier section “Distinct types,” I illustrate how you can create a user-defined type from a predefined type, using the example of creating a USDollar type from the DECIMAL type. This capability was introduced in SQL:1999. SQL:2011 expanded on this capability by enabling you to create a new user-defined type from a collection type. This enables the developer to define methods on the array as a whole, not just on the individual elements of the array, as allowed by SQL:1999.

Data type summary

Table 2-2 lists various data types and displays literals that conform to each type.

TABLE 2-2 Data Types

Data Type

Example Value

CHARACTER (20)

'Amateur Radio      '

VARCHAR (20)

'Amateur Radio'

CLOB (1000000)

'This character string is a million characters long …'

SMALLINT, BIGINT, or INTEGER

7500

NUMERIC, DECIMAL, or DECFLOAT

3425.432

REAL, FLOAT, or DOUBLE PRECISION

6.626E-34

BINARY (1)

'01100011'

VARBINARY (4)

'011000111100011011100110'

BLOB (1000000)

'1001001110101011010101010101…'

BOOLEAN

'TRUE'

DATE

DATE '1957-08-14'

TIME (2) WITHOUT TIME ZONE 1

TIME '12:46:02.43' WITHOUT TIME ZONE

TIME (3) WITH TIME ZONE

TIME '12:46:02.432-08:00' WITH TIME ZONE

TIMESTAMP WITHOUT TIME ZONE (0)

TIMESTAMP '1957-08-14 12:46:02' WITHOUT TIME ZONE

TIMESTAMP WITH TIME ZONE (0)

TIMESTAMP '1957-08-14 12:46:02-08:00' WITH TIME ZONE

INTERVAL DAY

INTERVAL '4' DAY

XML(SEQUENCE)

<Client>Vince Tenetria</Client>

ROW

ROW (Street VARCHAR (25), City VARCHAR (20), State CHAR (2), PostalCode VARCHAR (9))

ARRAY

INTEGER ARRAY [15]

MULTISET

No literal applies to the MULTISET type

REF

Not a type, but a pointer

USER DEFINED TYPE

Currency type based on DECIMAL

1 Argument specifies number of fractional digits.

Remember Your SQL implementation may not support all the data types that I describe in this section. Furthermore, your implementation may support nonstandard data types that I don't describe here. (Your mileage may vary, and so on. You know the drill.)

Null Values

Remember If a database field contains a data item, that field has a specific value. A field that does not contain a data item is said to have a null value. Keep in mind that

  • In a numeric field, a null value is not the same as a value of zero.
  • In a character field, a null value is not the same as a blank.

Both a numeric zero and a blank character are definite values. A null value indicates that a field’s value is undefined — its value is not known.

Many situations exist in which a field may have a null value. The following list describes a few of these situations and gives an example of each:

  • The value exists, but you don’t know what the value is yet. You set NUMBER to null in the Lifeforms row of the Exoplanets table before astronomers have discovered unequivocal evidence of life beyond our solar system in the Milky Way galaxy.
  • The value doesn't exist yet. You set TOTAL_SOLD to null in the SQL For Dummies, 9th Edition row of the BOOKS table because the first set of quarterly sales figures is not yet reported.
  • The field isn't applicable for this specific row. You set SEX to null in the C3PO row of the EMPLOYEE table because C3PO is a droid that has no gender. (You knew that.)
  • The value is out of range. You set SALARY to null in the Oprah Winfrey row of the EMPLOYEE table because you designed the SALARY column as type NUMERIC (8,2) and Oprah's contract calls for pay in excess of $999,999.99. (You knew that too.)

Tip A field can have a null value for many different reasons. Don’t jump to any hasty conclusions about what any particular null value means.

Constraints

Constraints are restrictions that you apply to the data that someone can enter into a database table. You may know, for example, that entries in a given numeric column must fall within a certain range. If anyone makes an entry that falls outside that range, then that entry must be an error. Applying a range constraint to the column prevents this type of error from happening.

Traditionally, the application program that uses the database applies any constraints to a database. The most recent DBMS products, however, enable you to apply constraints directly to the database. This approach has several advantages. If multiple applications use the same database, you apply the constraints only once (rather than multiple times). Also, adding constraints at the database level is usually simpler than adding them to an application. Often all you do is tack the appropriate clause onto your CREATE statement.

I discuss constraints and assertions (which are constraints that apply to more than one table) in detail in Chapter 5.

Using SQL in a Client/Server System

SQL is a data sublanguage that works on a standalone system or on a multiuser system. SQL works particularly well on a client/server system. On such a system, users on multiple client machines that connect to a server machine can access — via a local-area network (LAN) or other communications channel — a database that resides on the server to which they’re connected. The application program on a client machine contains SQL data-manipulation commands. The portion of the DBMS residing on the client sends these commands to the server across the communications channel that connects the server to the client. At the server, the server portion of the DBMS interprets and executes the SQL command and then sends the results back to the client across the communication channel. You can encode very complex operations into SQL at the client, and then decode and perform those operations at the server. This type of setup results in the most effective use of the bandwidth of that communication channel.

The server

Unless it receives a request from a client, the server does nothing; it just stands around and waits. If multiple clients require service at the same time, however, servers must respond quickly. Servers generally differ from client machines in terms of how much data they handle. They have large amounts of very fast disk storage, optimized for fast data access and retrieval. And because they must handle traffic coming in simultaneously from multiple client machines, servers need fast multi-core processors.

What the server is

The server (short for database server) is the part of a client/server system that holds the database. The server also holds the server software — the part of a database management system that interprets commands coming in from the clients and translates these commands into operations in the database. The server software also formats the results of retrieval requests and sends the results back to the requesting client.

What the server does

The server’s job is relatively simple and straightforward. All a server needs to do is read, interpret, and execute commands that come to it across the network from clients. Those commands are in one of several data sublanguages.

A sublanguage doesn’t qualify as a complete language — it implements only part of a language. A data sublanguage may, for example, deal only with data handling. The sublanguage has operations for inserting, updating, deleting, and selecting data, but may not have flow control structures such as DO loops, local variables, functions, procedures, or input/output to printers. SQL is the most common data sublanguage in use today and has become an industry standard. In fact, SQL has supplanted proprietary data sublanguages on machines in all performance classes. With SQL:1999, SQL acquired many of the features missing from traditional sublanguages. However, SQL is still not a complete general-purpose programming language; it must be combined with a host language to create a database application.

The client

The client part of a client/server system consists of a hardware component and a software component. The hardware component is the client computer and its interface to the local-area network. This client hardware may be very similar (or even identical) to the server hardware. The software is the distinguishing component of the client.

What the client is

The client’s primary job is to provide a user interface. As far as the user is concerned, the client machine is the computer, and the user interface is the application. The user may not even realize that the process involves a server. The server is usually out of sight — often in another room. Aside from the user interface, the client also contains the application program and the client part of the DBMS. The application program performs the specific task you require (say, in accounts receivable or order entry). The client part of the DBMS executes the application program’s commands and exchanges data and SQL data-manipulation commands with the server part of the DBMS.

What the client does

The client part of a DBMS displays information onscreen and responds to user input transmitted via the keyboard, mouse, or other input device. The client may also process data coming in from a telecommunications link or from other stations on the network. The client part of the DBMS does all the application-specific “thinking.” To a developer, the client part of a DBMS is the interesting part. The server part just handles the requests of the client part in a repetitive, mechanical fashion.

Using SQL on the Internet or an Intranet

Database operation on the Internet (now often called “the cloud”) and on intranets differs fundamentally from database operation in a traditional client/server system. The difference is primarily on the client end. In a traditional client/server system, much of the functionality of the DBMS resides on the client machine. On an Internet-based database system, most or all of the DBMS resides on the server. The client may host nothing more than a web browser. At most, the client holds a browser and a browser extension, such as a Firefox add-on or an ActiveX control. Thus, the conceptual “center of mass” of the system shifts toward the server. This shift has several advantages:

  • The client portion of the system (browser) is low-cost or even free.
  • You have a standardized user interface.
  • The client is easy to maintain.
  • You have a standardized client/server relationship.
  • You have a common means of displaying multimedia data.

The main disadvantages of performing database manipulations over the Internet involve security and data integrity:

  • To protect information from unwanted access or tampering, both the web server and the client browser must support strong encryption.
  • Browsers don’t perform adequate data-entry validation checks.
  • Database tables residing on different servers may become desynchronized.

Client and server extensions designed to address these concerns make the Internet a feasible location for production database applications. The architecture of an intranet is similar to that of the Internet, but security is less of a concern. Because the organization maintaining the intranet has physical control over all the client machines — as well as the servers and the network that connects these components together — an intranet suffers much less exposure to the efforts of malicious hackers. Data-entry errors and database desynchronization, however, do remain concerns.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.174.95