MySQL Stored Procedure [Copy table 1 -> table 2] - php

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.

Related

How do i make my database auto-generate unique Certificate number that will also be auto-incremented for example 00001

I am creating a database that will store 'graduate details'. I have been able to set the ID to auto increment, Unique and also my primary key that start from 1...
I also want to make the certificate number field unique and auto-increment value in an increasing order that will start from 00001.
Please any help on how to go about this will be very appreciated. Thank you.
This is the MYSQL code for the database below;
CREATE TABLE IF NOT EXISTS `graduates` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`f_name` varchar(25) NOT NULL,
`l_name` varchar(25) NOT NULL,
`gender` varchar(6) DEFAULT NULL,
`address` varchar(100) DEFAULT NULL,
`city` varchar(15) DEFAULT NULL,
`region` varchar(30) DEFAULT NULL,
`phone` varchar(15) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`certificate_number` int(50) NOT NULL AUTO_INCREMENT,
`programme` varchar(50) CHARACTER SET utf8mb4 NOT NULL,
`marks` int(10) NOT NULL,
`college_name` varchar(50) CHARACTER SET utf8mb4 NOT NULL,
`date_of_birth` date DEFAULT NULL,
`created_by` int(10) UNSIGNED NOT NULL DEFAULT 0,
`created_at` date DEFAULT NULL,
`updated_by` int(10) UNSIGNED NOT NULL DEFAULT 0,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `certificate_number` (`certificate_number`)
) ENGINE=InnoDB AUTO_INCREMENT=90 DEFAULT CHARSET=utf8;

One Table is not being created other is being created

I am working on creating table in to my database with mysql and here is my code:
mysqli_query($link, "CREATE TABLE IF NOT EXISTS `review` (
`clients_id` int(15) NOT NULL AUTO_INCREMENT,
`client_id` varchar(150) NOT NULL DEFAULT '',
`rating` tinyint(2) NOT NULL DEFAULT '0',
`proj_date` date NOT NULL DEFAULT '0000-00-00',
`proj_desc` text NOT NULL DEFAULT '',
`photoname` text NOT NULL,
`companyname` text NOT NULL,
`feedback` text NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '0',
`emailid` varchar(100) NOT NULL DEFAULT '',
`customratings` varchar(100) NOT NULL DEFAULT '',
`photo_option` varchar(100) NOT NULL DEFAULT '',
`title` varchar(100) NOT NULL DEFAULT '',
`citation` varchar(100) NOT NULL DEFAULT '',
`date_option` varchar(100) NOT NULL DEFAULT '',
`rating_option` varchar(100) NOT NULL DEFAULT '',
PRIMARY KEY (`clients_id`),
FULLTEXT KEY `feedback` (`feedback`)
) ENGINE=MyISAM AUTO_INCREMENT=1") or mysqli_error($link);
But this is not reflecting into my database ? Why were I may be going wrong ?
but I tried creating other table with the following code
mysqli_query($link, "CREATE TABLE IF NOT EXISTS `setting` (
`id` int(11) NOT NULL auto_increment,
`script_url` text NOT NULL,
`date` varchar(4) NOT NULL,
`rateing` varchar(4) NOT NULL,
`photo` varchar(4) NOT NULL,
`dateformat` varchar(4) NOT NULL,
`page_limit` int(4) NOT NULL,
`proj_desc` varchar(4) NOT NULL,
`companyname` varchar(4) NOT NULL,
`text_color` varchar(255) NOT NULL,
`citation_color` varchar(255) NOT NULL,
`bg_color` varchar(255) NOT NULL,
`border_color` varchar(255) NOT NULL,
`ratingsformat` varchar(250) NOT NULL,
`rating` varchar(250) NOT NULL,
`customratings` varchar(250) NOT NULL,
`speed` varchar(250) NOT NULL,
`pagination` varchar(250) NOT NULL,
`version` varchar(250) NOT NULL,
`global_option` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0;
") or mysqli_error($link);
it is being created correctly and both the tables are in the same file
No you cannot create table using an insert statement.
There are four types of SQL statements:
DML (DATA MANIPULATION LANGUAGE)
DDL (DATA DEFINITION LANGUAGE)
DCL (DATA CONTROL LANGUAGE)
TCL (TRANSACTION CONTROL LANGUAGE)
DML is your SELECT, INSERT, UPDATE and DELETE statements...
DDL is you your CREATE, ALTER and DROP statements.
See more info about types of SQL statements
In order to insert data in your table, first you need to create it.
How to create sql table from php
No. "INSERT INTO" used to insert the data to existing table only.You should create the table with respective column before try to insert the values.
Or you can check the table exist or not before going insert."If exists" u can insert the value else just create and insert the values.
This worked for me but I don't know how?
I just removed DEFAULT '' present while creating review table and table just got created
Here is edited code
mysqli_query($link, "CREATE TABLE IF NOT EXISTS `review` (
`clients_id` int(15) NOT NULL AUTO_INCREMENT,
`client_id` varchar(150) NOT NULL,
`rating` tinyint(2) NOT NULL,
`proj_date` date NOT NULL,
`proj_desc` text NOT NULL,
`photoname` text NOT NULL,
`companyname` text NOT NULL,
`feedback` text NOT NULL,
`status` tinyint(1) NOT NULL,
`emailid` varchar(100) NOT NULL,
`customratings` varchar(100) NOT NULL,
`photo_option` varchar(100) NOT NULL,
`title` varchar(100) NOT NULL,
`citation` varchar(100) NOT NULL,
`date_option` varchar(100) NOT NULL,
`rating_option` varchar(100) NOT NULL,
PRIMARY KEY (`clients_id`),
FULLTEXT KEY `feedback` (`feedback`)
) ENGINE=MyISAM AUTO_INCREMENT=1") or mysqli_error($link);

Error While Creating Table In MySQL

CREATE DATABASE vehicle_system;
USE vehicle_system;
USE vehicle_system; CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) DEFAULT NULL,
`email` varchar(255) NOT NULL,
`password_hash` text NOT NULL,
`api_key` varchar(32) NOT NULL,
`phone` varchar(14) NOT NULL,
`status` int(1) NOT NULL DEFAULT '1',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `phone` (`phone`)
);
CREATE TABLE IF NOT EXISTS `vehicle_model` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) DEFAULT NULL,
`manufacturer_id` int(11) NOT NULL,
`manufacturer_name` varchar(250) NOT NULL,
`model` varchar(32) NOT NULL,
`model_no` varchar(32) NOT NULL,
`type` varchar(32) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `manufacturer_id` (`manufacturer_id`)
);
CREATE TABLE IF NOT EXISTS `user_vehicles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) DEFAULT NULL,
`user_id` int(11) NOT NULL,
`manufacturer_id` int(11) NOT NULL,
`vehicle_no` varchar(32) NOT NULL,
`rc_no` text,
`engine_type` varchar(32),
`year` int(4),
`insurance_exp_date` timestamp,
`pollution_exp_date` timestamp,
`rc_renew_date` timestamp,
`insurance_company` varchar(32),
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `vehicle_no` (`vehicle_no`),
KEY `user_id` (`user_id`),
KEY `manufacturer_id` (`manufacturer_id`)
);
While executing above query I get
#1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
But I do not see two default time stamp in the query, Can any one point out what could be the possible error. Is it because current time stamp is present in the users table ? If so how to get rid of this ?
I think the problem is that you do not have a default for all the fields of type timestamp.
Running your query I got the following error:
Error Code: 1067. Invalid default value for 'updated_at'
Then I got:
Error Code: 1067. Invalid default value for 'pollution_exp_date'
Everything works fine when all the timestamp field have a default value:
CREATE DATABASE vehicle_system;
USE vehicle_system;
USE vehicle_system; CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) DEFAULT NULL,
`email` varchar(255) NOT NULL,
`password_hash` text NOT NULL,
`api_key` varchar(32) NOT NULL,
`phone` varchar(14) NOT NULL,
`status` int(1) NOT NULL DEFAULT '1',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `phone` (`phone`)
);
CREATE TABLE IF NOT EXISTS `vehicle_model` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) DEFAULT NULL,
`manufacturer_id` int(11) NOT NULL,
`manufacturer_name` varchar(250) NOT NULL,
`model` varchar(32) NOT NULL,
`model_no` varchar(32) NOT NULL,
`type` varchar(32) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `manufacturer_id` (`manufacturer_id`)
);
CREATE TABLE IF NOT EXISTS `user_vehicles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) DEFAULT NULL,
`user_id` int(11) NOT NULL,
`manufacturer_id` int(11) NOT NULL,
`vehicle_no` varchar(32) NOT NULL,
`rc_no` text,
`engine_type` varchar(32),
`year` int(4),
`insurance_exp_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`pollution_exp_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`rc_renew_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`insurance_company` varchar(32),
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `vehicle_no` (`vehicle_no`),
KEY `user_id` (`user_id`),
KEY `manufacturer_id` (`manufacturer_id`)
);
This is on MySql 5.7.10.
There is an interesting discussion on this here:
https://bugs.mysql.com/bug.php?id=41137
Creation of timestamp fields is different from any other!
Practically up to 5.6.6 you do not have to declare NOT NULL as by definition that field is NOT NULL and with neither DEFAULT nor ON UPDATE clauses, it is the same as DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.
After 5.6.6 you have to explicit defaults. Look also:
https://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html
and
http://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html
Finally, in your case removing NOT NULL is the best approach.
Regards

Issue with execution query of update table in stored trigger

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;

Confusing mysql problem

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

Categories