Can I update child table without updating parent table - php

see table for example
CREATE TABLE parent (
id serial not null,
CONSTRAINT parent_pkey PRIMARY KEY (id)
);
CREATE TABLE child (
id serial not null,
parent_id serial not null,
username` varchar(90) NOT NULL
CONSTRAINT child_pkey PRIMARY KEY (id),
CONSTRAINT parent_fk FOREIGN KEY (parent_id)
REFERENCES parent (id)
ON DELETE RESTRICT
ON UPDATE RESTRICT
);
is there anyway i can update the username in child table? if yes how can i update it with php code.
if no what is the other option?

UPDATE `child` SET `username` = '$username' WHERE id = '$id'
replace $username and $id with your desired values.

Related

delete in adjacency table

I have an adjacency table with parent and child elements and when I delete my parent element I would like to delete all his child.
My table:
id name parent
1 Name1 null
2 SubName1 1
When I'm trying to delete row with id=1 I would like to delete and id=2
How can I do this?
My table:
CREATE TABLE IF NOT EXISTS `cats` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`parent` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `parent` (`parent`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
Foreign keys could be the solution.
How you create them is dependent on your used Database.
At foreign keys there is a primary key defined which is like a "parent", if the parent gets deleted the childs get deleted to if you define it.

When parent row link to child row

Example: I have category table with the FOREIGN KEY to the same table:
CREATE TABLE `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
`parent_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `categories_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `categories` (`id`)
)
How to constraint the parent_id that it can't be the child parent_id.
Example:
we have a parent's row where parent_id equal child's id:
['1', 'parent_name', '**2**']
Child row:
['2', 'child_name', '**1**']
How to fix this by MySQL?
It already does not happen during insert (base on the defination of the table), you have foreign key on parent_id to id your example is a paradox, but it may happen during update of the table so you need to create a trigger for Update to prevent updating the table such as the example also For this case The CHECK constraint does not work, as I mentioned one way is to use a trigger for before update:
CREATE TRIGGER trigger_categories
BEFORE Update
ON categories FOR EACH ROW
BEGIN
DECLARE msg VARCHAR(255);
IF EXISTS (select * from categories c where c.id=NEW.parent_id and c.parent_id=NEW.id) THEN
set msg = "DIE: you can not make a parent of chield as it's chield...";
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = msg;
// also you can make NEW as NULL for preventing update under mentioned condition
END IF;
END;

Foreign key constraint failure [duplicate]

This question already has answers here:
Why 'foreign key constraint fails' when foreign key exists?
(3 answers)
Closed 8 years ago.
I'm working on a new script which has a foreign key error.
I have the following tables: request, category and request_category.
There are now 6 categories in table category, so that's all good.
My script inserts the data into the request table and after that it tries to add an entry to request_category but this is where it fails.
The code:
$oUser = new User();
$oUser->firstname = $this->oLibrary->Request->post('firstname');
$oUser->initials = $this->oLibrary->Request->post('initials');
$oUser->lastname = $this->oLibrary->Request->post('lastname');
$oUser->zip = $this->oLibrary->Request->post('zip');
$oUser->city = $this->oLibrary->Request->post('city');
$oUser->email = $this->oLibrary->Request->post('email');
$oUser->setStatus(UserStatus::STATUS_PENDING);
$oUser->create();
$oRequest = new ServiceRequest();
$oRequest->title = $this->oLibrary->Request->post('title');
$oRequest->description = $this->oLibrary->Request->post('description');
$oRequest->user_id = $oUser->id;
$oRequest->setStatus(RequestStatus::STATUS_INCOMPLETE);
$oRequest->create();
// above here goes fine
$oRequestCategory = new RequestCategory();
$oRequestCategory->category_id = $this->oLibrary->Request->post('category');
$oRequestCategory->request_id = $oRequest->id;
$oRequestCategory->create();
I don't quite understand why it won't insert. I have checked and both keys the request_table requires are present in the database. When I try to do the insert in Workbench it throws the same error, So I guess it's a flaw in my database design somewhere. Could someone explain to me why I'm getting this foreign key error?
The error message itself:
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails (`ruiljedienst`.`request_category`, CONSTRAINT `fk_request_category_category1` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
SQL Statement:
INSERT INTO `ruiljedienst`.`request_category` (`category_id`, `request_id`) VALUES ('1', '1')
Here's a screenshot of the related database tables in MySQL workbench:
http://prntscr.com/2fwce6
Update:
show create table ruiljedienst.request_category
Results in :
CREATE TABLE `request_category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category_id` int(11) NOT NULL,
`request_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
KEY `fk_request_category_category1_idx` (`category_id`),
KEY `fk_request_category_request1_idx` (`request_id`),
CONSTRAINT `fk_request_category_category1` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_request_category_request1` FOREIGN KEY (`request_id`) REFERENCES `request` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1'
select id from ruiljedienst.category
This gives:
http://prntscr.com/2fwhfl
You have a foreign key constraint on category_id, so you have to add id=1 to the category table before you add the row to request_category.
Make sure that all the referenced tables use ENGINE=InnoDB.

MySQL syntax error - can't create a table

here is my code
CREATE TABLE IF NOT EXISTS items
(
id INT NOT NULL AUTO_INCREMENT,
name varchar(256) ,
description TEXT,
price INT ,
images TEXT,
views INT ,
hidden TEXT,
purchases INT,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS methods
(
method_id INT NOT NULL AUTO_INCREMENT,
method varchar(256),
username varchar(256),
password varchar(256),
PRIMARY KEY (method_id)
);
CREATE TABLE IF NOT EXISTS payments
(
payment_id INT NOT NULL AUTO_INCREMENT,
item_id INT NOT NULL,
method varchar(256),
display INT,
PRIMARY KEY (payment_id) ,
FOREIGN KEY (item_id) REFERENCES items (id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (method) REFERENCES methods (method) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
The first 2 tables gets generated ok, but the third one "payments" gives me error code 150 ! which should be related to FK ?!
any help ?
The primary key in the methods table is method_id not method. And the data type is INT not VARCHAR(256)
You need:
CREATE TABLE IF NOT EXISTS payments
(
payment_id INT NOT NULL AUTO_INCREMENT,
item_id INT NOT NULL,
method_id int, -- this is different
display INT,
PRIMARY KEY (payment_id) ,
FOREIGN KEY (item_id) REFERENCES items (id) ON DELETE CASCADE ON UPDATE CASCADE,
-- and this line is different
FOREIGN KEY (method_id) REFERENCES methods (method_id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Mysql error with converting from myISAM to innodb

hey guys, i'm getting this error.
Error 1452 : Cannot add or update a child row: a foreign key constraint fails (`s2794971db/ProfileInterests`, CONSTRAINT `ProfileInterests_ibfk_2` FOREIGN KEY (`InterestID`) REFERENCES `Interests` (`ID`))
I change my tables from myISAM to innodb...found out I needed to so that delete was easier.
I had issues with it so I deleted the table which I needed to create the relationships with.
Then I made it again
I originally had
create table if not exists Users (
ID int not null auto_increment primary key,
FirstName varchar(40) not null,
LastName varchar(40) not null,
UserName varchar(40) not null,
UserEmail varchar(40) not null,
UserDOB timestamp not null,
UserJoin datetime not null
);
create table if not exists Interests(
ID int not null auto_increment primary key,
Interests varchar(40) not null
);
create table if not exists ProfileInterests (
userID int not null References Users(ID),
InterestID int not null References Interests(ID),
MiddleID int not null auto_increment primary key
);
but then I deleted the last table and made it
create table if not exists ProfileInterests (
userID int not null,
InterestID int not null,
MiddleID int not null auto_increment primary key
);
and then I made userID and InterestID into index's and then I added a relation User-> ID for userID and interests->ID for interestID
the error occurs when i'm trying to input data into via a php form.
Any ideas...really need some help!
Obviously, you're trying to insert a record into ProfileInterests for which there is no matching record in the Interests table. Look at the exact insert query, and check that you're supplying a valid value for every field in the table which is part of a foreign key relationship.

Categories