在创建表时重用约束

时间:2012-07-20 08:40:37

标签: sql constraints

在创建表时,如何重用前一列中提到的约束?

create table ticket_details(
  from_stn char(3)
  constraint chk check(from_stn in ('vsh','mas','ndl'))
  constraint nn NOT NULL,
  to_stn char(3)
  constraint nn1 NOT NULL, (instead of crea)
  seat_no number(3)
  constraint PK primary key,
); 

2 个答案:

答案 0 :(得分:2)

将在域的任何实例上强制执行域约束。另外:改变后,你只需要在一个地方改变它。 (实现之间的语法可能略有不同)

CREATE DOMAIN THE_STN CHAR(3) constraint THE_STN_check_da_value check(VALUE in ('vsh','mas','ndl'))
        ;

CREATE table ticket_details
        ( seat_no INTEGER NOT NULL PRIMARY KEY
        , from_stn THE_STN NOT NULL
        , to_stn THE_STN
        );
INSERT INTO ticket_details(seat_no,from_stn,to_stn) VALUES (1, 'vsh', 'ndl' ); -- succeeds
INSERT INTO ticket_details(seat_no,from_stn,to_stn) VALUES (2, 'vsh', NULL ); -- succeeds
INSERT INTO ticket_details(seat_no,from_stn,to_stn) VALUES (2, 'lol', 'mas' ); -- fails

答案 1 :(得分:0)

无法为其他列重用约束。如果需要,您必须为其他列定义