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
);
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.
I am trying to delete these tables from my data base and I keep getting: "mysql>...
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...
/* 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 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 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',...
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 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 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...
Having trouble creating SQL data base. Need help trying to correct the following data so that it will run... CREATE TABLE CUSTOMER_T (Customer_ID VARCHAR2(10), CUSTOMER_NAME VARCHAR2(50), CUSTOMER_STREET VARCHAR2(50), CUSTOMER_CITY VARCHAR2(25), CUSTOMER_STATE VARCHAR2(2), CUSTOMER_POSTAL_CODE VARCHAR2(9), CUSTOMER_TYPE VARCHAR2(20), LOCATION_ID VARCHAR2(15), CONSTRAINT CUSTOMER_PK PRIMARY KEY (CUSTOMER_ID), CONSTRAINT CUSTOMER_FK FOREIGN KEY(LOCATION_ID) REFERENCES LOCATION_T (LOCATION_ID)); CREATE TABLE LOCATION_T (LOCATION_ID VARCHAR2(15), LOCATION_NAME VARCHAR2(30), LOCATION_COUNTRY VARCHAR2(30), CONSTRAINT LOCATION_PK PRIMARY KEY (LOCATION_ID), CONSTRAINT LOCATION_FK FOREIGN KEY(RATE_ID) REFERENCES LOCATION(LOCATION_ID)); CREATE TABLE RATE_T (RATE_ID VARCHAR2(15), RATE_CLASS VARCHAR2(20), RATE_PER_kwh NUMBER(10,2), CONSTRAINT...
Use the SQL statements provided to create your tables. Write one
SQL statement for each of the following problems. You can only use
the conditions listed in the tasks. E.g., in task 1, you cannot
manually look up pid of Information Systems undergraduate
program.
Here is the given code:
drop table textbook_schedule cascade constraints;
drop table textbook_author cascade constraints;
drop table schedule cascade constraints;
drop table course cascade constraints;
drop table textbook cascade constraints;
drop table author cascade constraints;
drop...
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...