MySQL table incrementing by 10 for some reason - mysql

The id field in a mysql table is incrementing by 10 (11, 21, 31) for some reason. Here is the table definition:
CREATE TABLE `clients` (
`id` int(11) NOT NULL auto_increment,
`first_name` varchar(255) default NULL,
`last_name` varchar(255) default NULL,
) ENGINE=InnoDB AUTO_INCREMENT=52 DEFAULT CHARSET=utf8;
If I do a simple insert statement in SQL the next ID will be 41.

You have auto_increment_increment set to 10, change it back to 1.

Related

mysql insert into select join - copy values from one column to another table, passing through a connecting table

I can't get this to work
CREATE TABLE `oc_tax_class` (
`tax_class_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`description` varchar(255) NOT NULL,
`date_added` datetime NOT NULL,
`date_modified` datetime NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `oc_tax_rate`
--
CREATE TABLE `oc_tax_rate` (
`tax_rate_id` int(11) NOT NULL,
`geo_zone_id` int(11) NOT NULL DEFAULT 0,
`name` varchar(255) NOT NULL,
`rate` decimal(15,4) NOT NULL DEFAULT 0.0000,
`type` char(1) NOT NULL,
`date_added` datetime NOT NULL,
`date_modified` datetime NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `oc_tax_rule`
--
CREATE TABLE `oc_tax_rule` (
`tax_rule_id` int(11) NOT NULL,
`tax_class_id` int(11) NOT NULL,
`tax_rate_id` int(11) NOT NULL,
`based` varchar(10) NOT NULL,
`priority` int(5) NOT NULL DEFAULT 1
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
3 tables. I want oc_tax_class.title = oc_tax_rate.name
I believe, although I'm not sure, that I should
INSERT INTO oc_tax_class(title)
or
UPDATE oc_tax_class SET title = ...
SELECT oc_tax_rate.name, oc_tax_rule.tax_class_id
JOIN oc_tax_rule ON oc_tax_rate.tax_rate_id = oc_tax_rule.tax_rate_id
And then I don't know what to do next.
I need to copy values from one column to another table, passing through a connecting table.
MySQL supports a multi-table UPDATE syntax, but the documentation (https://dev.mysql.com/doc/refman/en/update.html) has pretty sparse examples of it.
In your case, this may work:
UPDATE oc_tax_class
JOIN oc_tax_rule USING (tax_class_id)
JOIN oc_tax_rate USING (tax_rate_id)
SET oc_tax_class.title = oc_tax_rate.name;
I did not test this. I suggest you test it first on a sample of your data, to make sure it works the way you want it to.

Update a variable in a mysql table depending on a time difference

CONTEXT
I would like to update a variable inside a table depending on a time difference.
DATA
I have the following mySQL Table
CREATE TABLE `userTP` (
`id` int(11) NOT NULL,
`profile` int(11) NOT NULL DEFAULT '1',
`username` varchar(50) NOT NULL,
`inprogress` int(11) NOT NULL,
`launchedat` datetime DEFAULT NULL,
`password` varchar(255) NOT NULL,
`npoints` int(11) NOT NULL DEFAULT '100',
`ms` int(11) NOT NULL DEFAULT '5000'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `userTP` (`id`, `profile`, `username`, `inprogress`, `launchedat`, `password`, `npoints`, `ms`) VALUES
(4, 0, 'binome1', 1, '2020-11-10 17:20:07', 'vvvvv', 10, 1000),
(48, 0, 'binome2', 0, '2020-06-11 14:45:07', 'llll', 10, 1000);
ALTER TABLE `userTP`
ADD PRIMARY KEY (`id`);
ALTER TABLE `userTP`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=49;
COMMIT;
WHAT I WOULD LIKE TO DO
If the time difference between launchedat and the current time is larger than 10 minutes, I would like to update the variable inprogress and set it to 2 for user with id 4
WHAT I TRIED
UPDATE userTP SET inprogress = '2' WHERE ROUND(TIME_TO_SEC(timediff(launchedat,NOW()))/60)>10 AND id = 4
This gives no error but does not work .....
Any Idea ?

How do I INSERT into a database with a primary autoincrement key?

So I have a table that is laid out according to:
CREATE TABLE `dealerships` (
`dealership_id` BIGINT(20) UNSIGNED NOT NULL,
`zone` CHAR(200) NULL DEFAULT NULL,
`phone` BIGINT(20) UNSIGNED NOT NULL,
`fax` BIGINT(20) UNSIGNED NOT NULL,
`name` CHAR(200) NULL DEFAULT NULL,
`address1` CHAR(200) NULL DEFAULT NULL,
`address2` CHAR(200) NULL DEFAULT NULL',
`servicephone` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
`timezone` CHAR(20) NULL DEFAULT NULL,
PRIMARY KEY (`dealership_id`)
)
COMMENT='This stores dealership information. '
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
Now when I insert with
INSERT INTO dealerships (dealership_id, ~the rest~), VALUES( NULL, ~values);
This returns an error that dealership_id cannot be NULL error 1048.
INSERT INTO dealerships (dealership_id, ~the rest~), VALUES(0, ~values);
This returns an error that dealership_id already exists for value 0.
INSERT INTO dealerships (dealership_id, ~the rest~), VALUES(default, ~values);
This returns an error that dealership_id has no default value error 1364.
When I simply say screw it, and ommit dealership_id
INSERT INTO dealerships (~the rest~), VALUES(~values);
I get the error that a value must be specified for dealership_id.
Every-time I've worked with autoincrement primary keys. Normally inserting NULL would work for the index to do the auto-magical things for me. What is happening??
Platform Specific Information:
MariaDB10.1.18 x64
Windows Server 2012 R2
For create add AUTO_INCREMENT
CREATE TABLE `dealerships` (
`dealership_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`zone` CHAR(200) NULL DEFAULT NULL,
`phone` BIGINT(20) UNSIGNED NOT NULL,
`fax` BIGINT(20) UNSIGNED NOT NULL,
`name` CHAR(200) NULL DEFAULT NULL,
`address1` CHAR(200) NULL DEFAULT NULL,
`address2` CHAR(200) NULL DEFAULT NULL',
`servicephone` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
`timezone` CHAR(20) NULL DEFAULT NULL,
PRIMARY KEY (`dealership_id`)
)
COMMENT='This stores dealership information. '
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
and for insert don't insert the key (is automatically create by auto increment)
insert into `dealerships` (
`zone`,
`phone`,
`fax` ,
`name`,
`address1`,
`address2`',
`servicephone` ,
`timezone`
) values (
'vale_zone',
1,
2 ,
'val_name',
'val_address1',
'val_address2',
3 ,
'val_timezone'
)
To insert a field with auto increment property, use ID int NOT NULL AUTO_INCREMENT in the create table.
CREATE TABLE `dealerships` (
`dealership_id` INT NOT NULL AUTO_INCREMENT,
`zone` CHAR(200) NULL DEFAULT NULL,
`phone` BIGINT(20) UNSIGNED NOT NULL,
`fax` BIGINT(20) UNSIGNED NOT NULL,
`name` CHAR(200) NULL DEFAULT NULL,
`address1` CHAR(200) NULL DEFAULT NULL,
`address2` CHAR(200) NULL DEFAULT NULL',
`servicephone` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
`timezone` CHAR(20) NULL DEFAULT NULL,
PRIMARY KEY (`dealership_id`)
)
COMMENT='This stores dealership information. '
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
After, you have two solutions to insert data.
Solution 1
INSERT INTO dealerships(zone, phone, fax, name) VALUES('zone1', '00000', '00000', 'name1')
In this case I chossen to add data in only few columns (add the others on the wame way).
Solution 2
INSERT INTO dealerships SET zone = 'zone1', phone = '0000', name = 'Eddy'
Here, I used the field SET field = value, field2 = value2.
As you can see I didn't added any reference to the ID. Normal, the field is AUTO_INCREMENT mode, and Mysql (or tothers SGBD) will create automatically a value.
Of course, you can force the value:
INSERT INTO dealerships SET dealership_id = 10, zone = 'zone1', phone = '0000', name = 'Eddy'
INSERT INTO dealerships(dealership_id, zone, phone, fax, name) VALUES(9, 'zone1', '00000', '00000', 'name1')
http://www.w3schools.com/sql/sql_insert.asp
About your errors
Normal. You specified NOT NULL
Normal, there is already a record for the value 0 (check your database)
Normal. You can't use default. This is a constant with his own value (1364).
Normal, the field as if can't be ommited
The only way to does not have this issue is to remove completely the reference of the ID field. Use the solution described :)
Georges explained it all for you. If you want to add auto increment to table just run this sql statement
ALTER TABLE dealerships MODIFY COLUMN dealership_id BIGINT(20) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT;
This will add the auto increment property on the column dealership_id. And if you want to change auto increment start value run this (Change 100 to your start value)
ALTER TABLE dealerships AUTO_INCREMENT=100;

How do I reset primary key according to timestamp in MySQL table?

I know that I can reset the primary key in this way:
ALTER TABLE `users` DROP `id`;
ALTER TABLE `users` AUTO_INCREMENT = 1;
ALTER TABLE `users` ADD `id` int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
But I'm wondering how I can adapt this to apply the primary key according to the timestamp in my timestamp column, i.e. the row with the oldest timestamp gets the value 1, the next oldest, the value 2, etc.
OUTPUT of SHOW CREATE TABLE
CREATE TABLE `tracks` (
`id` int(10) unsigned NOT NULL auto_increment,
`timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
`from_user` varchar(50) NOT NULL,
`source_filename` varchar(80) NOT NULL,
`uploaded_page` varchar(50) NOT NULL,
`operating_system` varchar(50) NOT NULL,
`browser` varchar(50) NOT NULL,
`os_browser_version` varchar(200) NOT NULL,
`title` varchar(100) NOT NULL,
`artist` varchar(60) NOT NULL,
`album` varchar(120) NOT NULL,
`genre` varchar(120) NOT NULL,
`format` varchar(10) NOT NULL,
`bitrate` mediumint(9) NOT NULL,
`conversion_needed` tinyint(1) NOT NULL COMMENT 'if not mp3, or higher than 192kbps',
`conversion_successful` tinyint(1) NOT NULL,
`art_extracted` tinyint(1) NOT NULL,
`art_location` varchar(200) NOT NULL,
`file_location` varchar(200) NOT NULL,
`status` varchar(500) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=96 DEFAULT CHARSET=utf8
Create a new table called users2 that has the same structure as users:
CREATE TABLE users2 LIKE users;
Then run this statment:
INSERT INTO users2
(col1, col2, ... coln)
SELECT col1, col2, ... coln
FROM users
ORDER BY your_timestamp
For your specific table:
INSERT INTO tracks2
(`timestamp`,`from_user`,`source_filename`,`uploaded_page`,`operating_system`,`browser`,`os_browser_version`,`title`,`artist`,`album`,`genre`,`format`,`bitrate`,`conversion_needed`,`conversion_successful`,`art_extracted`,`art_location`,`file_location`,`status`)
SELECT `timestamp`,`from_user`,`source_filename`,`uploaded_page`,`operating_system`,`browser`,`os_browser_version`,`title`,`artist`,`album`,`genre`,`format`,`bitrate`,`conversion_needed`,`conversion_successful`,`art_extracted`,`art_location`,`file_location`,`status`
FROM tracks
ORDER BY timestamp
I know you've already accepted an answer, but for the sake of posterity I want to chime in with a simpler solution. MySQL allows you to re-order the rows of a table using ALTER TABLE...ORDER BY..., so you can easily do this by adding one more ALTER TABLE statement to your original example:
ALTER TABLE `users` DROP COLUMN `id`;
ALTER TABLE `users` ORDER BY `timestamp`;
ALTER TABLE `users` AUTO_INCREMENT = 1;
ALTER TABLE `users` ADD COLUMN `id` int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;

What causes duplicate PKs in MySQL?

I encountered this quite a few times so far, but still don't understand it (my MySQL internals skills are equal to none).
I know it's probably a PEBKAC but trying to replicate the behavior manually ends up with an error (autoincrement).
CREATE TABLE `foo_bar` (
`id` int(12) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(12) unsigned DEFAULT NULL,
`order_id` int(12) unsigned DEFAULT NULL,
`email_address` varchar(50) DEFAULT NULL,
`mobile_number` varchar(20) DEFAULT NULL,
`message` longtext NOT NULL,
`message_received` int(12) unsigned DEFAULT NULL,
`failed_to_send` tinyint(1) unsigned DEFAULT NULL,
`fraudulent_activity` tinyint(1) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=ARCHIVE DEFAULT CHARSET=utf8;
When your program inserts a row in the database, it should provide NULL as the value for auto-incremented field:
CREATE TABLE `customers` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 128 ) NOT NULL
) ENGINE = MYISAM ;
INSERT INTO `customers` ( `id` , `name` )
VALUES ( NULL , 'Customer 1' ), ( NULL , 'Customer 2' );
If you try to insert a specific value in id field, MySQL will give an error:
SQL query:
INSERT INTO `customers` ( `id` , `name` )
VALUES ( '1', 'Customer 3' );
MySQL said:
#1062 - Duplicate entry '1' for key 'PRIMARY'
Although the answer to "what caused this" didn't come up, REPAIR TABLE fixes the problem.
Answering this so I can close the question.