Tom van Stiphout (to*************@cox.net) writes:
I'm designing a new database from scratch. It has some columns that I
currently have as datatype=int with check constraint of x>=1 and x
<=32, or others with a range of 1-3, etc. It occurred to me I could
create a UDDT to represent each. Benefit would be that I don't have to
enter the check constraints all the time.
Is there any consensus on whether or not this is a good idea? I am a
bit concerned my front-end application (.NET) will not be able to
recognize these types as easily as "int".
If you have any experience using a CLR-defined type in SQL Server I'd
love to hear from you too.
There is some confusion here. The CREATE TYPE command can be used to
define types in two ways:
CREATE TYPE mytype FROM varchar(23)
CREATE TYPE mytype EXTERNAL NAME ...
The former is known as "alias data types" in Books Online and User-Defined
Data types" in Mgmt Studio. MS appears to prefer User-Defined Types (UDT)
for the latter. I prefer to call them CLR types.
It seems that you have CLR types in mind, but I am not sure.
Anyway, I think that creating a CLR type to encode an integer value
that may only be a in certain range, is making thins overly complex.
On the other hand, using an alias data type could be a could solution
in lieu of a domain:
CREATE TYPE mytype AS tinyint
CREATE RULE myrule AS @x IN (1, 2, 3)
EXEC sp_bindrule myrule, mytype1to3
Bound rules and defaults have been deprecated, and originally Microsoft
said that SQL 2008 would be the last version to have them. I beat them
up over that, pointing out that as long as they don't have real domains,
they need to keep bound rules and defauls.
--
Erland Sommarskog, SQL Server MVP,
es****@sommarskog.se
Links for SQL Server Books Online:
SQL 2008:
http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005:
http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000:
http://www.microsoft.com/sql/prodinf...ons/books.mspx