Sei sulla pagina 1di 1

26 Chapter 1: Creating

User-defined data types are simply a shorthand notation for the full column def-
inition. Ease of coding is the only real advantage, and there are several
disadvantages. First of all, a user-defined data type cannot be dropped or
changed without dropping all the references to it first. Second, a user-defined
data type hides the true column definition from the application programmer, and
that information is often important when writing applications. Third, constraint
names cannot be used so its hard to make the error messages meaningful.
Finally, except for the built-in base data type, all the properties of a
user-defined data type can be overridden in the CREATE TABLE. Here is an
example where the INSERT puts an empty string into address_2 and NULL into
address_3 even though those values deviate from the CREATE DOMAIN
definition:
CREATE DOMAIN address AS VARCHAR ( 100 )
NOT NULL
DEFAULT ''
CHECK ( LENGTH ( TRIM ( @col ) ) > 0 );

CREATE TABLE office (


office_code INTEGER PRIMARY KEY,
address_1 address,
address_2 address CHECK ( address_2 IS NOT NULL ),
address_3 address NULL DEFAULT ( NULL ) );

INSERT office ( office_code, address_1 ) VALUES ( 1, '123 Main Street' );


To refer to the current column name in a CREATE DOMAIN CHECK condi-
tion, use any identifier beginning with @. In the example above, @col is
replaced with address_1, address_2, and address_3 when the CREATE TABLE
is processed.
SQL Anywhere 9 provides several simple user-defined data types for com-
patibility with Microsoft SQL Server and Sybase Adaptive Server Enterprise.
For example, DATETIME corresponds to the built-in type TIMESTAMP, and
TEXT is defined as LONG VARCHAR.
<builtin_user_defined_data_type> ::= DATETIME
| IMAGE
| MONEY
| OLDBIT
| SMALLDATETIME
| SMALLMONEY
| SYSNAME
| TEXT
| UNIQUEIDENTIFIER
| UNIQUEIDENTIFIERSTR
| XML
Table 1-2 describes the built-in user-defined data types.
Table 1-2. Built-in user-defined data types
Data Type Definition
DATETIME TIMESTAMP
IMAGE LONG BINARY
MONEY NUMERIC ( 19, 4 )

Potrebbero piacerti anche