SQL创建表依赖项
我一直致力于创建表格 下面是我一直在研究的代码,如何使代码在SQL创建表依赖项,sql,oracle,create-table,Sql,Oracle,Create Table,我一直致力于创建表格 下面是我一直在研究的代码,如何使代码在loan\u type中工作,以编写上班族或非上班族代码 create table loaner ( loan_id number(5) primary key, loan_type VARCHAR2 (16), loan_start_date date, loan_end_date date, ) create table office_worker ( worker_id number(5) pr
loan\u type
中工作,以编写上班族或非上班族代码
create table loaner
(
loan_id number(5) primary key,
loan_type VARCHAR2 (16),
loan_start_date date,
loan_end_date date,
)
create table office_worker
(
worker_id number(5) primary_key,
loan_id number(5) references loaner(loan_id),
worker_name varchar2(50)
)
create table nonoffice_worker
(
nonworker_id number(5) primary_key,
loan_id number(5) references loaner(loan_id),
nonworker_name varchar2(50)
);
commit;
不能创建约束来检查现有表结构的约束。通常的做法如下:
create table loaner (
loan_id number(5) primary key,
loan_type VARCHAR2 (16),
loan_start_date date,
loan_end_date date,
constraint loaner_uk unique (loan_id, loan_type)
);
create table office_worker (
worker_id number(5) primary_key,
loan_id number(5),
loan_type VARCHAR2 (16),
worker_name varchar2(50),
constraint office_worker_loaner_fk foreeign key (loan_id, loan_type) references loaner (loan_id, loan_type),
constraint office_worker_loan_type_chk check (loan_type = 'OFFICE')
);
create table nonoffice_worker (
nonworker_id number(5) primary_key,
loan_id number(5),
loan_type VARCHAR2 (16),
nonworker_name varchar2(50),
constraint nonoffice_worker_loaner_fk foreeign key (loan_id, loan_type) references loaner (loan_id, loan_type),
constraint nonoffice_worker_loan_type_chk check (loan_type = 'NONOFFICE')
);
即:
您还可以向表中添加检查约束,但必须检查“loan_type”列是否仅包含所需的值(
office worker
或非office worker
),否则这将不起作用:
alter table loaner add (CONSTRAINT chk_loan_type CHECK
(loan_type='office worker' or loan_type='non office worker'));
您是否遇到错误?如果将所有员工都放在一个表中,并且只包含一个名为
is_office
的列,不是更好吗?不,没有错误,办公室员工和非办公室员工的贷款类型不同,因此,我需要的是,如何对借贷者类型是上班族还是非上班族进行检查worker@Matt对不起,忘了给你贴标签了,伙计。他们只是工人之间的区别吗?贷款类型?这似乎很复杂,谢谢你的帮助。这是一个有效的解决方案。我还添加了check约束选项,以防他的表在loan_type列上有rigth值