Data not deleted from database - mysqli

I have a main table called "callshopusers"
CREATE TABLE IF NOT EXISTS `callshopusers` (
`id_callshopuser` int(11) NOT NULL AUTO_INCREMENT,
`id_callshop` int(11) NOT NULL,
`id_client` int(11) NOT NULL,
`client_type` int(11) NOT NULL,
PRIMARY KEY (`id_callshopuser`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=14 ;
But when I try to delete data from this table, it doesn't work. Here id_client column is used in another table as a Primary key.
My query for delete is:
$sql="DELETE FROM callshopusers WHERE id_callshopusers=$id";
But it doesn't work.

You have a typo in id_callshopusers, should be id_callshopuser without 's'
$sql="DELETE FROM callshopusers WHERE id_callshopusers=$id";

Related

MySQL Workbench - Cant select FK column. Dropdown not populating

Here are my tables CREATE Statements:
CREATE TABLE `User` (
`UserId` int NOT NULL AUTO_INCREMENT,
`CompanyId` int NOT NULL,
`FirstName` varchar(50) NOT NULL,
`LastName` varchar(50) NOT NULL,
`UserName` varchar(100) NOT NULL,
PRIMARY KEY (`UserId`),
KEY `FK_Company_User_idx` (`CompanyId`),
CONSTRAINT `FK_Company_User` FOREIGN KEY (`CompanyId`) REFERENCES `Company` (`CompanyId`)
) ENGINE=InnoDB AUTO_INCREMENT=256 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE `Notification` (
`NotificationId` int NOT NULL AUTO_INCREMENT,
`Email` varchar(100) DEFAULT NULL,
`Message` text,
`Subject` varchar(100) DEFAULT NULL,
`UserId` int NOT NULL,
PRIMARY KEY (`NotificationId`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
I am trying to create a FK between UserId in Notification and UserId in User but the dropdown for selecting the field is not populating.
Any help is appreciated.

Mysql FK referenced column not show results

I have problem with creating FK. Am checking and checking and i cant see what is wrong.
I have product, sites and product_site_ref tables.
When i try to add FK to table product_site_ref i cant choose referenced column. Check screenshot.
Relation table
CREATE TABLE `product_site_ref` (
`id` bigint(20) NOT NULL,
`product_id` bigint(20) unsigned NOT NULL,
`site_id` bigint(20) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `product_index` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Product table
CREATE TABLE `product` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`code` varchar(125) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`name` varchar(225) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Site table
CREATE TABLE `site` (
`id` bigint(20) NOT NULL,
`name` varchar(245) DEFAULT NULL,
`url` varchar(45) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Note: table proizvod on screenshot is product. I just translate for your goys to better understand. All is the some

postgresql cannot insert data to newly added column

In postgresql I have a table which I need to add a new column. the original table ddl is belowing:
CREATE TABLE survey.survey_response (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
survey_id uuid NOT NULL,
survey_question_id uuid NULL,
user_id varchar(256) NULL,
device_id varchar(256) NULL,
user_country varchar(100) NULL,
client_type varchar(100) NULL,
product_version varchar(100) NULL,
answer text NULL,
response_date timestamptz NOT NULL DEFAULT now(),
survey_category varchar(100) NULL,
tags varchar(250) NULL,
tracking_id uuid NULL,
CONSTRAINT survey_response_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
) ;
Then I alter the table to add a new column:
alter table survey.survey_response add column system_tags varchar(30) ;
But after that I found my instert statement cannot make change to this new column, for all the original columns it works fine:
INSERT INTO survey.survey_response
(id, survey_id, user_id, tags, system_tags)
VALUES(uuid_generate_v4(), uuid_generate_v4(),'1123','dsfsd', 'dsfsd');
select * from survey.survey_response where user_id = '1123';
The "tags" columns contains inserted value, however, system_tags keeps null.
I tested the above scenario in my local postgreSQL 9.6, any ideas about this strange behavior? Thanks a lot
-----------------update----------
I found this survey.survey_response table has been partitioning based on month, So my inserted record will also be displayed in survey.survey_response_y2017m12. but the new system_tags column is also NULL
CREATE TABLE survey.survey_response_y2017m12 (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
survey_id uuid NOT NULL,
survey_question_id uuid NULL,
user_id varchar(256) NULL,
device_id varchar(256) NULL,
user_country varchar(100) NULL,
client_type varchar(100) NULL,
product_version varchar(100) NULL,
answer text NULL,
response_date timestamptz NOT NULL DEFAULT now(),
survey_category varchar(100) NULL,
tags varchar(250) NULL,
tracking_id uuid NULL,
system_tags varchar(30) NULL,
CONSTRAINT survey_response_y2017m12_response_date_check CHECK (((response_date >= '2017-12-01'::date) AND (response_date < '2018-01-01'::date)))
)
INHERITS (survey.survey_response)
WITH (
OIDS=FALSE
) ;
If I run the same scenario in a non-partition table then the insert works fine.
So do I need any special settings for alter table for partition table?
Old thread but you need to drop and create again the RULE to fix the issue.

cannot add foreign key constraint workbench

Executing the following SQL script in the server:
ERROR: Error 1215: Cannot add foreign key constraint
SQL Code:
CREATE TABLE IF NOT EXISTS `telecom`.`jobreq` (
`id` INT NOT NULL,
`jobName` VARCHAR(45) NULL,
`priority` VARCHAR(45) NULL,
`dates` DATE NULL,
`status` VARCHAR(45) NULL,
`user` VARCHAR(45) NULL,
`timestamp` DATE NULL,
`service_id` INT(11) NOT NULL,
`client_id` INT(11) NOT NULL,
PRIMARY KEY (`id`, `service_id`, `client_id`),
INDEX `fk_jobreq_service1_idx` (`service_id` ASC),
INDEX `fk_jobreq_client1_idx` (`client_id` ASC),
CONSTRAINT `fk_jobreq_service1`
FOREIGN KEY (`service_id`)
REFERENCES `telecom`.`service` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_jobreq_client1`
FOREIGN KEY (`client_id`)
REFERENCES `telecom`.`client` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 12 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
I am unable to find out why I am getting this error can someone with a high intelligence please assist me.
So it seems all I had to do was to change the name of the table and it went through im guessing somehow the name was magically somehow still in the system

Multi-table query - get friend updates

SELECT M.msg_id, M.uid_fk, M.message, M.created,
U.fname, U.lname, M.uploads
FROM messages M, users_friends F, users U
WHERE M.uid_fk=F.friendID
and F.userID = '5'
and status='2'
Building a facebook-like wall and want to grab messages(updates) from friends.
The query above returns an empty set, even though I've made sure there are messages from user 5 in the table.
Schema:
CREATE TABLE IF NOT EXISTS `messages` (
`msg_id` int(11) NOT NULL AUTO_INCREMENT,
`message` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
`uid_fk` int(11) DEFAULT NULL,
`ip` varchar(30) DEFAULT NULL,
`created` int(11) DEFAULT '1269249260',
`uploads` varchar(30) DEFAULT NULL,
PRIMARY KEY (`msg_id`),
KEY `uid_fk` (`uid_fk`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=263 ;
CREATE TABLE IF NOT EXISTS `users` (
`fname` varchar(15) NOT NULL,
`lname` varchar(15) NOT NULL,
`userID` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(23) NOT NULL,
`email` varchar(50) NOT NULL,
`password` varchar(32) NOT NULL,
`DOB` date DEFAULT NULL,
`sex` varchar(1) DEFAULT NULL,
`about` text NOT NULL,
`location` varchar(20) DEFAULT NULL,
`last_login` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`user_level` int(11) NOT NULL DEFAULT '0',
`profile_image` varchar(200) NOT NULL,
`profile_image_small` varchar(200) NOT NULL,
PRIMARY KEY (`userID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=19 ;
CREATE TABLE IF NOT EXISTS `users_friends` (
`userID` int(11) NOT NULL,
`friendID` int(11) NOT NULL,
`status` int(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`userID`,`friendID`),
KEY `fk_users_has_friends_users1` (`userID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
You are single quoting your INT values in the WHERE clause → '5' & '2'.
Also, try JOINs.
SELECT M.msg_id,
M.uid_fk,
M.message,
M.created,
U.fname,
U.lname,
M.uploads
FROM messages M
INNER JOIN users_friends F ON F.friendID = M.uid_fk
AND F.userID = 5
AND F.status = 2
INNER JOIN users U ON U.userID = F.friendID;