4

i don't know how to create a domain with optional constrains? i tried

CREATE DOMAIN idx INT CHECK (VALUE > 100 AND VALUE < 999);

but faced the below err Unknown object type 'DOMAIN' used in a CREATE, DROP, or ALTER statement.

r bhd
  • 61
  • 1
  • 1
  • 3
  • Do you mean `CREATE TYPE`? The error is telling you the problem here; `DOMAIN` isn't a SQL Server data type. – Thom A Feb 23 '18 at 10:25
  • yes,my problems is type creation – r bhd Feb 23 '18 at 10:32
  • That comment doesn't add anything. Did using the correct keyword fix the problem? – Thom A Feb 23 '18 at 10:36
  • i didn't get your comment. my main problem is type creation in sql server. i don't know the true syntax of type creation – r bhd Feb 23 '18 at 10:45
  • As I said, `CREATE DOMAIN` isn't valid syntax; do you mean `CREATE TYPE`? I can't really be more clear here. – Thom A Feb 23 '18 at 10:56
  • yse i mean CREATE TYPE – r bhd Feb 23 '18 at 10:57
  • 2
    Then why haven't you used `CREATE TYPE`..? – Thom A Feb 23 '18 at 10:58
  • i cant define constraint in create type. just i can set name and base_type – r bhd Feb 23 '18 at 11:03
  • You don't add Constraints to data types, you add rules. Have a look at: https://www.mssqltips.com/sqlservertip/1628/sql-server-user-defined-data-types-rules-and-defaults/ – Thom A Feb 23 '18 at 11:05
  • i want to create a type that is tenth multiply such as 10,20,30,.... – r bhd Feb 23 '18 at 11:12
  • So, you want a data type where `[Value] % 10 = 0`? So, would `1010` be valid? Your `CONSTRAINT` at in your question allows any value between `100` and `999`. None of the values you just listed (`10`, `20` and `30`) are between `100` and `999`. This completely conflicts with your original question. I think you need to edit your question and actually provide your needs and goals. – Thom A Feb 23 '18 at 11:16
  • thank you. i finde solution in next answer – r bhd Feb 23 '18 at 13:19

2 Answers2

9
CREATE TYPE myType AS TABLE
(
   idx INT,
   CHECK (idx > 100 AND idx < 999)
)

Or you can also create rules and bind them to your type.

CREATE TYPE [dbo].[myType] 
FROM [INT] NOT NULL
GO

CREATE RULE multiplyByTen
AS @myType % 10 = 0
AND @myType > 100
AND @myType < 999

Then Bind Rule

EXEC sp_bindrule 'multiplyByTen', 'myType'
otri
  • 500
  • 3
  • 12
  • 4
    `sp_bindrule`: **Important** This feature will be removed in a future version of Microsoft SQL Server. – Ian Boyd Jul 20 '19 at 21:15
2

You might want to look at this.

It explains how to create a type in sql-server. There's 3 kind of types. The one you're trying to create does not allow adding a CHECK CONSTRAINT. You need to use rules instead.

In your case, you should be using this next queries:

--First we create the rule.
CREATE RULE range_rule  
AS   
    @range >= 100 AND @range < 999; 
GO

--Then we create the user-defined data type
CREATE TYPE int_and_range FROM INT;

--Then we bind the rule to the data type.
EXEC sys.sp_bindrule @rulename=N'[dbo].[range_rule]', @objname=N'[dbo].[int_and_range]'

After doing this, we could have such test:

CREATE TABLE test_table (
    custom_data_type_column int_and_range
)

--Try to insert a value against our rule
INSERT INTO dbo.test_table
        ( custom_data_type_column )
VALUES  ( 10  
          )

--RESULT:
--A column insert or update conflicts with a rule imposed by a previous CREATE RULE statement. The statement was terminated. 
--The conflict occurred in database 'db', table 'dbo.test_table', column 'custom_data_type_column'.  
--The statement has been terminated.

--Inserting a valid data:  
INSERT INTO dbo.test_table
        ( custom_data_type_column )
VALUES  ( 100 )

--RESULT:
--(1 row(s) affected)

--Select
Select * FROM test_table

--RESULT:

custom_data_type_column
-----------------------
100

(1 row(s) affected)

Beware though, CREATE RULE page says:

This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature. We recommend that you use check constraints instead. Check constraints are created by using the CHECK keyword of CREATE TABLE or ALTER TABLE. For more information, see Unique Constraints and Check Constraints.

Paul Karam
  • 4,052
  • 8
  • 30
  • 53