Using Datatypes to Enforce Data Integrity

You can use system datatypes (supplied by SQL Server ) and user-defined datatypes (custom datatypes based on system datatypes) to enforce data integrity.

You create user-defined datatypes with the sp_addtype system stored procedure. When you create user-defined datatypes, you supply three parameters: the name of the datatype, the system datatype upon which the new datatype is based, and the datatype's nullability (whether it allows null values ¾ if defined explicitly, NULL or NOT NULL). When nullability is not explicitly defined, it will be assigned based on the ANSI null default setting for the database or connection.

Note Most examples refer to the pubs sample database. The pubs sample database is included as a learning tool; it is created when the server is installed. To use the sample database, your system must have access to the pubs database. If you have a localized version of SQL Server and want to try the examples, drop the localized version of pubs and install the U.S. English version of pubs. To install the U.S. English version of pubs, run the INSTPUBS.SQL script with the isql command-line utility. This script can be found in the INSTALL directory of SQL Server. Also if other users have executed the examples in this chapter, it is likely that the pubs database has been altered. This will change the output you see from some of the examples. You can reinstall the original pubs database at any time by running the INSTPUBS.SQL script.

To run the INSTPUBS.SQL script, from an operating-system prompt, type:

isql /Usa /Ppassword /Sserver -i\sql60\install\instpubs.sql

For details about isql, see the isql command-line utility in the Microsoft SQL Server Transact-SQL Reference.