create rule

Description

Specifies the domain of acceptable values for a particular column or for any column of a user-defined datatype and creates access rules.

Syntax

create [ [ and | or ] access]] rule
	[owner.]rule_name 
	as condition_expression

Parameters

access

specifies that you are creating an access rule. For information on access rules, see Chapter 11, “Managing User Permissions” in the System Administration Guide.

rule_name

is the name of the new rule. It must conform to the rules for identifiers and cannot be a variable. Specify the owner’s name to create another rule of the same name owned by a different user in the current database. The default value for owner is the current user.

condition_expression

specifies the conditions that define the rule. It can be any expression that is valid in a where clause, and can include arithmetic operators, relational operators, in, like, between, and so on. However, it cannot reference a column or any other database object. Built-in functions that do not reference database objects can be included.

A condition_expression takes one argument. The argument is prefixed by the @ sign and refers to the value that is entered via the update or insert command. You can use any name or symbol to represent the value when you write the rule, but the first character must be the @ sign. Enclose character and date constants in quotes, and precede binary constants with “0x”.

Examples

Example 1

Creates a rule named limit, which limits the value of advance to less than $1000:

create rule limit 
as @advance < $1000

Example 2

Creates a rule named pubid_rule, which restricts the values of pub_id to 1389, 0736, or 0877:

create rule pubid_rule 
as @pub_id in ('1389', '0736', '0877')

Example 3

Creates a rule named picture, which restricts the value of value to always begin with the indicated characters:

create rule picture 
as @value like '_-%[0-9]'

Usage


Restrictions


Binding rules


Rules and NULLs


Defaults and rules


Using integrity constraints in place of rules


Getting information about rules

Standards

SQL92 – Compliance level: Entry-level compliant.

To create rules using SQL92-compliant syntax, use the check clause of the create table statement.

Permissions

create rule permission defaults to the Database Owner, who can transfer it to other users.

See also

Commands alter table, create default, create table, drop rule, drop table

System procedures sp_bindrule, sp_help, sp_helptext, sp_hidetext, sp_rename, sp_unbindrule