Question

I am trying to delete these tables from my data base and I keep getting: "mysql>...

I am trying to delete these tables from my data base and I keep getting:

"mysql> DROP TABLE Courses;

ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails"

I am using the command DROP TABLE Courses; Below is my sql file

use sdev300;

// Create a student table
CREATE TABLE Students (
PSUsername varchar(30) primary key,
FirstName varchar(30),
LastName varchar(30),
EMail varchar(60)
);
CREATE TABLE Courses(
CourseID int primary key,
CourseDisc varchar(4),
CourseNum varchar(4),
CourseTitle varchar(75)
);
CREATE TABLE StudentCourses (
StudentCourseID int primary key,
CourseID int,
PSUsername varchar(30),
Constraint SC1 Foreign Key (CourseID) references Courses(CourseID) on delete cascade,
Constraint SC2 Foreign Key (PSUsername) references Students(PSUsername)
On delete cascade
);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Question:

I am trying to delete these tables from my data base and I keep getting:

"mysql> DROP TABLE Courses;

ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails"

I am using the command DROP TABLE Courses; Below is my sql file

use sdev300;

// Create a student table
CREATE TABLE Students (
PSUsername varchar(30) primary key,
FirstName varchar(30),
LastName varchar(30),
EMail varchar(60)
);
CREATE TABLE Courses(
CourseID int primary key,
CourseDisc varchar(4),
CourseNum varchar(4),
CourseTitle varchar(75)
);
CREATE TABLE StudentCourses (
StudentCourseID int primary key,
CourseID int,
PSUsername varchar(30),
Constraint SC1 Foreign Key (CourseID) references Courses(CourseID) on delete cascade,
Constraint SC2 Foreign Key (PSUsername) references Students(PSUsername)
On delete cascade
);

Answer:

As per the problem Statement

SQL Operation to Drop TABLE Courses fails with Error 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails

This Error #1217 happens when other tables has Foreign Table Key Constraints to the Table that you are trying to delete.

In this Problem Statement.

Column CourseID is Primary Key Constraint to Table Courses

Column CourseID is added as a Foreign Key Constraint to Table StudentCourses

Above Foreign Key Constraint caused this Error #1217 .

This Error #1217 can be avoided by using below SQL Query Approach:

Approach 1:

Use 'FOREIGN_KEY_CHECK' variable to 0 First

Then Do DROP Operation for Table Courses

Then Reset the 'FOREIGN_KEY_CHECK' variable to 1

Above Solution can be achieved using below SQL Query :

SQL> SET FOREIGN_KEY_CHECK=0; DROP TABLE Courses; SET FOREIGN_KEY_CHECK=1;

Approach 2:

Use ALTER SQL Command to do Alter Operation in Table Courses to drop Foreign Key Constraint THEN

Removing the Foreign Key Constraint Column in Table Courses THEN Do DROP TABLE Courses;

Above Solution can be achieved using below SQL Query :

SQL> ALTER TABLE Courses DROP FOREIGN KEY CourseID; DROP TABLE Courses;

=======================================

Both Approach will do the Drop Operation, But Approach 1 is Ideal Solution.

Add a comment
Know the answer?
Add Answer to:
I am trying to delete these tables from my data base and I keep getting: "mysql>...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • SQL Query Question: I have a database with the tables below, data has also been aded...

    SQL Query Question: I have a database with the tables below, data has also been aded into these tables, I have 2 tasks: 1. Add a column to a relational table POSITIONS to store information about the total number of skills needed by each advertised position. A name of the column is up to you. Assume that no more than 9 skills are needed for each position. Next, use a single UPDATE statement to set the values in the new...

  • Given the mySQL tables created below...    Create a mySQL solution to return the itemIDs of...

    Given the mySQL tables created below...    Create a mySQL solution to return the itemIDs of items posted by user X, such that all the reviews are “Excellent” or “Good” for these items CREATE TABLE Users (    userId varchar (30) NOT NULL,    pass varchar (30),    fname varchar (50),    lname varchar (50),    email varchar (50),    gender char(1),    age integer,    banned boolean,    PRIMARY KEY (userId),    UNIQUE(email)) CREATE TABLE FavSellers (    userId...

  • /* I am executing following SQL statement for the below code but I am getting error....

    /* I am executing following SQL statement for the below code but I am getting error. Pls, upload screenshot after running it. SQL Statement: SELECT OfferNo, CourseNo FROM Offering WHERE OfferTerm = 'Sum' AND OfferYear = 2012 AND FacSSN IS NULL; */ CREATE TABLE STUDENT( StdSSN INT NOT NULL PRIMARY KEY, StdFName VARCHAR2(50), StdLName VARCHAR2(50), StdCity VARCHAR2(50), StdState VARCHAR2(2), StdZip VARCHAR2(10), StdMajor VARCHAR2(15), StdYear VARCHAR2(20) ); CREATE TABLE FACULTY( FacSSN INTEGER NOT NULL PRIMARY KEY, FacFName VARCHAR(50), FacLName VARCHAR(50), FacCity...

  • This is database system concept. 1.Find the ids of instructors who are also students using a...

    This is database system concept. 1.Find the ids of instructors who are also students using a set operation. Assume that a person is identified by her or his id. So, if the same id appears in both instructor and student, then that person is both an instructor and a student. Remember: set operation means union, intersect or set difference. 2.Find the ids of instructors who are also students using the set membership operator. 3.Find the ids of instructors who are...

  • These question is for mysql, so I want know what is answer that is information for...

    These question is for mysql, so I want know what is answer that is information for source: DROP DATABASE IF EXISTS travel; CREATE DATABASE travel; USE travel; -- -- Table structure for table `equipment` -- DROP TABLE IF EXISTS `equipment`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `equipment` ( `EquipID` int(11) NOT NULL DEFAULT '0', `EquipmentName` varchar(50) NOT NULL DEFAULT '', `EquipmentDescription` varchar(100) NOT NULL DEFAULT '', `EquipmentCapacity` int(11) NOT NULL DEFAULT '0',...

  • Someone Please Help Me modify this in PHP I'm in desperate need I cant figure this out ... Make t...

    Someone Please Help Me modify this in PHP I'm in desperate need I cant figure this out ... Make the following modifications: For the vendors table: Comment out the table-level primary key Change the VendorIDcolumn to be a column-level primary key Add a VendorEmail column to the bottom of the table definition (define the data type for the column as variable character and set it to not exceed 45 characters) After the lineItems table, add code to create a table...

  • Regrettably, the company you work for did not hire a student from this class to design...

    Regrettably, the company you work for did not hire a student from this class to design their database. You are given the following table definitions. You (hopefully) take one look and are amazed at how bad the design decisions are in these two tables alone. Amazingly the database seems to be working OK. Propose a new way to implement these tables, list and explain the changes you make including why the old way was bad and your new change is...

  • Using SQL and the following info Question 3: Find the people who do not have Viper...

    Using SQL and the following info Question 3: Find the people who do not have Viper Certification but are still assigned to Viper class ship Find the fname, lname, and ship_instance_id for all people who do not have Viper certification but are assigned to at least one instance of a Viper class ship (this includes all variants of Viper class ships). Return a row for every ship/person combination. Order your results by fname in ascending order. Use the BSG database...

  • Using SQL and the following info Question 3: Find the people who do not have Viper...

    Using SQL and the following info Question 3: Find the people who do not have Viper Certification but are still assigned to Viper class ship Find the fname, lname, and ship_instance_id for all people who do not have Viper certification but are assigned to at least one instance of a Viper class ship (this includes all variants of Viper class ships). Return a row for every ship/person combination. Order your results by fname in ascending order. Use the BSG database...

  • I NEED TO WRITE THE FOLLOWING QUERIES IN MYSQL (13)Next, grant to a user admin the read privileges on the complete descr...

    I NEED TO WRITE THE FOLLOWING QUERIES IN MYSQL (13)Next, grant to a user admin the read privileges on the complete descriptions of the customers who submitted no orders. For example, these are the customers who registered themselves and submitted no orders so far. The granted privilege cannot be propagated to the other users. 0.3 (14)Next, grant to a user admin the read privileges on information about the total number of orders submitted by each customer. Note, that some customers...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT