I'm looking to append a comments table from one WordPress site to another. The users are different. When I import the comments from site B to A, I run into a duplicate key issue; comment_id is already taken.
So how can I resolve this and append the table with a simple .sql file? Would I have to take the user information, generate a new user, check for comments made on site B, pull the content and postID, then go back to site A and recreate the comment for the newly created user!?
What a headache! THanks.
if your only problem is a duplicate key issue, go to the end of your sql file after
ENGINE=MyISAM
and make it
ENGINE=MyISAM AutoIncrement=a nubmer above the last id in the new database
or
Query database A for the last id then add one and use it on a new insert query.
Example 1:
CREATE TABLE IF NOT EXISTS `movies` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`year` int(4) NOT NULL,
`size` varchar(255) NOT NULL,
`added` date NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `title` (`title`,`year`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
The Inserts From My Dump:
INSERT INTO `movies` (`title`, `year`, `size`, `added`) VALUES
('[REC] 2', 0, '716688', '2011-09-23'),
('5 Days of War', 0, '1435406', '2012-01-09'),
('[REC]', 0, '1353420800', '2011-11-06');
See how i didnt include the PRIMARY KEY (id) in my includes, but it will still check against my UNIQUE KEY and see if the title exists. Just a little demo that hopefully helps out. If your table already exists on the new database then just skip to the inserts and dont include the primary key and it will be auto set on a new insert to the next available value.
Related
I got a MySQL database with some tables.
In one of these tables i want to insert by a SQL script some new rows.
Unfortunately i have to insert in two columns an empty string and the two columns are part of an unique key for that table.
So i tried to set UNIQUE_CHECKS before and after the insert, but i'm getting errors because of duplicate entries.
Here is the definition of the table:
CREATE TABLE `Table_A` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`number` varchar(25) DEFAULT NULL,
`changedBy` varchar(150) DEFAULT NULL,
`changeDate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`,`number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And the INSERT statement which causes error:
SET UNIQUE_CHECKS = 0;
INSERT INTO `Table_A`
(`name`, `number`, `changedBy`, `changeDate`)
SELECT DISTINCT '', 'myUser', CURRENT_TIMESTAMP
FROM Table_A
AND id NOT IN
(
SELECT DISTINCT id
FROM Table_A
);
SET UNIQUE_CHECKS = 1;
As You can see, i'm using UNIQUE_CHECKS.
But as i said this doesn't work properly.
Any help or suggestion would be appreciated.
Patrick
Switching off Unique Keys for the insert operation doesn't indicate that it will check uniqueness only for the operations that happen after you switch it on again. It just means that database will not waste time to check the constraint during the time it is switch off but it will check the constraint when you switch it on again.
What it measn is that you nead to ensure that column has unique value in a columns with Unique Keys before you can turn it on. Which you don't do.
If you want to maintain Uniqueness somehow for new records you insert after some point in time you would need to create trigger and manually check the new records against already existing data. The same possibly goes for updates. But I don't recommend it - you should probably redesign data so either the Unique Key is not there or the data is truly unique for all the records there are and will be.
So I have someone working on a csv (generated from my table) where they will be updating some fields by hand. This may happen multiple times. I'd like to take the modified csv and update my existing table.
From my understanding, I will need to create a tmp table and then use that to update the existing table. So I can create the temporary table, but how can I iterate through that table and use it to update the existing table?
My sql querying skills are pretty basic. I think its possible, but I'm not sure where to start.
You don't need a temporary table. Just make sure the CSV file includes the primary key of the table. Then you can use the REPLACE modifier in LOAD DATA INFILE. From the documentation:
If you specify REPLACE, input rows replace existing rows. In other words, rows that have the same value for a primary key or unique index as an existing row.
In the CSV you are generating that gets edited, you must include a unique value that will allow you to match the edited record to the original record. Make sure the user doesn't change that column! Also, make sure you have a unique key on that column.
You can then import the edited data into a table with the same (or at least very similar) structure as the original table.
Once the data is imported, you can use an INSERT ... ON DUPLICATE KEY UPDATE ... statement to update the original table. Here's an example:
Main data table:
DROP TABLE IF EXISTS `my_table`;
CREATE TABLE IF NOT EXISTS `my_table` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Primary Key',
`fld1` VARCHAR(100) NULL,
`fld2` VARCHAR(100) NULL,
`fld3` VARCHAR(100) NULL,
PRIMARY KEY (`id`)
)
ENGINE=MyISAM
AUTO_INCREMENT=1
DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci;
Temporary table for edited CSV import:
DROP TABLE IF EXISTS `import_table`;
CREATE TABLE IF NOT EXISTS `import_table` (
`n_id` INT(10) NOT NULL COMMENT 'Original Primary Key',
`n_fld1` VARCHAR(100) NULL,
`n_fld2` VARCHAR(100) NULL,
`n_fld3` VARCHAR(100) NULL,
PRIMARY KEY (`id`)
)
ENGINE=MyISAM
AUTO_INCREMENT=1
DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci;
Simulated data before export and editing:
INSERT INTO `my_table`
(`fld1`,`fld2`,`fld3`)
VALUES
('John','Doe','Atlanta'),
('Jane','Smith','New York'),
('Bill','Howe','San Antonio'),
('Harry','Fields','Paris');
Simulate the imported, edited records:
INSERT INTO `import_table`
(`n_id`,`n_fld1`,`n_fld2`,`n_fld3`)
VALUES
(1,'John','Doe','Decatur, IL'),
(2,'Jane','Smithsonian','New York, NY'),
(3,'Bill','Bellweather','San Antonio, TX'),
(4,'Harry','Belefonte','Houston, TX');
Merge the imported, edited records bak into the main table:
INSERT INTO `my_table`
(`id`,`fld1`,`fld2`,`fld3`)
SELECT `n_id`,`n_fld1`,`n_fld2`,`n_fld3`
FROM `import_table`
ON DULPICATE KEY UPDATE
`fld1` = `n_fld1`,
`fld2` = `n_fld2`,
`fld3` = `n_fld3`;
I have two MySQL Tables
CREATE TABLE IF NOT EXISTS `orders` (
`order_id` int(5) NOT NULL AUTO_INCREMENT,
`order_address` varchar(255) NOT NULL,
`order_paymentmethod` varchar(50) NOT NULL,
`coupon_code` varchar(50) NOT NULL,
`user_id` int(5) NOT NULL,
PRIMARY KEY (`order_id`),
KEY `fk_orderuser` (`user_id`),
KEY `fk_ordercoupon` (`coupon_code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
CREATE TABLE IF NOT EXISTS `coupons` (
`coupon_code` varchar(50) NOT NULL,
`coupon_discount` int(255) NOT NULL,
PRIMARY KEY (`coupon_code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
When am deleting the coupon code from the coupon table the order record that the coupon is related too is also deleted. And i just want to delete the coupon code without any effect on the table orders
Is their any solution for that please?
Regards
In this case you can solve that problem by using mysql trigger. Create trigger for coupons table
CREATE TRIGGER `coupons_before_delete` AFTER DELETE ON `coupons` FOR EACH ROW BEGIN
-- INSERT INTO add_inout_to_error_log(msg) VALUES(old.coupon_code);
DELETE FROM orders WHERE orders.coupon_code = old.coupon_code;
END
in this code old.coupon_code is current deleted coupon code. You can get access to any field of deleted item
There are three options here
Don't delete the coupon, use another boolean field (eg deleted) with default = true, but set it to false when you want to remove it (it doesn't actually remove it but you can handle deleted coupons using this field). This way you can also track the orders initiated with a coupon that on the way this was deleted.
Remove the not null constraint from coupon_code varchar(50) NOT NULL (in orders table) and add a foreign key constraint to ON DELETE SET NULL. *For orders without a coupon set it to null from the beginning.
Using the known as NULL pattern. Create a dummy coupon (ZERO discount) in your db and instead of deleting coupons, assign this dummy coupon to orders that do not require a real coupon.
*depending on the "tracking" requirements, a combination of the above approaches may be required
I have created the simple table in a MySQL database;
CREATE TABLE `opportunity` (
`customer` varchar(255) NOT NULL DEFAULT '',
`category` varchar(255) NOT NULL DEFAULT '',
`bill_amount` decimal(13,2) NOT NULL,
PRIMARY KEY (`bill_amount`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I used 'bill_amount' as my primary key so I could remove duplicate information when I import data to the table, but I need to also need to separate records by separate categories as well. Here is an example of what I have, what results I am getting, and what I need. If anyone could give me advice on how to properly set up my table so it groups the data as need I would appreciate the help.
I have this problem that occurred today I am left baffled, The issues is that when I insert data in one of my database table called tbl_template_log, it exists but it is not showing in browse mode I am using phpMyAdmin.
But when I click "edit" the data appears correct...
Hopefully my question is understandable if not ask me for additional details.
This is how my data appear in browse Mode:
And The proof that the data in user_id actually exists after running this query "SELECT * FROM tbl_template_log" is here:
tbl_template_log structure:
CREATE TABLE IF NOT EXISTS `tbl_template_log` (
`templog_id` int(6) NOT NULL AUTO_INCREMENT,
`user_id` int(6) DEFAULT NULL,
`temp_id` int(6) DEFAULT NULL,
`savetemp_id` int(6) DEFAULT NULL,
`send_date` datetime NOT NULL,
`send_to` varchar(254) NOT NULL,
`email_send` text NOT NULL,
PRIMARY KEY (`templog_id`),
KEY `tbl_user.user_id` (`user_id`,`temp_id`,`savetemp_id`),
KEY `tbl_template.temp_id` (`temp_id`),
KEY `tbl_saved_template.savetemp_id` (`savetemp_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=85 ;
--
-- Dumping data for table `tbl_template_log`
--
INSERT INTO `tbl_template_log` (`templog_id`, `user_id`, `temp_id`, `savetemp_id`, `send_date`, `send_to`, `email_send`) VALUES
(83, 77, NULL, NULL, '2014-05-20 22:08:25', 'tomasz#onetwotrade.com', '<html blahh blahh>'),
--
-- Constraints for table `tbl_template_log`
--
ALTER TABLE `tbl_template_log`
ADD CONSTRAINT `tbl_template_log_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `tbl_user` (`user_id`),
ADD CONSTRAINT `tbl_template_log_ibfk_2` FOREIGN KEY (`temp_id`) REFERENCES `tbl_template` (`temp_id`),
ADD CONSTRAINT `tbl_template_log_ibfk_3` FOREIGN KEY (`savetemp_id`) REFERENCES `tbl_saved_template` (`savedtemp_id`);
I have found a problem, firstly thank you all who tried to help me, Funny enough the problem was with my Safari browser :o, I cleared my browsing History, Catche & Cookies then everything started to work suddenly
Scroll all the way down in PHP my admin and change the Show 30 row(s) value to something like 100 or 500 or how many you want.
Your value is most probably on a different page.