create procedure to delete child table data if parent data is deleted - sql

create table a (
a_id integer,
a_name varchar2(10),
PRIMARY KEY (a_id)
)
create table b(
b_id,
b_name varchar2(10),
a_id integer,
PRIMARY KEY (a_id),
FOREIGN KEY (a_id) REFERENCES a(a_id)
)

If the children have FKs linking them to the parent, then you can use DELETE CASCADE on the parent.
CREATE TABLE book
( book_id int not null,
book_name varchar(50) not null,
CONSTRAINT book_pk PRIMARY KEY (book_id)
);
CREATE TABLE bookdetails
( detail_id int not null,
book_id int not null,
CONSTRAINT fk_book
FOREIGN KEY (book_id)
REFERENCES book(book_id)
ON DELETE CASCADE
);

Complete Procedure as you required just change the tables name in procedure
visit https://anzblog.com/2017/04/29/create-procedure-delete-child-table-data-parent-data-deleted/
It will work for you.As per my understanding.

Related

How primary key in one table connect to other table with the same primary key?

How primary key in one table connect to another table with the same primary key?
I am trying to make it like this, which those two primary key in the table of CustomerCreditCard is connect to the table of Customer and table of Credit card]
https://i.stack.imgur.com/lIBUE.png
--3
CREATE TABLE Customer
(
CustomerID INT IDENTITY PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
);
--5
CREATE TABLE CreditCard
(
CreditCardNumber VARCHAR(16) PRIMARY KEY,
CreditCardOwnerName VARCHAR(50) NOT NULL,
);
--6
CREATE TABLE CustomerCreditCard
(
CreditCardNumber VARCHAR(16) NOT NULL,
CustomerID INT IDENTITY NOT NULL,
PRIMARY KEY(CreditCardNumber, CustomerID)
);
--6
CREATE TABLE CustomerCreditCard
(
CreditCardNumber VARCHAR(16) NOT NULL,
CustomerID INT NOT NULL,
CONSTRAINT pk_CustomerCreditCard
PRIMARY KEY(CreditCardNumber,CustomerID ),
CONSTRAINT fk_CustomerCreditCard_CreditCardNumber
FOREIGN KEY(CreditCardNumber)
REFERENCES CreditCard(CreditCardNumber)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT fk_CustomerCreditCard_CustomerID
FOREIGN KEY(CustomerID)
REFERENCES Customer(CustomerID)
ON DELETE CASCADE ON UPDATE CASCADE
);
To solve the problem of this question, add the foreign key to the table that has two primary keys, then reference to other tables that you want to connect with the same primary key.

There are no primary or candidate keys in the referenced table 'tblgender' that match the referencing column list in the foreign key 'giFK'

create database my_data
create table tblperson(
pid int primary key,
pname varchar(15),
pgender varchar(15)
)
create table tblgender(
gid varchar(15) primary key,
gender varchar(15)
)
Alter table tblperson add constraint giFK
Foreign key (pgender) references tblgender(gid)

Database Create table Primary key not found

I have two tables i'm trying to create with foreign keys.
The statements are below
Book_Copy Table
CREATE TABLE book_copy (
bid NUMBER(15) NOT NULL,
isbn VARCHAR(15) NOT NULL,
firstavaib VARCHAR(9) NOT NULL,
outservice VARCHAR(9) NULL,
CONSTRAINT primary_key PRIMARY KEY ( bid,isbn ),
FOREIGN KEY ( isbn )
REFERENCES book_catalog ( isbn )
);
History table
CREATE TABLE history (
bid NUMBER(15) NOT NULL,
mid NUMBER(10) NOT NULL,
FOREIGN KEY ( mid )
REFERENCES member ( mid ),
datetaken VARCHAR(9) NOT NULL,
datereturn VARCHAR(9) NULL,
FOREIGN KEY ( bid )
REFERENCES book_copy ( bid ),
CONSTRAINT primary_key PRIMARY KEY ( bid, datetaken )
);
Now when I run it the first one says table created but i get the following for the second.
CREATE TABLE history (
bid NUMBER(15) NOT NULL,
mid NUMBER(10) NOT NULL,
FOREIGN KEY ( mid )
REFERENCES member ( mid ),
datetaken VARCHAR(9) NOT NULL,
datereturn VARCHAR(9) NULL,
FOREIGN KEY ( bid )
REFERENCES book_copy ( bid ),
CONSTRAINT primary_key PRIMARY KEY ( datetaken )
)
*
ERROR at line 1:
ORA-02270: no matching unique or primary key for this column-list
There are several errors in the statements above and also not all the relevant information was made available.
Your table book_copy has a foreign key reference to the table book_catalog via the isbn column:
CREATE TABLE book_copy (
...
FOREIGN KEY ( isbn )
REFERENCES book_catalog ( isbn )
To proceed, I took the liberty to add a simple one:
CREATE TABLE book_catalog (
isbn VARCHAR(15) NOT NULL,
CONSTRAINT pk_ct PRIMARY KEY (isbn));
And similar, they history table also references another table member via the mid column:
CREATE TABLE history (
...
FOREIGN KEY ( mid )
REFERENCES member ( mid ),
I also create that one in order to proceed:
CREATE TABLE MEMBER (
mid NUMBER(10) NOT NULL,
CONSTRAINT pk_mem PRIMARY KEY (mid));
The error that you get above means what it says: There is no such primary key or unique key on the other table that you try to reference, hence a value cannot be uniquely identified and hence the integrity of the data not verified. So the database stops you from doing so in the first place.
The culprit is that you specify the PRIMARY KEY of your table book_copy to be bid,isbn while your foreign key reference in the history table only references the bid:
CREATE TABLE history (
...
FOREIGN KEY ( bid )
REFERENCES book_copy ( bid ),
So you ask the database to check the integrity on something that is not uniquely identifiable. In order to solve this you have to expand your foreign key of the history table to include the isbn column as well:
So the full DDL looks like this:
CREATE TABLE book_catalog (
isbn VARCHAR(15) NOT NULL,
CONSTRAINT book_catalog_pk PRIMARY KEY (isbn));
CREATE TABLE MEMBER (
mid NUMBER(10) NOT NULL,
CONSTRAINT member_pk PRIMARY KEY (mid));
CREATE TABLE book_copy (
bid NUMBER(15) NOT NULL,
isbn VARCHAR(15) NOT NULL,
firstavaib VARCHAR(9) NOT NULL,
outservice VARCHAR(9) NULL,
CONSTRAINT book_copy_pk PRIMARY KEY ( bid,isbn ),
FOREIGN KEY ( isbn )
REFERENCES book_catalog ( isbn )
);
CREATE TABLE history (
bid NUMBER(15) NOT NULL,
mid NUMBER(10) NOT NULL,
isbn VARCHAR2(15) NOT NULL,
FOREIGN KEY ( mid )
REFERENCES member ( mid ),
datetaken VARCHAR(9) NOT NULL,
datereturn VARCHAR(9) NULL,
FOREIGN KEY ( bid, isbn )
REFERENCES book_copy ( bid, isbn ),
CONSTRAINT history_pk PRIMARY KEY ( bid, datetaken )
);
There is another problem in your DDL above. In Oracle, constraint names are globally unique! That means that whatever you put after CONSTRAINT has to be unique. In the case above you use the same name primary_key for both your tables which will fail with an ORA-02264: name already used by an existing constraint error on creating the history table. You will see that I have given the primary key constraints on the tables more meaningful names.
Err: Foreign key(BID) references Book_Copy(BID)
The primary key of Book_Copy is (BID, DateTaken) not just (BID)
You need a primary or unique constraint in the target table referenced in a FK of another table.
1) Add DateTaken to you FK definition on HISTORY or replace the FK with a CHECK constraint something like (EXISTS (select * from book_copy c where c.BID = BID)

Foreign Key creation in Oracle [duplicate]

This question already has an answer here:
ORA-00907: missing right parenthesis
(1 answer)
Closed 8 years ago.
I tried to create Foreign key and PRIMARY KEY
My code is ok with Table1
CREATE TABLE Persons
(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
Table2:
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int(10),
P_Id int FOREIGN KEY REFERENCES Persons(P_Id)
);
error :-ORA-00907: missing right parenthesis
Don't know what the error is.
When you declare the FK "inline" you must not specify the foreign key keyword:
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int(10),
P_Id int REFERENCES Persons(P_Id)
);
Btw: you don't even need to list the column name when using an inline definition, P_Id int REFERENCES Persons would be enough.
That will generate a system-named constraint (e.g. SYS_C0066866), so it's generally better to use a format where you can specify the name of the constraint:
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int(10),
P_Id int,
constraint fk_orders_person foreign key (p_id) REFERENCES Persons(P_Id)
);
If you're inlining your foreign key definition in the column's definition, you don't need the foreign key phrase:
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int(10),
P_Id int REFERENCES Persons(P_Id)
);
For MySql
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int(10),
P_Id int,
FOREIGN KEY(P_Id) REFERENCES Persons(P_Id)
);
For SQL
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int(10),
P_Id int FOREIGN KEY REFERENCES Persons(P_Id)
);
Check Manual

How to set 2 different FK from different tables on PK with 2 columns

I have 3 tables:
create table book (book_id int not null primary key,
book_name char (100) unique)
create table author (author_id int not null primary key,
authorname char (100) unique)
create table bookauthor (book_id int,
author_id int,
CONSTRAINT pk_book_id PRIMARY KEY (book_id,author_id))
I want to set
column book.book_id as fk to bookauthor.book_id
column author.author_id as fk to bookauthor.author_id
Please remember that pk in bookauthor is on book_id,author_id.
Change your bookauther table to
create table bookauther (
book_id int ,
auther_id int,
CONSTRAINT pk_book_id PRIMARY KEY (book_id,auther_id),
FOREIGN KEY (book_id) REFERENCES book(book_id),
FOREIGN KEY (auther_id) REFERENCES auther(auther_id)
)
Have a look at FOREIGN KEY Constraints and SQL FOREIGN KEY Constraint