使用rule或者CHECK约束都可以达到你要的效果!
建议在数据库层实施!如果是在界面层,建议定义属性检查。

解决方案 »

  1.   

    CHECK 约束
    列可以有任意多个 CHECK 约束,并且约束条件中可以包含用 AND 和 OR 组合起来的多个逻辑表达式。列上的多个 CHECK 约束按创建顺序进行验证。
    搜索条件必须取值为布尔表达式,并且不能引用其它表。
    列级 CHECK 约束只能引用被约束的列,表级 CHECK 约束只能引用同一表中的列。 
    当执行 INSERT 和 DELETE 语句时,CHECK CONSTRAINTS 和规则具有相同的数据验证功能。当列上存在规则和一个或多个 CHECK 约束时,将验证所有限制。 
    LZ参考
    --使用 CHECK 约束
    --下例显示对输入到 jobs 表中的 min_lvl 列和 max_lvl 列的值的限制。这两个约束都未命名:CHECK (min_lvl >= 10)--与CHECK (max_lvl <= 250)--下例显示对输入到 employee 表的 emp_id 列中的字符数据具有模式限制的命名约束。CONSTRAINT CK_emp_id CHECK (emp_id LIKE 
       '[A-Z][A-Z][A-Z][1-9][0-9][0-9][0-9][0-9][FM]' OR
       emp_id LIKE '[A-Z]-[A-Z][1-9][0-9][0-9][0-9][0-9][FM]')--下例指定 pub_id 必须在特定的列表中或遵循给定的模式。此约束用于 publishers 表中的 pub_id 列。CHECK (pub_id IN ('1389', '0736', '0877', '1622', '1756')
       OR pub_id LIKE '99[0-9][0-9]')--F. 完整的表定义
    --下例显示 pubs 数据库中所创建的三个表(jobs、employee 和 publishers)的完整表定义,其中包含所有的约束定义。/* ************************** jobs table ************************** */
    CREATE TABLE jobs
    (
       job_id  smallint
          IDENTITY(1,1)
          PRIMARY KEY CLUSTERED,
       job_desc        varchar(50)     NOT NULL
          DEFAULT 'New Position - title not formalized yet',
       min_lvl tinyint NOT NULL
          CHECK (min_lvl >= 10),
       max_lvl tinyint NOT NULL
          CHECK (max_lvl <= 250)
    )/* ************************* employee table ************************* */
    CREATE TABLE employee 
    (
       emp_id  empid
          CONSTRAINT PK_emp_id PRIMARY KEY NONCLUSTERED
          CONSTRAINT CK_emp_id CHECK (emp_id LIKE 
             '[A-Z][A-Z][A-Z][1-9][0-9][0-9][0-9][0-9][FM]' or
             emp_id LIKE '[A-Z]-[A-Z][1-9][0-9][0-9][0-9][0-9][FM]'),
          /* Each employee ID consists of three characters that 
          represent the employee's initials, followed by a five 
          digit number ranging from 10000 through 99999 and then the 
          employee's gender (M or F). A (hyphen) - is acceptable 
          for the middle initial. */
       fname   varchar(20)     NOT NULL,
       minit   char(1) NULL,
       lname   varchar(30)     NOT NULL,
       job_id  smallint        NOT NULL
          DEFAULT 1
          /* Entry job_id for new hires. */
          REFERENCES jobs(job_id),
       job_lvl tinyint
          DEFAULT 10,
          /* Entry job_lvl for new hires. */
       pub_id  char(4) NOT NULL
          DEFAULT ('9952')
          REFERENCES publishers(pub_id),
          /* By default, the Parent Company Publisher is the company
          to whom each employee reports. */
       hire_date       datetime        NOT NULL
          DEFAULT (getdate())
          /* By default, the current system date is entered. */
    )/* ***************** publishers table ******************** */
    CREATE TABLE publishers
    (
       pub_id  char(4) NOT NULL 
             CONSTRAINT UPKCL_pubind PRIMARY KEY CLUSTERED
             CHECK (pub_id IN ('1389', '0736', '0877', '1622', '1756')
                OR pub_id LIKE '99[0-9][0-9]'),
       pub_name      varchar(40)     NULL,
       city         varchar(20)     NULL,
       state      char(2) NULL,
       country      varchar(30)     NULL
                DEFAULT('USA')
    )