Implementing Data Constraints

Implementing Data Constraints

The data constraints are created using the SQL CHECK constraint. The format for the CHECK constraint is the word CONSTRAINT followed by a developer-provided constraint name followed by the word CHECK and then by the constraint specification in parentheses. Expres- sions in CHECK constraints are akin to those used in the WHERE clause of SQL statements. Thus, the SQL IN keyword is used to provide a list of valid values. The SQL NOT IN keyword also can be used for negatively expressed domain constraints (not shown in this example). The SQL LIKE keyword is used for the specification of decimal places. Range checks are specified using comparison operators such as the less than (<) and greater than (>) symbols. Because interrelation constraints are unsupported, comparisons can be made as intrarelation constraints between columns in the same table.

DBMS products are inconsistent in their implementation of CHECK constraints. The use of the SQL LIKE keyword in Figure 7-13, for example,

will not work with Oracle Database 11g. However, Oracle Database 11g implements other types of constraints. Unfortunately, you must learn the peculiarities of the DBMS you use to know how best to implement constraints.

Part 3 Database Implementation

Figure 7-13

SQL Statements to Create

Creating the View Ridge Database Tables

the ARTIST and WORK

Figure 7-14 shows SQL for creating all of the tables in the View Ridge database documented at

Tables with Default Values and Data Constraints

the end of Chapter 6. Read each line and be certain that you understand its function and pur- pose. Notice that deletions cascade for the relationships between CUSTOMER and CUS- TOMER_ARTIST_INT and between ARTIST and CUSTOMER_ARTIST_INT.

Any DBMS reserved words used as table or column names need to be enclosed in square brackets ([ and ]), and thus converted to delimited identifiers. We have already decided to use the table name TRANS instead of TRANSACTION so that we do not use the transaction reserved word. The table name WORK is also a potential problem; the word work is a reserved word in most DBMS products, as are the column names Description in the WORK table and State in the TRANS table. Enclosing such terms in brackets signifies to the SQL parser that these terms have been provided by the developer and are not to be used in the standard way. Ironically, SQL Server can process the word WORK without problem, but Oracle Database cannot, whereas SQL Server chokes on the word TRANSACTION, but Oracle Database has no problem with it. Because Figure 7-14 shows SQL Server 2008 R2 T-SQL statements, we use WORK (no brackets), [Description], and [State].

You can find a list of reserved words in the documentation for the DBMS product that you use, and we deal with some specific cases in the chapters dedicated to SQL Server 2008 R2, Oracle Database 11g, and MySQL 5.5. Be assured that if you use any keyword from the SQL syntax, such as SELECT, FROM, WHERE, LIKE, ORDER, ASC, DESC, for table or column names, you will have problems. Enclose such words in square brackets. And, of course, your life will be easier if you can avoid using such terms for tables or columns altogether.

Chapter 7 SQL for Database Construction and Application Processing

Figure 7-14

SQL Statements to Create the View Ridge Database Table Structure

(continued)

Every now and then, the DBMS might generate bizarre syntax-error messages. For example, suppose you define a table with the name

ORDER. When you submit the statement SELECT * FROM ORDER;, you will get very strange messages back from the DBMS because ORDER is an SQL reserved word.

If you do receive odd messages back from statements that you know are coded correctly, think about reserved words. If a term might be reserved, enclose it in brackets and see what happens when you submit it to the DBMS. No harm is done by enclosing SQL terms in brackets.

If you want to torture your DBMS, you can submit queries like SELECT [SELECT] FROM [FROM] WHERE [WHERE] < [NOT FIVE];. Most likely, you have better ways to spend your time, however. Without a doubt, the DBMS has better ways to spend its time!

Part 3 Database Implementation

Figure 7-14

Running the SQL statements in Figure 7-14 (or the specific variant in Chapter 10A for

Continued

Oracle Database 11g or Chapter 10B for MySQL) with your DBMS will generate all of the tables, relationships, and constraints for the View Ridge database. Figure 7-15 shows the completed table structure in SQL Server 2008 R2 as a database diagram. It is far easier to create these tables and relationships using SQL code than by using GUI displays, which are discussed in Chapter 10 (SQL Server 2008), Chapter 10A (Oracle Database 11g), and

Figure 7-15

Chapter 10B (MySQL 5.5).

SQL Server 2008 R2 View Ridge Database Diagram

Chapter 7 SQL for Database Construction and Application Processing

Microsoft Access 2007 ANSI-89 SQL, unfortunately, does not support a number of standard SQL features we have examined in this discussion. However, you can run a basic SQL CREATE TABLE statement in

ANSI-89 SQL, and then use the Microsoft Access GUI display to finish building the tables and relationships. Specifically:

1. Although Microsoft Access supports a Number data type, it does not support the (m, n) extension to specify the number of digits and the number of digits to the right of the decimal place.

Solution: You can set these values in the table Design view after the column is created.

2. Although Microsoft Access does support an AutoNumber data type, it always starts at 1 and increments by 1. Further, AutoNumber cannot be used as an SQL data type.

Solution: Set AutoNumber data type manually after the table is created. Any other numbering system must be supported manually or by application code.

3. Microsoft Access ANSI-89 SQL does not support the UNIQUE and CHECK column constraints, nor the DEFAULT keyword.

Solution: Equivalent constraints and initial values can be set in the GUI table Design view.

4. Microsoft Access does completely support foreign key CONSTRAINT phrases. Although the basic referential integrity constraint can be created using SQL, the ON UPDATE and ON DELETE clauses are not supported.

Solution: ON UPDATE and ON DELETE actions can be set manually after the relationship is created.

5. Unlike SQL Server, Oracle Database, and MySQL, Microsoft Access does not support SQL scripts.

Solution: You can still create tables by using the SQL CREATE command and insert data by using the SQL INSERT command (discussed later in this chapter), but you must do so one command at a time.