How to use DELETE ON CASCADE in this senario? - sql

so I have 3 tables, Applicant, Vacancy and a Link table. I am trying to have the applicant data removed when the vacancy is removed. Here is the current SQL code I have, would this remove the applicant?
CREATE TABLE Applicant(
ID INT PRIMARY KEY,
name varchar(20),
address varchar(20),
VacancyID INT,
FOREIGN KEY (VacancyID) REFERENCES Vacancy(ID) ON DELETE CASCADE);
CREATE TABLE AppVac(
ApplicantID INT PRIMARY KEY,
VacancyID INT PRIMARY KEY,
FOREIGN KEY (ApplicantID) REFERENCES Applicant(ID),
FOREIGN KEY (VacancyID) REFERENCES Vacancy(ID);
CREATE TABLE Vacancy(
ID INT PRIMARY KEY,
.....

No. The Applicant has not relationship to the Vacancy. What gets removed are all corresponding rows in AppVac.
If you want to require that applicants have at least one row in AppVac, then you'll need a delete trigger on the table. Cascading foreign keys won't do that for you. That is, cascading deletes delete from the referring table, not from the reference table.

Related

Referencing 2 foreign keys in Oracle

I've got 3 schemas, Course, Takes, Instructs.
Under course, I have a course_id as primary key which I want to reference the attribute course_id in both takes and instructs. How can I do that?
Currently this is my course schema where I'm only referencing 1 other table
CREATE TABLE Course (
course_id int PRIMARY KEY REFERENCES Takes (course_id),
)
Are you sure you don't have it backwards?
Takes references Course.
Instructs references Course
Would be a more normal arrangement.
You can do ADD CONSTRAINT after table creation if that's truly your model.
As far as I understood, you have the Course table with the primary key course_id. Then, there are tables Takes and Instructs and each of them has to reference the Course table (have course_id as a foreign key). In your query, Course references Takes but it has to be vice versa.
In case none of those tables exist you need to create them as follows:
CREATE TABLE Course (course_id INT PRIMARY KEY);
CREATE TABLE Takes (take_id INT PRIMARY KEY, course_id INT,
FOREIGN KEY (course_id) REFERENCES Course(course_id));
CREATE TABLE Instructs (instruct_id INT PRIMARY KEY, course_id INT,
FOREIGN KEY (course_id) REFERENCES Course(course_id));
In case the tables Takes and Instructs already exist you should modify them with ALTER TABLE:
ALTER TABLE Takes
ADD CONSTRAINT fk_course
FOREIGN KEY (course_id)
REFERENCES Course(course_id);
And the same for the Instructs table.

How to add delete cascade constraint in sql

I have 2 tables.Table A have columns as (aid, name,depart) where aid is primary key.
Table B has (aid1,aid2,aid3,created_by) where aid1 is the primary_key. aid1, aid2 and aid3 all are primary key of Table A
I want to delete a record in Table B i.e aid1 and simultaneously with delete cascade all three records in TABLE A should be deleted. My doubt here is where should I put the delete cascade constraint. I know that in parent child relationship we need to put delete cascade on the child table so that when parent is deleted, child entities are also deleted but in this scenario I dont understand where I should put delete cascade
Cascading a table will be on the child table. You have to set this on your child table when creating or after creating the child table.
E.g
CREATE TABLE schools (
id int auto_increment primary key not null,
schoolname varchar(191) not null
);
CREATE TABLE students(
id int auto_increment primary key not null,
studentname varchar(191) not null,
school_id int FOREIGN KEY REFERENCES students(id) ONDELETE CASCADE
);
or
You can as well alter the table by running this command.
ALTER TABLE childTable
ADD FOREIGN KEY (childTableParentTableColumn) REFERENCES parentTable(parentTableColumn);

Create three tables with the same auto increment primary key and name

Cannot find a good answer for this online. I need to create three tables as an example below, a parent with an auto increment ID that will then link to the two child tables (Subject and Comment) with the same exact ID and cascade back if that parent ID is deleted.
Any ideas on how to solve?
I have googled and am extremely confused as to how to solve this one. I have a decent amount of experience with SQL, but not with creating tables and relationships.
CREATE TABLE Parent
(
ParentID INT NOT NULL IDENTITY PRIMARY KEY,
Email VARCHAR(50) NOT NULL,...
)
CREATE TABLE Subject
(
ParentID INT NOT NULL PRIMARY KEY,
Subject
)
CREATE TABLE Comment
(
ParentID INT NOT NULL PRIMARY KEY,
Comment VARCHAR(100)
)
Use a 1 to 1 relationship with on delete cascade:
CREATE TABLE Parent(
ParentID INT NOT NULL IDENTITY PRIMARY KEY,
Email VARCHAR(50) NOT NULL,...
)
CREATE TABLE Subject(
ParentID INT NOT NULL PRIMARY KEY,
Subject,
CONSTRAINT fk_SubjectParentId FOREIGN KEY (ParentID)
REFERENCES Parent (ParentID) ON DELETE CASCADE
)
CREATE TABLE Comment(
ParentID INT NOT NULL PRIMARY KEY,
Comment VARCHAR(100),
CONSTRAINT fk_CommentParentId FOREIGN KEY (ParentID)
REFERENCES Parent (ParentID) ON DELETE CASCADE
)
This is known as a 1 to 1 relationship, since both ends of the foreign key are unique within their table.
Though I have to agree with Mitch Wheat comment, cascading deletes is something to use with caution. by specifying cascade delete, you are telling the database engine to delete the related records whenever a parent record is deleted. Not having that cascade delete option will simply throw an error if you attempt to delete a record that is referenced by another table. This forces you, as a developer, to think about the side effects of deleting rows from the parent table and basically acts as a "Are you sure you want to delete?" guard against unwanted deletes.

No matching unique or primary key for this column-list in Oracle

I have created PERSON table in Oracle by this SQL syntax:
Create table person
(
p_id int not null,
personName char(5) not null );
Then I am trying to create ORDERS table with the following syntax:
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id)
REFERENCES person(p_id) );
But I am getting the following error .
No matching unique or primary key for this column-list.
What is the problem ? How can I solve this ?
Add primary key to person table:
CREATE TABLE person(
p_id int not null,
personName char(5) not null,
PRIMARY KEY (p_ID)
);
SqlFiddleDemo
Foreign keys enforce a one-to-many relationship. That is, however many records there are in the dependent table they can only reference a single record in the parent table. This means the referenced column(s) in the parent table must be constrained by a PRIMARY or UNIQUE key.
The error message is telling you that there is no such constraint on person(p_id). And lo! if we compare the two DDL statements you have posted we can see that you have created a primary key for ORDERS but not for PERSON.
The solution is simple: constrain P_ID by adding a primary key to PERSON. You can either drop and re-create the table, or you can use an alter table statement to add a primary key.
You should add primary key to person table.
try this:
ALTER TABLE Person
ADD CONSTRAINT p_id PRIMARY KEY (p_id);

On delete actions when a table has a 2 foreign key from 2 different tables

I have a table let's say table3 and it contains two foreign keys, each one referencing a different table. I want to learn that, could I define two different ON DELETE action for them.
Let me explain it via an example.
create table table3 (
ID varchar(255),
Name varchar(255),
primary key(ID,Name),
foreign key(ID) References user(id),
foreign key(Name) References shops(StoreName)
on update cascade
on delete cascade // I want to cascade table if id is deleted
on delete no actions); // and do not allowed if StoreName is deleted.
Is there anyone to help me ? Thanks in advance.
Not sure if I totally understand what you're trying to do - but if you want to have ON DELETE CASCADE on the fk reference to the User table, and ON DELETE NO ACTIONS on the fk reference to the Shops table, you need to use this T-SQL:
create table table3 (
ID varchar(255),
Name varchar(255),
primary key(ID,Name),
foreign key(ID) References user(id)
on delete cascade, // I want to cascade table if id is deleted
foreign key(Name) References shops(StoreName)
on update cascade
on delete no actions); // and do not allowed if StoreName is deleted.
You need to specify the ON DELETE .... action right with your foreign key definition