CONSTRAINTS
1. Values for the staffid attribute should be a string of exactly 4 characters, and should start with
letter ‘s’.
2. Values for the sid attribute of the Student relation should be a string of exactly 4 characters, and
should start with letter ‘u’.
3. Values for the title attribute should be one of the following strings: “Lecturer”, “Senior
Lecturer”, “Associate Professor” and “Professor”.
4. Values for the credits attribute should be integers between 0 and 6.
5. Values for the sex attribute should be either ‘M’ (for male) or ‘F’ (for female). NULL values are not
allowed.
还是上次的 几个题目
我之后看了postgresql 的 document 和Google 了一下 
我想这几个constraint 是可以用create domain 来完成
请问 mysql 里面有没有 create domain这种
如果这些题目是放在mysql里面写  应该是如何写

解决方案 »

  1.   

    ALTER TABLE products ADD CHECK (name <> '');
    ALTER TABLE products ADD CONSTRAINT some_name UNIQUE (product_no);我想问  第一句是 在已经insert 了 data之后才有作用的吗?
      

  2.   

    不是 是在没有不满足的data的情况下才有作用
      

  3.   

    还有个问题  像第一二题里面  要string 只有 4个characters 和star with s   我应该在check()括号里面如何写
      

  4.   

    MYSQL 中不支持CHECK。 也不没有 domain 值域这个概念。唯一能用上的就是ENUM这个数据类型。
      

  5.   

    mysql> create table c(name varchar(4) check left(name,1)="s");
    Query OK, 0 rows affected (0.08 sec)mysql> desc c;
    +-------+------------+------+-----+---------+-------+
    | Field | Type       | Null | Key | Default | Extra |
    +-------+------------+------+-----+---------+-------+
    | name  | varchar(4) | YES  |     | NULL    |       |
    +-------+------------+------+-----+---------+-------+
    1 row in set (0.01 sec)mysql> insert into c values("asfd");
    Query OK, 1 row affected (0.03 sec)mysql> select * from c;
    +------+
    | name |
    +------+
    | asfd |
    +------+
    1 row in set (0.00 sec)
    mysql的check好像不好用啊
    ==我再看看
      

  6.   

    呵呵  版主说了 我就不查了
    1.
    至于你的验证可以在服务端验证插到表中都是以's'打头的数据
    2.
    也可以都插入
    然后在执行删除
    mysql> select * from c;
    +------+
    | name |
    +------+
    | asfd |
    | asfd |
    +------+
    2 rows in set (0.00 sec)mysql> delete from c where left(name,1)="a";
    Query OK, 2 rows affected (0.01 sec)mysql> select * from c;
    Empty set (0.00 sec)
      

  7.   

    MySQL目前的官方文档中在 create table 的说明中如此描述:
    The CHECK clause is parsed but ignored by all storage engines.
    如果一定要在MYSQL数据库中实现,则可以使用触发器来检查。
      

  8.   

    囧  神了  postgresql 关于check的例子 少之又少  这次project 火了