题目要求Values for the semester attribute should be strings in the following format: YYYYsX, where YYYY
stands for the year and X is either “1” or “2”. For example, this session (semester) corresponds to
2009s2.tableCREATE TABLE Offering (
    code CHAR(8), -- PK
    semester    CHAR(20), -- PK, from its own partial key
    lecturer CHAR(8), -- FK                
    roomno CHAR(12), -- FK                
    day CHAR(3), -- Use the 3 letter abbrev. (UPPER CASE); Nullable
    starttime TIME, -- Postgresql have TIME data type.
    endtime TIME,
    PRIMARY KEY (code, semester),我的写法ALTER TABLE Offering
  ALTER COLUMN semester TYPE CHAR(6), 
  ADD CONSTRAINT ck_semester CHECK (semester like '[0-9][0-9][0-9][0-9][s][1-2]');输入数据 INSERT INTO Offering VALUES ('COMP2011', '2004s1', 's002', 'EE G24',
 'Thu', '9:00:00', '10:00:00');
报错 说违反了 ck semester的 constraint 

解决方案 »

  1.   

     (semester like '[0-9][0-9][0-9][0-9]s[1-2]');
      

  2.   

    不知道,你用的什么数据库,反正MYSQL中是没这种写法。
      

  3.   

    mysql里面目前不支持这种check约束
    你可以考虑把insert操作写成一个存储过程接口,然后相关数据检查操作在存储过程里面来判断(合适的就插入,不合适的就存储过程输出参数返回错误)
      

  4.   

    mysql> select "a" regexp '^[0-9]*';
    +----------------------+
    | "a" regexp '^[0-9]*' |
    +----------------------+
    |                    1 |
    +----------------------+
    1 row in set (0.00 sec)
    你可以在存储过程里面判断
      

  5.   

    谢谢楼上诸位  但是我现在这个project 要用postgresql 来写   constraint 要独立成一个sql 文件   等postgresql 高手