我如何在oracle中级联更新和删除?

时间:2014-03-10 01:48:08

标签: sql oracle cascade

我正在尝试按照我的书中的示例来级联更新和删除,但问题是它们的语法在oracle DB中不起作用..

我理解级联更新意味着对父级的更新也会更新其子级和删除相同..但我无法弄清楚oracle的语法..我的书中的具体问题是:

7.5 - 为EMPLOYEE表编写CREATE TABLE语句。电子邮件是 required是备用键,Department的默认值是 人力资源。级联更新,但不是从DEPARTMENT到EMPLOYEE的删除。

7.6 - 为PROJECT表编写CREATE TABLE语句。的默认值 MaxHours为100.级联更新,但不是从DEPARTMENT到EMPLOYEE的删除。

7.7 - 为ASSIGNMENT表编写CREATE TABLE语句。仅限级联 从项目到分配的删除;不要级联删除或 从员工到分配的更新。

我终于成功使用此查询在iSQL * Plus中成功创建了这些表:

CREATE TABLE DEPARTMENT (
DepartmentName  char(35) NOT NULL,
BudgetCode      char(30) NOT NULL,
OfficeNumber    char(15) NOT NULL,
Phone           char(12) NOT NULL,

Constraint DepartmentPK PRIMARY KEY(DepartmentName)

);

INSERT INTO DEPARTMENT VALUES (
    'Administration', 'BC-100-10', 'BLDG01-300', '360-285-8100');

CREATE TABLE EMPLOYEE (
EmployeeNumber  int         NOT NULL,
FirstName       char(25)    NOT NULL,
LastName        char(25)    NOT NULL,
Department      char(35)    DEFAULT 'Human Resources' NOT NULL,
Phone           char(12)    NULL,
Email           char(30)    NOT NULL,
DepartmentName_FK char(35) NOT NULL,

Constraint EmployeePK   PRIMARY KEY(EmployeeNumber),
Constraint EmployeeAK1 UNIQUE(Email),
Constraint DepartmentFK FOREIGN KEY (DepartmentName_FK)
    references DEPARTMENT(DepartmentName)
--ON UPDATE CASCADE 
--ON DELETE no ACTION 

);

CREATE TABLE PROJECT (
ProjectID   int         NOT NULL,
Name        char(30)    NOT NULL,
Department1 char(15)    NOT NULL,
MaxHours    int         DEFAULT 100 NOT NULL,
StartDate   DATE        NULL,
EndDate     DATE        NULL,
DepartmentName_FK1 char(30)   NULL,


Constraint  datecheck check (StartDate < EndDate),
Constraint ProjectIDPK   PRIMARY KEY(ProjectID),
Constraint DepartmentFK1 FOREIGN KEY (DepartmentName_FK1)
    references DEPARTMENT(DepartmentName)
-- ON UPDATE CASCADE 
-- ON DELETE no ACTION 
);

CREATE TABLE Assignment(
ProjectID       Number      NOT NULL,
EmployeeNumber  Number      NOT NULL,
HoursWorked     Number      NULL,

Constraint ProjectIDEmpNumPK PRIMARY KEY(ProjectID, EmployeeNumber),

constraint ProjectIDFK FOREIGN KEY(ProjectID)
    references Project(ProjectID),
constraint EmpNumFK FOREIGN KEY(EmployeeNumber)
    references Employee(EmployeeNumber)
--CONSTRAINT UniqueEmployee UNIQUE (EmployeNumber)

--ON DELETE CASCADE 
);

但是如何指定级联删除和更新并指定不??/ / p>

1 个答案:

答案 0 :(得分:0)

你试过这个吗?

CREATE TABLE Assignment(
ProjectID       Number      NOT NULL,
EmployeeNumber  Number      NOT NULL,
HoursWorked     Number      NULL,

Constraint ProjectIDEmpNumPK PRIMARY KEY(ProjectID, EmployeeNumber),

constraint ProjectIDFK FOREIGN KEY(ProjectID)
    references Project(ProjectID)
ON DELETE CASCADE 
ON UPDATE no ACTION ,
constraint EmpNumFK FOREIGN KEY(EmployeeNumber)
    references Employee(EmployeeNumber)
ON DELETE no ACTION 
ON UPDATE no ACTION 
);