代码中的.id是什么意思?是rowid伪列吗?

时间:2019-05-17 20:03:20

标签: oracle oracle-livesql

我是oracle的新手,正在尝试各种代码。我正在oracle live sql工作表和quick sql shorthand中练习sql。快速SQL速记为我不了解的部门和员工提供了现成的代码 这行其中     employee.department_id = department.id 。我不理解 .id列,因为在部门表创建期间没有创建.id列。是通过Employees表中的外键引用自动生成的department_id列(pkey)?还是伪列。

我在oracle sql文档中遇到过rowid伪列。

这是代码

create table departments (
    name                           varchar2(255) not null,
    location                       varchar2(4000),
    country                        varchar2(4000)
)
;

create table employees (
    department_id                  number
                                   constraint employees_department_id_fk
                                   references departments on delete cascade,
    name                           varchar2(50) not null,
    email                          varchar2(255),
    cost_center                    number,
    date_hired                     date,
    job                            varchar2(255)
)
;


-- triggers
create or replace trigger departments_biu
    before insert or update 
    on departments
    for each row
begin
    null;
end departments_biu;
/

create or replace trigger employees_biu
    before insert or update 
    on employees
    for each row
begin
    :new.email := lower(:new.email);
end employees_biu;
/


-- indexes
create index employees_i1 on employees (department_id);

-- create views
create or replace view emp_v as 
select 
    departments.name                                   department_name,
    departments.location                               location,
    departments.country                                country,
    employees.name                                     employee_name,
    employees.email                                    email,
    employees.cost_center                              cost_center,
    employees.date_hired                               date_hired,
    employees.job                                      job
from 
    departments,
    employees
where
    *employees.department_id(+) = departments.id;*
/

-- load data

insert into departments (
    name,
    location,
    country
) values (
    'Government Sales',
    'Tanquecitos',
    'United States'
);

insert into departments (
    name,
    location,
    country
) values (
    'Security',
    'Sugarloaf',
    'United States'
);

insert into departments (
    name,
    location,
    country
) values (
    'Finance',
    'Dale City',
    'United States'
);

insert into departments (
    name,
    location,
    country
) values (
    'Customer Support',
    'Grosvenor',
    'United States'
);

commit;
-- load data

insert into employees (
    department_id,
    name,
    email,
    cost_center,
    date_hired,
    job
) values (
    1,
    'Gricelda Luebbers',
    'gricelda.luebbers@aaab.com',
    76,
    sysdate - 63,
    'Engineer'
);

insert into employees (
    department_id,
    name,
    email,
    cost_center,
    date_hired,
    job
) values (
    1,
    'Dean Bollich',
    'dean.bollich@aaac.com',
    35,
    sysdate - 93,
    'Security Specialist'
);

insert into employees (
    department_id,
    name,
    email,
    cost_center,
    date_hired,
    job
) values (
    1,
    'Milo Manoni',
    'milo.manoni@aaad.com',
    100,
    sysdate - 90,
    'Marketing Manager'
);

insert into employees (
    department_id,
    name,
    email,
    cost_center,
    date_hired,
    job
) values (
    1,
    'Laurice Karl',
    'laurice.karl@aaae.com',
    43,
    sysdate - 16,
    'Accounting Analyst'
);

insert into employees (
    department_id,
    name,
    email,
    cost_center,
    date_hired,
    job
) values (
    1,
    'August Rupel',
    'august.rupel@aaaf.com',
    66,
    sysdate - 44,
    'President'
);

insert into employees (
    department_id,
    name,
    email,
    cost_center,
    date_hired,
    job
) values (
    1,
    'Salome Guisti',
    'salome.guisti@aaag.com',
    11,
    sysdate - 70,
    'Sales Representative'
);

insert into employees (
    department_id,
    name,
    email,
    cost_center,
    date_hired,
    job
) values (
    1,
    'Lovie Ritacco',
    'lovie.ritacco@aaah.com',
    7,
    sysdate - 18,
    'Sales Representative'
);

insert into employees (
    department_id,
    name,
    email,
    cost_center,
    date_hired,
    job
) values (
    1,
    'Chaya Greczkowski',
    'chaya.greczkowski@aaai.com',
    21,
    sysdate - 33,
    'Data Architect'
);

insert into employees (
    department_id,
    name,
    email,
    cost_center,
    date_hired,
    job
) values (
    1,
    'Twila Coolbeth',
    'twila.coolbeth@aaaj.com',
    9,
    sysdate - 95,
    'Webmaster'
);

insert into employees (
    department_id,
    name,
    email,
    cost_center,
    date_hired,
    job
) values (
    1,
    'Carlotta Achenbach',
    'carlotta.achenbach@aaak.com',
    51,
    sysdate - 82,
    'Security Specialist'
);

insert into employees (
    department_id,
    name,
    email,
    cost_center,
    date_hired,
    job
) values (
    1,
    'Jeraldine Audet',
    'jeraldine.audet@aaal.com',
    54,
    sysdate - 12,
    'Help Desk Specialist'
);

insert into employees (
    department_id,
    name,
    email,
    cost_center,
    date_hired,
    job
) values (
    1,
    'August Arouri',
    'august.arouri@aaam.com',
    32,
    sysdate - 26,
    'Marketing Manager'
);

insert into employees (
    department_id,
    name,
    email,
    cost_center,
    date_hired,
    job
) values (
    1,
    'Ward Stepney',
    'ward.stepney@aaan.com',
    8,
    sysdate - 62,
    'Business Applications'
);

insert into employees (
    department_id,
    name,
    email,
    cost_center,
    date_hired,
    job
) values (
    1,
    'Ayana Barkhurst',
    'ayana.barkhurst@aaao.com',
    25,
    sysdate - 93,
    'Webmaster'
);

commit;

代码中的.id列是什么?

1 个答案:

答案 0 :(得分:0)

DEPARTMENTS表的定义似乎缺少名为ID的字段,该字段应该是数据类型NUMBER,并且应该是DEPARTMENTS表的主键。我基于以下事实:在EMPLOYEES表中有一个DEPARTMENT_ID字段,其类型为NUMBER,这是引用DEPARTMENTS表的外键。如上所示,部门上没有PRIMARY KEY,因此ID列(缺失)应该是主键。

我建议将DEPARTMENTS表定义为:

create table departments (
    ID                             NUMBER
      CONSTRAINT PK_DEPARTMENTS
        PRIMARY KEY
          USING INDEX,
    name                           varchar2(255) not null,
    location                       varchar2(4000),
    country                        varchar2(4000)
);

此外,触发器DEPARTMENTS_BIU应该定义如下:

create or replace trigger departments_biu
    before insert or update 
    on departments
    for each row
begin
    IF :NEW.ID IS NULL THEN
      :NEW.ID := DEPARTMENTS_SEQ.NEXTVAL;
    END IF;
end departments_biu;

这意味着您需要定义DEPARTMENTS_SEQ:

CREATE DEPARTMENTS_SEQ
  MINVALUE 1
  NOMAXVALUE
  START WITH 1
  NOCACHE;

好运。