ERROR 1215: Cannot add foreign key constraint when using ON DELETE SET NULL - mysql

I'm trying to create the following tables in MySQL. The first one:
CREATE TABLE IF NOT EXISTS address(
address_id INT NOT NULL AUTO_INCREMENT,
address_region VARCHAR(10) NOT NULL,
address_country VARCHAR(20) NOT NULL,
address_city VARCHAR(30) NOT NULL,
PRIMARY KEY(address_id))ENGINE = InnoDB;
I created it successfully,but when I try to create another table as following
CREATE TABLE IF NOT EXISTS spot(
spot_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
spot_address INT(11) NOT NULL,
spot_name VARCHAR(50) NOT NULL,
spot_desc VARCHAR(500) DEFAULT ' ',
spot_speadd VARCHAR(100) NOT NULL,
spot_viewtime INT DEFAULT 0,
FOREIGN KEY(spot_address)
REFERENCES address(address_id)
ON DELETE SET NULL
ON UPDATE SET NULL);
I get an error:
ERROR 1215 (HY000): Cannot add foreign key constraint
Why is this create table statement failing?

You have NOT NULL constraint on spot_address in the spot table and then try to set it to NULL on delete or update in the parent table address.

You can try this:
CREATE TABLE IF NOT EXISTS address
(
address_id INT NOT NULL AUTO_INCREMENT,
address_region VARCHAR(10) NOT NULL,
address_country VARCHAR(20) NOT NULL,
address_city VARCHAR(30) NOT NULL,
PRIMARY KEY(address_id)
)ENGINE = INNODB;
CREATE TABLE IF NOT EXISTS `spot`
(
`spot_id` INT(11) NOT NULL AUTO_INCREMENT,
`spot_address` INT(11) NOT NULL,
`spot_name` VARCHAR(50) NOT NULL,
`spot_desc` VARCHAR(500) DEFAULT ' ',
`spot_speadd` VARCHAR(100) NOT NULL,
`spot_viewtime` INT(11) DEFAULT '0',
PRIMARY KEY (`spot_id`),
KEY `spot_address` (`spot_address`),
CONSTRAINT `spot_ibfk_1` FOREIGN KEY (`spot_address`) REFERENCES `address` (`address_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8

Related

How to convert MySQL query to equivalent Oracle sql Query

I want to convert below MY SQL query to Oracle SQL query, could someone please help ?
Below instructor table is parent entity, and instructor_detail table child entity.
CREATE TABLE `instructor_detail` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`youtube_channel` varchar(128) DEFAULT NULL,
`hobby` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
CREATE TABLE `instructor` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(45) DEFAULT NULL,
`last_name` varchar(45) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
`instructor_detail_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK_DETAIL_idx` (`instructor_detail_id`),
CONSTRAINT `FK_DETAIL` FOREIGN KEY (`instructor_detail_id`) REFERENCES `instructor_detail` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
I have tried below but not working
CREATE TABLE instructor (
id numeric(11) NOT NULL PRIMARY KEY,
first_name varchar(45) DEFAULT NULL,
last_name varchar(45) DEFAULT NULL,
email varchar(45) DEFAULT NULL,
instructor_detail_id numeric(10) not null
);
CREATE TABLE instructor_detail (
id NUMERIC(11) NOT NULL PRIMARY KEY,
youtube_channel varchar(128) DEFAULT NULL,
hobby varchar(45) DEFAULT NULL,
CONSTRAINT fk_instructor
FOREIGN KEY (instructor_detail_id)
REFERENCES instructor(instructor_detail_id)
);
error : Error report -
ORA-00904: "INSTRUCTOR_DETAIL_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Appreciated
Oracle 11 doesn't support auto-generated primary keys, so you need a trigger for that. And there are some different rules on cascading constraints.
But otherwise:
CREATE TABLE instructor_detail (
id int PRIMARY KEY,
youtube_channel varchar2(128) DEFAULT NULL,
hobby varchar2(45) DEFAULT NULL
) ;
CREATE TABLE instructor (
id int NOT NULL PRIMARY KEY,
first_name varchar2(45) DEFAULT NULL,
last_name varchar2(45) DEFAULT NULL,
email varchar2(45) DEFAULT NULL,
instructor_detail_id int,
CONSTRAINT FK_DETAIL FOREIGN KEY (instructor_detail_id) REFERENCES instructor_detail (id)
) ;
CREATE INDEX idx_instructor_instruct_detail_id ON instructor(instructor_detail_id);
Here is a db<>fiddle.

Foreign key constrains fails when inserting in a table where two foreign keys in one table?

I have a table called tbl_vehicles and in this table, there are two foreign keys these are:
ownerID and
agencyID
tbl_agency:
CREATE TABLE `tbl_agency` (
`agencyID` int(11) NOT NULL,
`ag_agencyName` varchar(50) NOT NULL,
`ag_email` varchar(50) NOT NULL,
`ag_username` varchar(50) NOT NULL,
`ag_password` varchar(15) NOT NULL,
`ag_confirmPassword` int(11) NOT NULL,
`ag_phoneNo` int(11) NOT NULL,
`ag_address` varchar(50) NOT NULL,
`ag_photo` varchar(50) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `tbl_agency`
ADD PRIMARY KEY (`agencyID`);
ALTER TABLE `tbl_agency`
MODIFY `agencyID` int(11) NOT NULL AUTO_INCREMENT;
tbl_owner:
CREATE TABLE `tbl_owner` (
`ownerID` int(11) NOT NULL,
`ow_ownerName` varchar(20) NOT NULL,
`ow_nationalID` varchar(50) NOT NULL,
`ow_email` varchar(50) NOT NULL,
`ow_username` varchar(50) NOT NULL,
`ow_password` varchar(15) NOT NULL,
`ow_confirmPassword` int(11) NOT NULL,
`ow_phoneNo` int(11) NOT NULL,
`ow_address` varchar(50) NOT NULL,
`ow_photo` varchar(50) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1
ALTER TABLE `tbl_owner`
ADD PRIMARY KEY (`ownerID`);
ALTER TABLE `tbl_owner`
MODIFY `ownerID` int(11) NOT NULL AUTO_INCREMENT;
tbl_vehicles:
CREATE TABLE `tbl_vehicles` (
`vehiclesID` int(11) NOT NULL,
`ve_name` varchar(100) NOT NULL,
`ve_capacity` varchar(100) NOT NULL,
`ve_fuelType` varchar(100) NOT NULL,
`ve_availability` varchar(100) NOT NULL,
`ve_mileage` varchar(100) NOT NULL,
`ve_color` varchar(100) NOT NULL,
`ve_modelName` varchar(100) NOT NULL,
`ve_brandName` varchar(100) NOT NULL,
`ve_price` varchar(50) NOT NULL,
`ve_licenseCode` int(11) NOT NULL,
`ve_noOfPassenger` int(11) NOT NULL,
`ve_photo` varchar(50) NOT NULL,
`agencyID` int(11) NOT NULL,
`ownerID` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `tbl_vehicles`
ADD PRIMARY KEY (`vehiclesID`),
ADD KEY `agencyID` (`agencyID`),
ADD KEY `ownerID` (`ownerID`);
ALTER TABLE `tbl_vehicles`
MODIFY `vehiclesID` int(11) NOT NULL AUTO_INCREMENT;
--
-- Constraints for table `tbl_vehicles`
--
ALTER TABLE `tbl_vehicles`
ADD CONSTRAINT `fk_tbl_vehicles_tbl_agency` FOREIGN KEY (`agencyID`) REFERENCES `tbl_agency` (`agencyID`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `tbl_vehicles`
ADD CONSTRAINT `fk_tbl_vehicles_tbl_owner` FOREIGN KEY (`ownerID`) REFERENCES `tbl_owner` (`ownerID`) ON DELETE CASCADE ON UPDATE CASCADE;
If a vehicle belongs to an agency then the owner field should be null and If a vehicle belongs to an owner then the agency field should be null.
But it gives me an error.
Which is:
1452 - Cannot add or update a child row: a foreign key constraint fails (db_lamlex_car_rental.tbl_vehicles, CONSTRAINT
tbl_vehicles_ibfk_2 FOREIGN KEY (ownerID) REFERENCES tbl_owner
(ownerID) ON DELETE CASCADE ON UPDATE CASCADE)
How can I solve this?
Please help.

sql #1005 - Can't create table (errno: 150) [duplicate]

I'm trying to create the following tables in MySQL. The first one:
CREATE TABLE IF NOT EXISTS address(
address_id INT NOT NULL AUTO_INCREMENT,
address_region VARCHAR(10) NOT NULL,
address_country VARCHAR(20) NOT NULL,
address_city VARCHAR(30) NOT NULL,
PRIMARY KEY(address_id))ENGINE = InnoDB;
I created it successfully,but when I try to create another table as following
CREATE TABLE IF NOT EXISTS spot(
spot_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
spot_address INT(11) NOT NULL,
spot_name VARCHAR(50) NOT NULL,
spot_desc VARCHAR(500) DEFAULT ' ',
spot_speadd VARCHAR(100) NOT NULL,
spot_viewtime INT DEFAULT 0,
FOREIGN KEY(spot_address)
REFERENCES address(address_id)
ON DELETE SET NULL
ON UPDATE SET NULL);
I get an error:
ERROR 1215 (HY000): Cannot add foreign key constraint
Why is this create table statement failing?
You have NOT NULL constraint on spot_address in the spot table and then try to set it to NULL on delete or update in the parent table address.
You can try this:
CREATE TABLE IF NOT EXISTS address
(
address_id INT NOT NULL AUTO_INCREMENT,
address_region VARCHAR(10) NOT NULL,
address_country VARCHAR(20) NOT NULL,
address_city VARCHAR(30) NOT NULL,
PRIMARY KEY(address_id)
)ENGINE = INNODB;
CREATE TABLE IF NOT EXISTS `spot`
(
`spot_id` INT(11) NOT NULL AUTO_INCREMENT,
`spot_address` INT(11) NOT NULL,
`spot_name` VARCHAR(50) NOT NULL,
`spot_desc` VARCHAR(500) DEFAULT ' ',
`spot_speadd` VARCHAR(100) NOT NULL,
`spot_viewtime` INT(11) DEFAULT '0',
PRIMARY KEY (`spot_id`),
KEY `spot_address` (`spot_address`),
CONSTRAINT `spot_ibfk_1` FOREIGN KEY (`spot_address`) REFERENCES `address` (`address_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8

Creating a MySQL Table but get error

I get
errnno 150: InnoDB Documentation
Supports transactions, row-level locking, and foreign keys
Here is the SQL:
-- Table structure for table `Serving_Info`
--
CREATE TABLE IF NOT EXISTS `Serving_Info` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`Food_Value` varchar(40) NOT NULL,
`Food_name` varchar(40) NOT NULL,
`Served_On` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`Oncall` varchar(128) NOT NULL,
Foreign key(`Oncall`) REFERENCES `employees`(`name`),
Foreign key(`Food_name`) REFERENCES `Foods`(`Food_name`),
PRIMARY KEY (`Oncall`,`Food_name`, `Served_On`)
);
Any idea what is causing the error? I have tried making id part of the primary key but that isn't solving the problem either.
Here are the statements for the tables I am referencing:
CREATE TABLE IF NOT EXISTS `employees` (
`id_user` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL,
`email` varchar(64) NOT NULL,
`phone_number` varchar(16) NOT NULL,
`username` varchar(16) NOT NULL,
`password` varchar(32) NOT NULL,
`confirmcode` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id_user`)
);
--
-- Table structure for table `Foods`
--
CREATE TABLE IF NOT EXISTS `Foods` (
`Food_name` varchar(40) NOT NULL,
`CostPerRefill` double NOT NULL,
PRIMARY KEY (`Food_name`)
);
I just tried the below but I get the same error:
--
-- Table structure for table `Serving_Info`
--
CREATE TABLE IF NOT EXISTS `Serving_Info` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`Food_Value` varchar(40) NOT NULL,
`Food_name` varchar(40) NOT NULL,
`Served_On` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`Oncall` varchar(128),
Foreign key(`Oncall`) REFERENCES `employees`(`name`),
Foreign key(`Food_name`) REFERENCES `Foods`(`Food_name`),
PRIMARY KEY (`id`),
UNIQUE (`Oncall`,`Food_name`, `Served_On`)
);
Your problem is that the reference is to employees.name rather than employees.id_user. Try this:
CREATE TABLE IF NOT EXISTS `Serving_Info` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`Food_Value` varchar(40) NOT NULL,
`Food_name` varchar(40) NOT NULL,
`Served_On` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`Oncall` int,
PRIMARY KEY (`id`),
Foreign key(`Oncall`) REFERENCES `employees`(`id_user`),
Foreign key(`Food_name`) REFERENCES `Foods`(`Food_name`),
UNIQUE (`Oncall`,`Food_name`, `Served_On`)
);
Otherwise, change the employees table so name has an index:
CREATE TABLE IF NOT EXISTS `employees` (
`id_user` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL ,
`email` varchar(64) NOT NULL,
`phone_number` varchar(16) NOT NULL,
`username` varchar(16) NOT NULL,
`password` varchar(32) NOT NULL,
`confirmcode` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id_user`),
KEY (name)
);
In practice, if you are going to use name this way, you should probably make that unique rather than just key.

mysql stored procedure not accepting Foreign key Constraint

I had created a table using StoredProcedure in mysql and i had applied primary key constraint also when i tried to apply foreign key constraint..iam getting error 'Cant create table dbname.tablename'..and here is my Simple StoredProcedure
delimiter |
CREATE PROCEDURE SP_CREATE_TABLE_SAMP()
BEGIN
CREATE TABLE anan_user (
user_id bigint(15) NOT NULL AUTO_INCREMENT,
name varchar(70) DEFAULT NULL,
last_name varchar(70) DEFAULT NULL,
gender varchar(10) DEFAULT NULL,
username varchar(40) DEFAULT NULL,
password varchar(40) DEFAULT NULL,
dateofbirth date DEFAULT NULL,
email varchar(100) DEFAULT NULL,
phone varchar(25) DEFAULT NULL,
mobile varchar(25) DEFAULT NULL,
address varchar(250) DEFAULT NULL,
pincode varchar(15) DEFAULT NULL,
state_id bigint(15) DEFAULT NULL,
district_id bigint(15) DEFAULT NULL,
constituency_id bigint(15) DEFAULT NULL,
profile_Img varchar(100) DEFAULT NULL,
is_pwd_changed varchar(10) DEFAULT 'false',
registered_time timestamp NULL DEFAULT NULL,
updated_date timestamp NULL DEFAULT NULL,
PRIMARY KEY ( user_id )
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
END;
how to give foreign key constraints in stored procedures?
Check your syntax against the MySQL reference on foreign key constraints. It would also be helpful for you to post what you tried, and how you're trying to set the foreign key constraint.
An example constraint definition is:
CREATE TABLE parent (id INT NOT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE child (id INT, parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id) REFERENCES parent(id)
ON DELETE CASCADE
) ENGINE=INNODB;
The relevant syntax is the FOREIGN KEY (parent_id) REFERENCES parent(id).