Can UPDATE statement be used as INSERT statement? - mysql

I'm using MYSQL and my tables are like below. what I need to do is to update speciality_name field in specialities table corresponding to list_of_specialities in clinics table but when I do it like
below
UPDATE clinics c
LEFT
JOIN specialities s
ON s.clinic_id = c.clinic_id
SET s.speciality_name = concat_ws('',c.list_of_specialities,s.speciality_id)
WHERE s.clinic_id = c.clinic_id
my table looks like this
speciality_id speciality_name clinic_id
6 data 16
since there is no speciality_id for other list_of_specialities I think I must insert others but I need it to be automatically inserted and what I really need is that the table to look like this
speciality_id speciality_name clinic_id
6 data 16
7 data2 16
8 data3 16
9 data4 16
10 asdsads 16
CREATE TABLE IF NOT EXISTS `clinics` (
`clinic_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`clinic_name` varchar(100) DEFAULT NULL,
`location` varchar(500) DEFAULT NULL,
`list_of_specialities` json DEFAULT NULL,
PRIMARY KEY (`clinic_id`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `doctors` (
`doctor_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) DEFAULT NULL,
`last_name` varchar(50) DEFAULT NULL,
`age` int(10) UNSIGNED DEFAULT NULL,
`clinic_id` int(10) UNSIGNED NOT NULL,
`speciality_id` int(10) UNSIGNED NOT NULL,
`user_id` int(10) UNSIGNED NOT NULL,
PRIMARY KEY (`doctor_id`),
KEY `Doctor_FKIndex1` (`clinic_id`),
KEY `Doctor_FKIndex2` (`speciality_id`),
KEY `Doctor_FKIndex3` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `specialities` (
`speciality_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`speciality_name` varchar(100) DEFAULT NULL,
`clinic_id` int(10) UNSIGNED NOT NULL,
PRIMARY KEY (`speciality_id`),
KEY `Speciality_FKIndex1` (`clinic_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

problem solved with a little change to speciality_name field. it should be a UNIQUE INDEX and REPLACE statement must be used. here is an example:
REPLACE
INTO specialities
SET speciality_name = 'data1', clinic_id = '16'

Related

Unable to optimize queries - Bad design or Bad query or Both?

CREATE TABLE `questions` (
`question_id` int(5) unsigned NOT NULL AUTO_INCREMENT,
`FK_quiz_id` int(5) unsigned NOT NULL,
`question_identify_text` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`question_id`),
KEY `FK_quiz_id` (`FK_quiz_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `question_multilingual` (
`question_multilingual_id` int(5) unsigned NOT NULL AUTO_INCREMENT,
`FK_question_id` int(5) unsigned NOT NULL,
`FK_language_id` smallint(3) unsigned NOT NULL,
`question` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`question_multilingual_id`),
UNIQUE KEY `FK_question_id` (`FK_question_id`,`FK_language_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `choices` (
`choice_id` int(5) unsigned NOT NULL AUTO_INCREMENT,
`FK_question_id` int(5) unsigned NOT NULL,
`choice_identify_text` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`choice_id`),
KEY `FK_question_id` (`FK_question_id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `choice_multilingual` (
`choice_multilingual_id` int(5) unsigned NOT NULL AUTO_INCREMENT,
`FK_choice_id` int(5) unsigned NOT NULL,
`FK_language_id` smallint(3) unsigned NOT NULL,
`choice` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`choice_multilingual_id`),
UNIQUE KEY `FK_choice_id` (`FK_choice_id`,`FK_language_id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
These are the table structures.
EXPLAIN SELECT * FROM `questions` inner join question_multilingual on question_multilingual.FK_question_id = questions.question_id AND question_multilingual.FK_language_id = 1 INNER JOIN choices ON choices.FK_question_id = questions.question_id INNER JOIN choice_multilingual ON choice_multilingual.FK_choice_id = choices.choice_id AND choice_multilingual.FK_language_id = 1 WHERE FK_quiz_id = 1
1 SIMPLE choice_multilingual NULL ALL FK_choice_id NULL NULL NULL 14 10.00 Using where
1 SIMPLE question_multilingual NULL ref FK_question_id,FK_language_id FK_language_id 2 const 2 100.00 NULL
1 SIMPLE choices NULL eq_ref PRIMARY,FK_question_id PRIMARY 4 quizzes.choice_multilingual.FK_choice_id 1 14.29 Using where
1 SIMPLE questions NULL eq_ref PRIMARY,FK_quiz_id PRIMARY 4 quizzes.question_multilingual.FK_question_id 1 100.00 Using where
I tried to change composite index, single index. Tried to add columns.
The results varies but not getting the optimized one whatever i do.

SQL query; inner join on 4 tables

Is this the most efficient way of joining these 4 tables? Also is it possible to only have some rows of each tables selected? I tried changing * to a name of a column but only the columns from studentlist are allowed.
SELECT c.classID, c.instrument, c.grade, u.ID, u.firstname, u.lastname, u.lastsongplayed, u.title
FROM studentlist s
INNER JOIN classlist c ON s.listID = c.classID
INNER JOIN (
SELECT *
FROM users u
INNER JOIN library l ON u.lastsongplayed = l.fileID
)
u ON s.studentID = u.ID
WHERE teacherID =3
ORDER BY classID
LIMIT 0 , 30
Database structure:
CREATE TABLE IF NOT EXISTS `classlist` (
`classID` int(11) NOT NULL AUTO_INCREMENT,
`teacherID` int(11) NOT NULL,
`instrument` text,
`grade` int(11) DEFAULT NULL,
PRIMARY KEY (`classID`),
KEY `teacherID_2` (`teacherID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=27 ;
CREATE TABLE IF NOT EXISTS `studentlist` (
`listID` int(11) NOT NULL,
`studentID` int(11) NOT NULL,
KEY `teacherID` (`studentID`),
KEY `studentID` (`studentID`),
KEY `listID` (`listID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `users` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(60) NOT NULL,
`password` varchar(60) NOT NULL,
`firstname` text NOT NULL,
`lastname` text NOT NULL,
`sessionID` varchar(60) DEFAULT NULL,
`lastlogin` time DEFAULT NULL,
`registerdate` date NOT NULL,
`isteacher` tinyint(1) DEFAULT NULL,
`isstudent` tinyint(1) DEFAULT NULL,
`iscomposer` tinyint(1) DEFAULT NULL,
`lastsongplayed` int(11) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `ID` (`ID`),
UNIQUE KEY `email` (`email`,`sessionID`),
KEY `ID_2` (`ID`),
KEY `ID_3` (`ID`),
KEY `lastsongplayed` (`lastsongplayed`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=63 ;
CREATE TABLE IF NOT EXISTS `library` (
`fileID` int(11) NOT NULL AUTO_INCREMENT,
`userID` int(11) NOT NULL,
`uploaddate` datetime NOT NULL,
`title` varchar(60) NOT NULL,
`OrigComposer` varchar(60) NOT NULL,
`composer` varchar(60) NOT NULL,
`genre` varchar(60) DEFAULT NULL,
`year` year(4) DEFAULT NULL,
`arrangement` varchar(60) DEFAULT NULL,
PRIMARY KEY (`fileID`),
KEY `userID` (`userID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=77 ;
Is this the most efficient way of joining these 3 tables?
Your JOIN looks correct and you are joining on your keys. So this should be efficient. However, I would encourage you to analyze your query with EXPLAIN to determine additional optimizations.
Is it possible to only have some rows of each tables selected?
Yes. Change * to be the columns from each table you want. I encourage you to explicitly prefix them with the originating table. Depending on the columns you select, this could also make your query more performant.
SELECT studentlist.studentID, users.email FROM ...

create missing rows in left joined tables

I have two tables which share primary keys. I designed poorly, and it turns out that I need to ensure that every record in a1 has a corresponding record in a4
Query:
SELECT a1.id1, a4.id4
FROM `a1`
LEFT JOIN `a4` ON a1.id1 = a4.id4
Results:
a1.id1.............a4.id4
00000001 ......NULL
00000002 ......NULL
00001001 ......00001001
00001002 ......00001002
What is the best way to INSERT rows in a4 with a corresponding key to a1? In the example above, I need to insert the records 00000001 and 00000002 into a4. 00001001 and 00001002 should be left alone because they already exist in both a1 and a4
Database schema:
CREATE TABLE `a1` (
`id1` int(8) unsigned zerofill NOT NULL auto_increment,
`Shrt_Desc` varchar(200) default NULL,
`ptype` int(5) NOT NULL,
`userid` tinyint(5) NOT NULL,
`submit_id` int(11) NOT NULL,
`submit_time` int(11) NOT NULL,
`update_id` int(11) NOT NULL,
`update_time` int(11) NOT NULL,
`pub` tinyint(1) default '1',
`plate` tinyint(1) NOT NULL,
`item` varchar(11) default NULL,
PRIMARY KEY (`id1`),
KEY `fb_groupbyorder_Shrt_Desc_INDEX` (`Shrt_Desc`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=124106 ;
CREATE TABLE `a4` (
`id4` int(8) unsigned zerofill NOT NULL,
`Water` decimal(10,2) default NULL,
PRIMARY KEY (`id4`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO a4 (id4)
SELECT a1.id1
FROM `a1`
LEFT JOIN `a4` ON a1.id1 = a4.id4
WHERE a4.id4 IS NULL
http://sqlfiddle.com/#!2/91dc1/8

phpmyadmin Ondelete cascade not working

I have Three tables 1) product, 2)product_x, 3) product_y. I have set primary key for these three tables. The tables are
1) product : id, name, product_type, created_at
2) product_x : id, product_id,description, created_at
3) product_y : id,product_id,description, created_at
The product_id table in the product_x and product_y tables are foreign reference from the table product. If product_type is=1 entry will go to product_x and if product_type=0 entry will go to product_1. SO here my problem is i have set delete on cascade for the foreign key reference for these two tables. But when i delete an entry from product_x or product_y the corresponding id from the product table is not deleted. That means delete on cascade in not working. I need help from you guys, Please help.
Here is my product table.
--
-- Table structure for table `product`
--
CREATE TABLE IF NOT EXISTS `product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`code` varchar(100) NOT NULL,
`description` text NOT NULL,
`product_type` tinyint(4) NOT NULL COMMENT '1=pronova product,2=doctor product',
`ingredients` varchar(200) NOT NULL,
`directions` varchar(200) NOT NULL,
`status` tinyint(1) NOT NULL COMMENT '0=inactive,1=active',
`created_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=67 ;
my product_x table
--
-- Table structure for table product_x
CREATE TABLE IF NOT EXISTS `product_x` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`inventory_id` int(11) NOT NULL,
`doctor_id` int(11) NOT NULL,
`stock` varchar(20) NOT NULL,
`image` varchar(50) NOT NULL,
`small_image` varchar(100) NOT NULL,
`sail_price` float DEFAULT NULL,
`acquire_price` float DEFAULT NULL,
`created_at` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `product_id` (`product`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
and my product_y table is
CREATE TABLE IF NOT EXISTS `product_y` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`inventory_id` int(11) NOT NULL,
`specialization_type` int(11) NOT NULL,
`stock` varchar(20) NOT NULL,
`image` varchar(100) NOT NULL,
`unit_credit_value` int(11) NOT NULL,
`suggested_price` float NOT NULL,
`list_price` float DEFAULT NULL COMMENT 'the price which this product sold if pronova sold this',
`created_at` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `inventory_id` (`product`),
KEY `FK50E07CF68B1B2BCE` (`inventory_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
Check to make sure that you are using InnoDB and not MyISAM, if you really don't have control over it, you could write a trigger as explained below:
How to use delete cascade on MySQL MyISAM storage engine?

What could cause a foreign key to not be able to be dropped even when foreign_key_checks = 0?

I'm attempting to rebuild my database, but I'm unable to get past dropping any foreign keys from tables, even though I also call SET foreign_key_checks = 0; From the MySQL docs, it seems that's all that I should need to do. What else might I need to do?
SET foreign_key_checks=0;
alter table galleries drop foreign key fk_page_gallery ;
alter table photos drop foreign key fk_photo_gallery ;
create table galleries (
id int(11) auto_increment not null ,
page_id int(11) ,
cover_id int null ,
title varchar(1024) ,
slug varchar(1024) not null ,
description text null ,
sort_order int(11) ,
published tinyint(1) default 0,
created varchar(20) ,
modified datetime ,
constraint pk_galleries primary key (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
create table pages (
id int(11) auto_increment not null ,
menu_id int(11) not null ,
title varchar(1024) not null ,
slug varchar(1024) not null ,
body text not null ,
short_description varchar(1024) ,
published tinyint(1) default 0,
created datetime ,
modified datetime ,
constraint pk_pages primary key (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
SET foreign_key_checks=1;
And for reference:
mysql> SHOW CREATE TABLE galleries;
+-----------+-------------------------------------------+
| Table | Create Table |
+-----------+-------------------------------------------+
| galleries | CREATE TABLE `galleries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`page_id` int(11) DEFAULT NULL,
`cover_id` int(11) DEFAULT NULL,
`title` varchar(1024) DEFAULT NULL,
`slug` varchar(1024) NOT NULL,
`description` text,
`sort_order` int(11) DEFAULT NULL,
`published` tinyint(1) DEFAULT '0',
`created` varchar(20) DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_page_gallery` (`page_id`),
KEY `fk_gallery_cover` (`cover_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 |
+-----------+-------------------------------------------+
1 row in set (0.00 sec)
mysql> SHOW CREATE TABLE pages;
+-------+-----------------------------------------------+
| Table | Create Table |
+-------+-----------------------------------------------+
| pages | CREATE TABLE `pages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`menu_id` int(11) NOT NULL,
`title` varchar(1024) NOT NULL,
`slug` varchar(1024) NOT NULL,
`body` text NOT NULL,
`short_description` varchar(1024) DEFAULT NULL,
`published` tinyint(1) DEFAULT '0',
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_pages_menu` (`menu_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------+
The foreign keys in your create table statements are different than those in your drop statements. What's more is that your table definitions don't seem to have foreign keys at all. MySQL may throw a weird error if you try to drop a foreign key that is not there.