Using Data Types

Objects that contain data have an associated data type that defines the kind of data (character, integer, binary, and so on) the object can contain. The following objects have data types:

Assigning a data type to an object defines four attributes of the object:

If an object is defined as money, it can contain up to 19 digits, 4 of which can be to the right of the decimal. The object uses 8 bytes to store the data. The money data type therefore has a precision of 19, a scale of 4, and a length of 8.

Transact-SQL has these base data types:

binary bit char cursor datetime
decimal float image int money
nchar ntext nvarchar real smalldatetime
smallint smallmoney text timestamp tinyint
varbinary varchar uniqueidentifier    

All data stored in Microsoft® SQL Server™ must be compatible with one of these base data types. The cursor data type is the only base data type that cannot be assigned to a table column. It can be used only for variables and stored procedure parameters.

Several base data types have synonyms, for example numeric is a synonym for decimal and national character varying is a synonym for nvarchar.

User-defined data types can also be created, for example:

-- Create a birthday datetype that allows nulls.

EXEC sp_addtype birthday, datetime, 'NULL'

GO

-- Create a table using the new data type.

CREATE TABLE employee

  

emp_id char(5)
emp_first_name char(30)
emp_last_name char(40)
emp_birthday birthday

User-defined data types are always defined in terms of a base data type. They provide a mechanism for applying a name to a data type that is more descriptive of the types of values to be held in the object. This can make it easier for a programmer or database administrator to understand the intended use of any object defined with the data type.

SQL Server installations include a user-defined data type named sysname. sysname is used for table columns, variables, and stored procedure parameters that store object names. The exact definition of sysname is related to the rules for identifiers; therefore, it can vary between versions of SQL Server. In SQL Server version 7.0 sysname is defined as nvarchar(128). SQL Server versions up to 6.5 support only smaller identifiers; in these, sysname is defined as varchar(30).

See Also
Designing Tables Data Types
CREATE TABLE sp_addtype

  


(c) 1988-98 Microsoft Corporation. All Rights Reserved.