I'm trying to execute this auto generate query from PHP but it's not working
INSERT INTO mvt (id_piece, article_id_article, code_mvt, origine_mvt, type_mvt, code_art, des_art, qte_old, qte_mvt, qte_new, link_piece)
SELECT
32,
57,
'MVT/15/12/0001',
'BSR/15/12/001',
'S',
'ART_039',
'AAAA',
a.qte_art,
1,
a.qte_art - 1,
'uuuu'
FROM article a
WHERE a.id_article = 57
Structure of table mvt
CREATE TABLE IF NOT EXISTS `mvt` (
`id_mvt` int(11) NOT NULL AUTO_INCREMENT,
`id_piece` int(10) NOT NULL,
`code_mvt` varchar(20) NOT NULL,
`origine_mvt` varchar(100) NOT NULL,
`date_mvt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`type_mvt` varchar(20) NOT NULL,
`article_id_article` int(10) unsigned NOT NULL,
`code_art` varchar(20) NOT NULL,
`des_art` varchar(255) NOT NULL,
`qte_old` double NOT NULL,
`qte_mvt` double NOT NULL,
`qte_new` double NOT NULL,
`link_piece` varchar(255) NOT NULL,
PRIMARY KEY (`id_mvt`),
KEY `article_id_article` (`article_id_article`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=229 ;
Trigger of mvt
DROP TRIGGER IF EXISTS `before_insert_mvt`;
DELIMITER //
CREATE TRIGGER `before_insert_mvt` BEFORE INSERT ON `mvt`
FOR EACH ROW BEGIN
UPDATE article SET qte_art = NEW.qte_new WHERE id_article = NEW.article_id_article;
END
Structure of table article
CREATE TABLE IF NOT EXISTS `article` (
`id_article` int(10) unsigned NOT NULL AUTO_INCREMENT,
`famille_id_famille` int(10) unsigned NOT NULL,
`marque_id_marque` int(10) unsigned NOT NULL,
`tva_id_tva` int(10) unsigned NOT NULL,
`code_art` varchar(20) DEFAULT NULL,
`des_art` varchar(255) DEFAULT NULL,
`paht_art` double DEFAULT NULL,
`pamp_art` double DEFAULT NULL,
`qte_art` double DEFAULT NULL,
`seuil_alert` double NOT NULL,
`seuil_reap` double NOT NULL,
`date_modif` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`etat` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`id_article`),
KEY `article_FKIndex1` (`tva_id_tva`),
KEY `article_FKIndex2` (`marque_id_marque`),
KEY `article_FKIndex3` (`famille_id_famille`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=82 ;
And this is sql error
#1442 - Can't update table 'article' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
it doesn't work!
What am I missing ?
The trigger is trying to update the same table that you're getting the inserted data from, and that's not allowed. You can do it in two steps with a temporary table.
CREATE TEMPORARY TABLE temp_mvt AS
SELECT
32,
57,
'MVT/15/12/0001',
'BSR/15/12/001',
'S',
'ART_039',
'AAAA',
a.qte_art,
1,
a.qte_art - 1,
'uuuu'
FROM article a
WHERE a.id_article = 57;
INSERT INTO mvt SELECT * FROM temp_mvt;
DROP TABLE temp_mvt;
Related
I exported the sql from one phpmyadmin db and imported same at another phpmyadmin db.
The create table statement is as follows
CREATE TABLE IF NOT EXISTS `h_stats` (
`Id` int(11) DEFAULT NULL,
`Category` varchar(50) DEFAULT NULL,
`Success` int(11) DEFAULT NULL,
`OutcomeFailure` int(11) DEFAULT NULL,
`ThCount` int(11) DEFAULT NULL,
`OCount` int(11) DEFAULT NULL,
`HDate` varchar(50) DEFAULT '0',
`Count` int(11) DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The insert sql file also I have exported it as follows
INSERT INTO `h_stats` (`Id`, `Category`, `Success`, `Failure`, `ThCount`, `OCount`, `HDate`, `Count`) VALUES
(13, 'Hits', 31303, 8828, 8893, 30372, '2015-04-07', 40151),
Note :
When I try to insert new rows at the place of id NULL value is coming.
Can anyone suggest me what to change in create table statement.
You need to define the id as primary key and auto-increment .
CREATE TABLE IF NOT EXISTS `h_stats` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`Category` varchar(50) DEFAULT NULL,
`Success` int(11) DEFAULT NULL,
`OutcomeFailure` int(11) DEFAULT NULL,
`ThCount` int(11) DEFAULT NULL,
`OCount` int(11) DEFAULT NULL,
`HDate` varchar(50) DEFAULT '0',
`Count` int(11) DEFAULT '0',
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
So when you add null for id, the auto-increment will come in to play and set it the next value in insert.
Note that this will enforce you to have unique value for id so while doing insert leave id into the insert statement. Something as
insert into h_stats (`Category`,`Success` ...) values ( ....);
I have a table called "tables_data_info" where is stored some of the data relative to different tables. Data like "created time", "editing time", "editing user", "create by user id" etc. I'm using this table because there was a dynamic php script that generate it automatically.
But, when i have a huge number of record ( 15k in this case ) the query getting very very very slow, and take "minutes" to do his job! But i'm not selecting all 15k records, i'm limiting to select 10 records at all!
A simple query:
SELECT pd.id, pd.title, pd.sell_price, pd.available_qt, tdi.createtime, tdi.lastupdatetime, tdi.create_member_id, tdi.create_group_id, tdi.last_update_member_id, tdi.last_update_group_id FROM zd_products AS pd LEFT JOIN zd_tables_data_info AS tdi ON ( tdi.targetid = pd.id and tdi.table_name = 'products' ) ORDER by pd.title ASC LIMIT 0, 10
How can i run this query differently but more efficiently ?
Here the table structure:
zd_products
CREATE TABLE `zd_products` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(256) NOT NULL DEFAULT '',
`internalcode` varchar(256) DEFAULT NULL,
`ean13_jan_code` varchar(256) DEFAULT NULL,
`upc_code` varchar(11) DEFAULT NULL,
`active` tinyint(1) NOT NULL DEFAULT '0',
`status` varchar(12) NOT NULL DEFAULT 'new',
`product_tags` longtext,
`buy_price` double NOT NULL DEFAULT '0',
`sell_price` double NOT NULL DEFAULT '0',
`fiscal_tax_id` int(11) NOT NULL DEFAULT '0',
`box_width` double NOT NULL DEFAULT '0',
`box_height` double NOT NULL DEFAULT '0',
`box_depth` double NOT NULL DEFAULT '0',
`box_weight` double NOT NULL DEFAULT '0',
`shipment_extra_price` double NOT NULL DEFAULT '0',
`available_qt` int(11) NOT NULL DEFAULT '0',
`allow_purchase_out_stock` tinyint(1) NOT NULL DEFAULT '0',
`meta_title` varchar(256) DEFAULT NULL,
`meta_description` varchar(256) DEFAULT NULL,
`meta_keywords` longtext,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15730 DEFAULT CHARSET=utf8;
zd_tables_data_info
CREATE TABLE `zd_tables_data_info` (
`table_name` varchar(256) NOT NULL DEFAULT '',
`targetid` int(11) DEFAULT NULL,
`create_member_id` int(11) unsigned DEFAULT NULL,
`create_group_id` int(11) unsigned DEFAULT NULL,
`last_update_member_id` int(11) unsigned DEFAULT NULL,
`last_update_group_id` int(11) unsigned DEFAULT NULL,
`createtime` int(11) unsigned DEFAULT NULL,
`lastupdatetime` int(11) unsigned DEFAULT NULL,
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
KEY `INDEX` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=19692 DEFAULT CHARSET=utf8;
Your data is not particularly big. Here is the query:
SELECT pd.id, pd.title, pd.sell_price, pd.available_qt,
tdi.createtime, tdi.lastupdatetime, tdi.create_member_id, tdi.create_group_id,
tdi.last_update_member_id, tdi.last_update_group_id
FROM zd_products pd LEFT JOIN
zd_tables_data_info tdi
ON tdi.targetid = pd.id and tdi.table_name = 'products'
ORDER by pd.title ASC
LIMIT 0, 10;
You can improve performance of this query with indexes. The two that come to mind are zd_products(title, id) and zd_tables_data_info(targetid, table_name). Try these and see if they help. You can create these indexes either in the create table statement (or alter table) or by using:
create index zd_products_title_id on zd_products(title, id);
create index zd_tables_data_info_targetid_table_name on zd_tables_data_info(targetid, table_name);
If not, put explain in front of your query and then edit your question with the resulting plan.
My knowledge of MySQL/ SQL apart from the simple CRUD operations is basic.
If I had to use a stored procedure to move certain attributes (not in a specific order) to another table how could it be done?
These are the following tables. I want to move from the 1st to the 2nd table.
As you can see the datatype sizes are different for certain columns.
CREATE TABLE IF NOT EXISTS `source_cdr` (
`callstart` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`src` varchar(80) NOT NULL DEFAULT '',
`dst` varchar(80) NOT NULL DEFAULT '',
`accountcode` varchar(50) NOT NULL,
`uniqueid` varchar(100) NOT NULL,
`ID` int(11) NOT NULL AUTO_INCREMENT,
`callanswer` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`callend` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`disposition` varchar(50) NOT NULL,
`cdr_id` int(11) unsigned NOT NULL DEFAULT '0',
`pin_code` varchar(4) NOT NULL,
`provider` int(11) NOT NULL,
PRIMARY KEY (`ID`),
KEY `calldate_idx` (`callstart`) USING BTREE,
KEY `idx_acc_code_calldate` (`accountcode`,`callstart`) USING BTREE,
KEY `uniqueid` (`uniqueid`),
KEY `cdr_id` (`cdr_id`),
KEY `idx_uniqueid_cdr_id` (`uniqueid`,`cdr_id`)
) ENGINE=MyISAM;
--
CREATE TABLE IF NOT EXISTS `destination_cdr` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`calldate` datetime NOT NULL,
`source` varchar(80) NOT NULL,
`destination` varchar(80) NOT NULL,
`account_code` varchar(30) DEFAULT NULL,
`pincode` varchar(45) NOT NULL,
`duration_call` bigint(20) NOT NULL DEFAULT '0',
`duration_talk` bigint(20) NOT NULL,
`disposition` varchar(255) NOT NULL,
`clid` varchar(80) DEFAULT NULL,
`cdr_id` bigint(20) DEFAULT NULL,
`vxcdr_id` bigint(20) DEFAULT NULL,
`provider` int(11) NOT NULL DEFAULT '0'
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
EDIT 1
An example of a row
('2012-03-18 20:54:49', '5796', '0761100866', '103f0124ad510516f33cab132c0a695b', 'call-F1884808-6753-2F10-181C-3A#10.217.164.33', 308006367, '2012-03-18 20:55:05', '2012-03-18 20:55:51', '200 OK', 2, '', 0),
Thanks
You can use MySQL: INSERT ... SELECT Syntax to copy data from one table to the other.
Decide common fields in both and copy the same.
Example:
INSERT INTO TABLE2( COL1, COLx, ... ) SELECT colM, colY FROM TABLE1;
If the column sizes mismatch, data truncation takes place, and you can't overcome that but redefine the destination table columns.
I have a very strange problem here. I am exporting a query through phpmyadmin and then when I run it using mysql_query I get an error saying that syntax of the query isn't correct.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE IF NOT EXISTS `wp_cfs_events` ( `event_id` int(11) NOT NULL AUTO_' at line 14
The same query runs perfectly fine using phpmyadmin. What could be the problem?
Here is the SQL query
CREATE TABLE IF NOT EXISTS `wp_cfs_certificates` (
`certificate_id` varchar(8) DEFAULT NULL,
`parent_name` varchar(45) NOT NULL,
`address` varchar(45) NOT NULL,
`phone_number` varchar(15) NOT NULL,
`emergency_number` varchar(15) NOT NULL,
`email` varchar(45) NOT NULL,
`certificate_price` enum('40','50') NOT NULL,
`certificate_order_time` datetime DEFAULT NULL,
`is_used` tinyint(1) DEFAULT '0',
UNIQUE KEY `certificate_id_UNIQUE` (`certificate_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `wp_cfs_events` (
`event_id` int(11) NOT NULL AUTO_INCREMENT,
`event_name` varchar(60) DEFAULT NULL,
`event_desc` varchar(2048) DEFAULT NULL,
`event_date` date DEFAULT NULL,
PRIMARY KEY (`event_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
INSERT INTO `wp_cfs_events` (`event_id`, `event_name`, `event_desc`, `event_date`) VALUES
(1, 'Culture Night', 'all about culture', '2013-05-16'),
(2, 'Sports Night', 'all about sports', '2013-05-31'),
(3, 'Random Night', 'randomness overloaded', '2013-06-15'),
(4, 'Winter Fest', 'the awesome winter fest', '2013-11-20'),
(5, 'Archived Event', 'this event has been occured in past', '2013-04-02');
CREATE TABLE IF NOT EXISTS `wp_cfs_parents` (
`parent_id` int(11) NOT NULL,
`parent_name` varchar(60) DEFAULT NULL,
PRIMARY KEY (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `wp_cfs_registrations` (
`registration_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`certificate_id` varchar(8) NOT NULL,
`event_id` varchar(45) DEFAULT NULL,
`parent_name` varchar(45) NOT NULL,
`address` varchar(45) NOT NULL,
`phone_number` varchar(15) NOT NULL,
`emergency_number` varchar(15) NOT NULL,
`email` varchar(45) NOT NULL,
`child_name` varchar(45) DEFAULT NULL,
`child_age` int(11) DEFAULT NULL,
PRIMARY KEY (`registration_id`),
UNIQUE KEY `certificate_id_UNIQUE` (`certificate_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
ALTER TABLE `wp_cfs_registrations`
ADD CONSTRAINT `certificate_id` FOREIGN KEY (`certificate_id`) REFERENCES `wp_cfs_certificates` (`certificate_id`) ON DELETE NO ACTION ON UPDATE CASCADE;
Its because mysql_query() doesn't support multiple queries
Its mentioned in the official PHP documentation
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.
I have got a problem with two tables:
CREATE TABLE IF NOT EXISTS `addresses` (
`adr_id` int(11) NOT NULL auto_increment,
`per_id` int(11) NOT NULL,
`adr_street` varchar(50) NOT NULL,
`adr_houseno` int(11) default NULL,
`adr_housenoadd` varchar(10) default NULL,
`adr_postalcode` varchar(25) NOT NULL,
`adr_city` varchar(20) NOT NULL,
`adr_type` varchar(45) default NULL,
`cnt_id` int(11) NOT NULL,
`adr_date` date default NULL,
`sys-mut-dt` timestamp NULL default NULL,
`sys-mut-user` varchar(20) default NULL,
`sys-mut-id` int(11) NOT NULL default '0',
PRIMARY KEY (`adr_id`),
KEY `per_id` (`per_id`),
KEY `cnt_id` (`cnt_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
--
-- RELATIES VOOR TABEL `addresses`:
-- `cnt_id`
-- `countries` -> `cnt_id`
-- `per_id`
-- `persons` -> `per_id`
--
CREATE TABLE `events` (
`evt_id` int(11) NOT NULL auto_increment,
`evt_name` varchar(50) NOT NULL,
`evt_description` varchar(100) default NULL,
`evt_startdate` date NOT NULL,
`evt_enddate` date default NULL,
`evt_starttime` time default NULL,
`evt_endtime` time default NULL,
`evt_amtpersons` int(11) default NULL,
`sts_id` int(11) NOT NULL,
`adr_id` int(11) default NULL,
`evt_amtPersonsSubs` tinyint(4) NOT NULL default '0',
`evt_photo` varchar(50) default NULL,
`sys-mut-dt` timestamp NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`sys-mut-user` varchar(20) default NULL,
`sys-mut-id` int(11) NOT NULL default '0',
PRIMARY KEY (`evt_id`),
KEY `sts_id` (`sts_id`),
KEY `adr_id` (`adr_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The problem is, when i try to delete a record from address table I get the following error
DELETE
FROM addresses
WHERE per_id = 45
1451 - Cannot delete or update a parent row: a foreign key constraint fails (`site/events`, CONSTRAINT `adr_id` FOREIGN KEY (`adr_id`) REFERENCES `addresses` (`adr_id`) ON DELETE NO ACTION ON UPDATE NO ACTION) ;
The weird thing is that there is no record with the same per_id or adr_id in the events table.
So what's going on over here?
How Can I solve this? ;-)
/* I think it means "How can I solve this?" */
It may sound stupid, but are you absolutely sure there are no child rows?
Could you please run this:
SELECT e.*
FROM addresses a
JOIN events e
ON e.adr_id = a.adr_id
WHERE a.per_id = 45