【问题标题】:Need to reference another table but must not be the same value as a column需要引用另一个表但不能与列的值相同
【发布时间】:2016-08-16 04:29:29
【问题描述】:

所以我有一个名为 Timesheet_approved 的表 funtom_timesheet,我需要引用表 funtom_employee(emp ID), timesheet_approved 列的值也不能与 timesheet_emp 列的值相同,这是我到目前为止的代码...

create table Funtom_timesheet 
(
timesheet_ID          number(3) constraint timesheet_pk primary key,
timesheet_Emp         number(3) constraint timesheet_Emp not null constraint timesheet_Emp references funtom_employee,
timesheet_Wc          date      constraint timesheet_Wc not null,  
timesheet_OT          number(2) default 0,
timesheet_Approved    number(3)  constraint timesheet_approved references funtom_employee constraint timesheet_approved unique(timesheet_Approved,timesheet_Emp) 
)
;

新代码仍然出错...

create table Funtom_timesheet 
(
    timesheet_ID          number(3) constraint timesheet_pk primary key,
    timesheet_Emp         number(3) constraint timesheet_Emp not null references funtom_employee(emp_ID),
    timesheet_Wc          date      constraint timesheet_Wc not null,  
    timesheet_hours   number(2),
    timesheet_OT          number(2) default 0,
    timesheet_Approved    number(3),
    constraint timesheet_approved_uc unique(timesheet_Approved,timesheet_Emp),
    constraint timesheet_approved references funtom_employee(emp_ID)

);

【问题讨论】:

  • 这与您上一个问题有一些相同的基本错误。你似乎在发明语法而不是following the documentation。我建议您尝试将那里所说的内容应用到此表中,然后查看您的位置。
  • 创建了这个但仍然有错误?创建表 Funtom_timesheet ( timesheet_ID number(3) 约束 timesheet_pk 主键, timesheet_Emp number(3) 约束 timesheet_Emp not null 引用 funtom_employee(emp_ID), timesheet_Wc 日期约束 timesheet_Wc not null, timesheet_hours number(2), timesheet_OT number(2) 默认0, timesheet_Approved number(3), 约束timesheet_approved_uc unique(timesheet_Approved,timesheet_Emp), 约束timesheet_approved 引用 funtom_employee(emp_ID) );
  • 看起来像是触发器的工作 - 但我不喜欢使用它们。我会确保 UI 只提供有效的填充选项。
  • @TomMaughan - 请编辑您的问题以表明,作为评论阅读并不容易。谢谢。说出您遇到的什么错误也很有帮助。不过,前两个内联约束仍然是错误的,与上一个问题相同。
  • @AlexPoole 抱歉。

标签: sql oracle constraints


【解决方案1】:

您仍然犯一些相同的错误,并且混合了内联和外联约束的语法。您发布的代码会得到 ORA-00907: missing right parenthesis 因为最后一个约束格式错误:

constraint timesheet_approved references funtom_employee(emp_ID)

应该在这个表中指定约束类型和列名:

constraint timesheet_approved foreign key (timesheet_Approved)
  references funtom_employee(emp_ID)

您对唯一约束的尝试也是错误的;这将检查timesheet_approvedtimesheet_em 的组合只能出现在表中的单行中,不是同一行中的两个列值不同。为此,您需要一个检查约束:

create table funtom_timesheet (
  timesheet_id number(3),
  timesheet_emp number(3) not null,
  timesheet_wc date not null,
  timesheet_hours number(2),
  timesheet_ot number(2) default 0,
  timesheet_approved number(3),
  constraint timesheet_pk primary key (timesheet_id),
  constraint timesheet_fk1 foreign key (timesheet_emp)
    references funtom_employee(emp_id),
  constraint timesheet_fk2 foreign key (timesheet_approved)
    references funtom_employee(emp_id),
  constraint timesheet_check check (timesheet_approved != timesheet_emp)
);

最后一行检查timesheet_approvedtimesheet_emp 的编号不同,即有人没有批准他们自己的时间表。这两个字段对有效的员工记录都有 FK 约束;检查约束确保它们不同。

【讨论】:

  • 非常感谢将使用此代码作为语法指南,从现在开始可以正常工作。
猜你喜欢
  • 1970-01-01
  • 2022-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多