Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在创建表时重用约束_Sql_Constraints - Fatal编程技术网

Sql 在创建表时重用约束

Sql 在创建表时重用约束,sql,constraints,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 pri

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

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,
); 

不可能对另一列重用约束。如果需要,您必须为其他列定义它

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


欢迎使用StackOverflow:如果您发布代码、XML或数据示例,请在文本编辑器中突出显示这些行,并单击编辑器工具栏上的“代码示例”按钮(
{}
),以精确地格式化和语法突出显示它!将其设置为域约束,并使from_stn和to_stn都基于该域。
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