MySQL CREATE TABLE IF NOT EXISTS in PHPmyadmin import - mysql

I have the following code
CREATE TABLE IF NOT EXISTS `abuses` (
`abuse_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL DEFAULT '0',
`abuser_username` varchar(100) NOT NULL DEFAULT '',
`comment` text NOT NULL,
`reg_date` int(11) NOT NULL DEFAULT '0',
`id` int(11) NOT NULL,
PRIMARY KEY (`abuse_id`),
KEY `reg_date` (`reg_date`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='Table with abuse reports' AUTO_INCREMENT=2 ;
this table already exists in the database, but when i import an sql file with phpmyadmin, the following error occurs
--
-- Dumping data for table `probid_abuses`
--
INSERT INTO `abuses` ( `abuse_id` , `user_id` , `abuser_username` , `comment` , `reg_date` , `auction_id` )
VALUES ( 1, 100020, 'artictundra', 'I placed a bid for it more than an hour ago. It is still active. I thought I was supposed to get an email after 15 minutes.', 1338052850, 108625 ) ;
#1062 - Duplicate entry '1' for key 'PRIMARY'
i thought because it already exists it won't attempt to create it, why is it behaving as such?

On the CREATE TABLE,
The AUTO_INCREMENT of abuse_id is set to 2. MySQL now thinks 1 already exists.
With the INSERT statement you are trying to insert abuse_id with record 1. Please set AUTO_INCREMENT on CREATE_TABLE to 1 and try again.
Otherwise set the abuse_id in the INSERT statement to 'NULL'.
How can i resolve this?

it is because you already defined the 'abuse_id' as auto increment, then there is no need to insert its value. it will be inserted automatically. the error comes because you are inserting 1 many times that is duplication of data. the primary key should be unique. should not be repeated.
the thing you have to do is to change your insertion query as below
INSERT INTO `abuses` ( `user_id` , `abuser_username` , `comment` , `reg_date` , `auction_id` )
VALUES ( 100020, 'artictundra', 'I placed a bid for it more than an hour ago. It is still active. I thought I was supposed to get an email after 15 minutes.', 1338052850, 108625 ) ;

Depending on what you want to accomplish, you might replace INSERT with INSERT IGNORE in your file. This will avoid generating an error for the rows that you are trying to insert and already exist.
See http://dev.mysql.com/doc/refman/5.5/en/insert.html.

If you really want to insert this record, remove the `abuse_id` field and the corresponding value from the INSERTstatement :
INSERT INTO `abuses` ( `user_id` , `abuser_username` , `comment` , `reg_date` , `auction_id` )
VALUES ( 100020, 'artictundra', 'I placed a bid for it more than an hour ago. It is still active. I thought I was supposed to get an email after 15 minutes.', 1338052850, 108625 ) ;

In your case, the first value to insert must be NULL, because it's AUTO_INCREMENT.

Related

Update statement with Replace function results zero "0" value for all rows

I ran the following statement in our test mysql database, it works properly. However, when I ran it in production, I got all rows, including those of CLASS = 'ClassName', updated with XML value set to 0 !
UPDATE table_name c set c.XML= REPLACE (c.XML, '<flag_name>false</flag_name>', '')
WHERE c.CLASS = 'ClassName'
I want to know the reason behind this strange behaviour?
Note: The table structure is as follows:
CREATE TABLE `table_name` (
`ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`CLASS` varchar(250) NOT NULL,
`XML` mediumtext NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=23510 DEFAULT CHARSET=utf8;
The expected behaviour is to remove the targeted tag

problem with mysql update - its change all rows

I have mysql history table that set like this:
CREATE TABLE `history` (
`id` int(11) DEFAULT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`user_id` int(11) DEFAULT '0'
)
I ran this line:
ALTER TABLE history ADD COLUMN removed int(11) DEFAULT '0';
but when i run this:
update history set removed=1 where user_id=1599;
I get all rows changed with random values and and date reset to now!
I think i'v error in table but I dont now what. the set seem fine....
Since the user_id is Default to '0'
and here you're overwriting the values in user_id, it's messing up the table
So just do
Create table history ( user_id int(11));
remove " Default '0' ".

MySQL storing TIMESTAMP with milliseconds

I need to create a database to store some logs which can occurs once per millisecond.
I've created the following table:
CREATE TABLE `log` (
`DataEvento` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
`CodiceEvento` int(11) NOT NULL,
`IdApplicativo` int(11) NOT NULL,
PRIMARY KEY (`DataEvento`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
And a stored procedure:
CREATE DEFINER=`root`#`localhost` PROCEDURE `ScriviEvento`(IN evt_id INT, IN app_id INT, IN evt_descr TEXT)
BEGIN
DECLARE timestamp_now TIMESTAMP(3) DEFAULT NOW(3);
INSERT INTO log (DataEvento, CodiceEvento, IdApplicativo) VALUES (timestamp_now, evt_id, app_id);
IF (LENGTH(evt_descr) > 0) THEN
INSERT INTO descrizionelog (DataEvento, DescrizioneEvento) VALUES (timestamp_now, evt_descr);
END IF;
END
Inserting manually some entries I get the correct timestamp with milliseconds but if I create a thread
with a Sleep(1) I got duplicate key error, same happens if I press execute button fast in workbench with
CALL(1, 0, '');
Is there a workaround to this (excluding using an auto-increment id), or am I doing something wrong?
You are doing something wrong by assuming that the timestamp is going to be unique for log records. That really doesn't make sense.
I'm not sure why you are opposed to an auto-increment solution. This would be the right approach:
CREATE TABLE `log` (
LogId int auto_increment primary key,
`DataEvento` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
`CodiceEvento` int NOT NULL,
`IdApplicativo` int NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

auto-increment value in update conflicts with internally generated values

I've been getting this error from an insert on duplicate update query in MYSQL randomly every now and then.
Any idea what's going on? I can't seem to reproduce the error consistently it occurs sometimes and then sometimes not.
Here is the query in question:
INSERT INTO friendships (u_id_1,u_id_2,status) VALUES (?,?,'active') ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id);
And the schema describing the table is:
DROP TABLE IF EXISTS `friendships`;
CREATE TABLE `friendships` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`u_id_1` int(11) NOT NULL,
`u_id_2` int(11) NOT NULL,
`status` enum('active','pending','rejected','blocked') DEFAULT 'pending' NOT NULL,
`initiatiator` enum('1','2','system') DEFAULT 'system' NOT NULL,
`terminator` enum('1','2','system') DEFAULT NULL,
`confirm_timestamp` timestamp DEFAULT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY (`u_id_1`,`u_id_2`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Your ON DUPLICATE KEY UPDATE statement isn't helping you at all here.
You are taking the LAST_INSERT_ID, which is the auto inc of the last successfully inserted row, and trying to update the duplicated row with that id. This will always cause a duplicate primary (you're trying to change the id of some row to match the id of the last thing you added)
If your goal is to either
Insert a new row, or
Update an existing row with 'active'
Then
INSERT INTO friendships (u_id_1,u_id_2,status)
VALUES ( ? , ? ,'active')
ON DUPLICATE KEY UPDATE
status = 'active'; -- I changed this
A separate consideration is to check the source for duplicates. I had a simple audit table
INSERT INTO table
field1, field2, ... , field3
ON DUPLICATE KEY UPDATE row_id=row_id;
where field1 is an INDEX but not UNIQUE with row_ID as INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY.
Ran for years, but an unexpected duplicate row triggered this error.
Fixed by de-duping the source.
Possibly a trivial point to many readers here, but it cost me some head-scratching (followed by a facepalm).

MySQL Update not updating any rows

This is a weird issue I'm having, I have a table and try to do a MySQL-Update query, however, PHPMyAdmin keeps saying 0 rows affected.
Table:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`last_login` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=55069 ;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `userid`, `name`, `last_login`) VALUES
(1, 55068, 'temp', '2012-02-02 09:04:50');
Query:
UPDATE `users` SET name='xorinzor' AND last_login=NOW() WHERE userid='55068'
No errors are returned, just nothing is happening, got no clue why that would be.
Regards,
Jorin
Change your update sentence to:
UPDATE `users` SET password='encryptedthingy', name='xorinzor', last_login=NOW()
WHERE userid=55068
Your SQL syntax was wrong. If you want to update multiple fields at once, you should not separate them with and keyword but with ,.
Also, make getting rid of single quotes for '55068' should help, since that column is a number. And '55068' is a string literal.
Make sure this sentence returns a value:
select * from `users` where userid=55068