PL/SQL外键的建立和表数据删除

1。表和外键的建立
--Create the Foreign Key on the PartOrder Table
ALTER TABLE PartOrder
 ADD CONSTRAINT PartOrder_Customer_FK
    FOREIGN KEY (CustomerID)
    REFERENCES Customer(CustomerID);

This code assumes that you have the structure listed below. Be aware that you must have a Primary Key before you can create the Foreign Key.

--Create the Customer Table
CREATE TABLE Customer
 (
  customerid    NUMBER(12) NOT NULL,
  name          VARCHAR2(50),
  address       VARCHAR2(200),
  lastvisit     DATE DEFAULT (sysdate)
 );

--Create the PartOrder Table
CREATE TABLE PartOrder
  (
  PartOrderID   Number(12) NOT NULL,
  CustomerID    Number(12) NOT NULL,
  SKU           Number(8)  
  );

--Create a Primary Key on the Customer Table
ALTER TABLE Customer
 ADD CONSTRAINT Customer_PK
    PRIMARY KEY (CustomerID)
    USING INDEX;
2。删除表中的数据
 
declare
begin
 LOCK TABLE customer in SHARE ROW EXCLUSIVE MODE NOWAIT;
 LOCK TABLE partorder in SHARE ROW EXCLUSIVE MODE NOWAIT;
 delete from partorder where partorderid=99;
 delete from partorder where partorderid=77;
 delete from customer where customerid=1;
end;
/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值