Domains are aliases for built-in data types, including precision and scale values where applicable.
Domains, also called user-defined data types, allow columns throughout a database to be defined automatically on the same data type, with the same NULL or NOT NULL condition. This encourages consistency throughout the database. Domain names are case-insensitive. Sybase IQ returns an error if you attempt to create a domain with the same name as an existing domain except for case.
You create domains using the CREATE DOMAIN statement. See “CREATE DOMAIN statement” in Reference: Statements and Options.
The following statement creates a data type named street_address, which is a 35-character string:
CREATE DOMAIN street_address CHAR( 35 )
You can use CREATE DATATYPE as an alternative to CREATE DOMAIN, but this is not recommended, as CREATE DOMAIN is the syntax used in the draft SQL/3 standard.
Resource authority is required to create data types. Once a data type is created, the user ID that executed the CREATE DOMAIN statement is the owner of that data type. Any user can use the data type, and unlike other database objects, the owner name is never used to prefix the data type name.
The street_address data type may be used in exactly the same way as any other data type when defining columns. For example, the following table with two columns has the second column as a street_address column:
CREATE TABLE twocol (id INT, street street_address)
Owners or DBAs can drop domains by issuing a COMMIT and then using the DROP DOMAIN statement:
DROP DOMAIN street_address
You can carry out this statement only if no tables in the database are using data type.
Many of the attributes associated with columns, such as allowing NULL values, having a DEFAULT value, and so on, can be built into a user-defined data type. Any column that is defined on the data type automatically inherits the NULL setting, CHECK condition, and DEFAULT values. This allows uniformity to be built into columns with a similar meaning throughout a database.
For example, many primary key columns in the sample database are integer columns holding ID numbers. The following statement creates a data type that may be useful for such columns:
CREATE DOMAIN id INT NOT NULL DEFAULT AUTOINCREMENT CHECK( @col > 0 )
Any column created using the data type id is not allowed to hold NULLs, defaults to an autoincremented value, and must hold a positive number. Any identifier could be used instead of col in the @col variable.
The attributes of the data type can be overridden if needed by explicitly providing attributes for the column. A column created on data type id with NULL values explicitly allowed does allow NULLs, regardless of the setting in the id data type.
Named constraints and defaults In Sybase IQ, user-defined data types are created with a base data type, and optionally, a NULL or NOT NULL condition. Named constraints and named defaults are not supported.
Creating data types In Sybase IQ, you can use the sp_addtype system procedure to add a domain, or you can use the CREATE DOMAIN statement. In Adaptive Server Enterprise, you must use sp_addtype.