MySQL problem: autoID not starting with 1 - mysql

I'm creating a new table from scratch through scripting - first, I'm dropping it (in case it already exists):
DROP TABLE IF EXISTS `myTable`
then I'm creating it:
CREATE TABLE IF NOT EXISTS `myTable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
... and so on
The problem: for some strange reason, my autoID-field ALWAYS starts at 2028 instead of 1, although I'm generating it from scratch. What is wrong?

Look at the end of create block. You probably have something like AUTO_INCREMENT=2028. If this is the case just put AUTO_INCREMENT=1 at the end of create table block
such as
CREATE TABLE IF NOT EXISTS `myTable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
...
) ENGINE=xxx AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS `myTable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id));
Please add primary key

You can update it by altering the table
ALTER TABLE <tablename> AUTO_INCREMENT = 1;

Related

Auto Increment missing in my users table after importing on production. How should I modify it?

When I run this query :
ALTER TABLE `users` MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=56;
It gives me error like :
#1833 - Cannot change column 'id': used in a foreign key constraint
'designation_user_user_id_foreign' of table
'databasename.designation_user'
Found a solution that worked!
I made id column in users table unique using more option in action column of users table and then executed below query :
ALTER TABLE `users` MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

Best way to add column to a big table with longblob

I have the following table:
CREATE TABLE IF NOT EXISTS PDF_STORAGE (
ID_PDF_STORAGE bigint(20) NOT NULL AUTO_INCREMENT,
DESC_FILE varchar(255) DEFAULT NULL,
PDF_FILE longblob,
LINK_FILE varchar(255) DEFAULT NULL,
VERSION int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (ID_PDF_STORAGE)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Where PDF_FILE is a file of 10 MB on average. Today it has about 50.000 rows. I need to add a new column to this table but it is taking a long time, more than 10 min, some times giving a 401 error in PhpMyAdmin, so I'd like to know what is the proper way to achieve this...
I already tried:
ALTER TABLE PDF_STORAGE ADD VERSION INT NOT NULL DEFAULT '0' AFTER LINK_FILE ;
and
SET FOREIGN_KEY_CHECKS=0;
CREATE TABLE PDF_STORAGE_new LIKE PDF_STORAGE;
ALTER TABLE PDF_STORAGE_new ADD VERSION INT NOT NULL DEFAULT '0' AFTER LINK_FILE ;
INSERT INTO PDF_STORAGE_new (PDF_STORAGE, DESC_FILE, ID_PDF_STORAGE, LINK_FILE) SELECT * FROM PDF_STORAGE;
RENAME TABLE PDF_STORAGE TO PDF_STORAGE_old, PDF_STORAGE_new TO PDF_STORAGE;
DROP TABLE PDF_STORAGE_old;
SET FOREIGN_KEY_CHECKS = 1;
but they are also slow.. is there a better way?
Thanks
What you are doing now ALTER TABLE is the best approach to my knowledge. You can try making this change when there is not much transaction (or) DB operation going on. I mean say, do the changes in idle time.
ALTER TABLE PDF_STORAGE ADD VERSION INT NOT NULL DEFAULT '0' AFTER LINK_FILE ;
You can as well create a new table same as this table schema along with the new column.
Insert all the records from this table to the newly created table.
Rename the new table to the old table name.
delete the old table.

mysql drop statement with --

What is the difference between drop table and --drop table in mysql
For example: I'm getting error if I use -- but in all other places of Magento they are using -- before drop.
--DROP TABLE IF EXISTS {$this->getTable('faq/dinkchika')};
CREATE TABLE IF NOT EXISTS {$this->getTable('faq/dinkchika')} (
`faq_id` int(11) NOT NULL AUTO_INCREMENT,
`faq_question` varchar(255) DEFAULT NULL,
`faq_answer` varchar(255) DEFAULT NULL,
PRIMARY KEY (`faq_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
A line that starts with -- and has a space after that, is treated as a comment until the end of line. It won't be executed.
You can read more about Mysql comment syntax here: http://dev.mysql.com/doc/refman/5.1/en/comments.html
use a white space after -- ,if you are not using whitespace after -- then it will not
count as comment.after whitespace your query will look like this.
-- DROP TABLE IF EXISTS {$this->getTable('faq/dinkchika')};
CREATE TABLE IF NOT EXISTS {$this->getTable('faq/dinkchika')} (
`faq_id` int(11) NOT NULL AUTO_INCREMENT,
`faq_question` varchar(255) DEFAULT NULL,
`faq_answer` varchar(255) DEFAULT NULL,
PRIMARY KEY (`faq_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
Or you may use #(Hash) as well and
Try this: Drop table IF EXISTS table_name;
And then continue with creating the table, as it will be guaranteed to no longer exist.
I hope it will help for you..

Should I add multiple indexes?

If I have a unique index on a table that covers 2 fields, should I add another index on each field?
Example:
My table looks like this:
CREATE TABLE IF NOT EXISTS `my_table` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`usersID` int(11) NOT NULL,
`userTypesID` int(11) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
I then add a unique index which covers both usersID and userTypesID:
ALTER TABLE `my_table` ADD UNIQUE `usersID_userTypesID` ( `usersID` , `userTypesID` )
Is it worth me adding 2 more indexes, one on usersID and another on userTypesID? e.g:
ALTER TABLE `my_table` ADD INDEX ( `usersID` )
ALTER TABLE `my_table` ADD INDEX ( `userTypesID` )
Would adding these extra indexes speed up some queries? Such as:
SELECT `usersID`
FROM `my_table`
WHERE `userTypesID` = 101
Or
SELECT `usersTypesID`
FROM `my_table`
WHERE `usersID` = 29
In theory the index on (usersID, userTypesID) will also act as an index on usersID by itself, because it's the left most column.
You would benefit from an index on userTypesID too.
You don't need additional indexes, see here.
Also try SHOW INDEXES FROM my_table

mysql how to update a foreign key automatically

this is my tables
CREATE TABLE IF NOT EXISTS `carslibrary` (
`CarID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`CarName` varchar(255) NOT NULL,
PRIMARY KEY (`CarID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
CREATE TABLE IF NOT EXISTS `colorslibrary` (
`ColorID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`ColorName` varchar(255) NOT NULL,
PRIMARY KEY (`ColorID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
CREATE TABLE IF NOT EXISTS `facerecord` (
`carslibrary_ID` int(10) unsigned NOT NULL,
`colorslibrary_ID` int(11) unsigned NOT NULL,
KEY `carslibrary_ID` (`carslibrary_ID`),
KEY `colorslibrary_ID` (`colorslibrary_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
i noticed carslibrary_ID attribute inside facerecord table is not automatically updated when i add a car record inside carslibrary table, what should i do to be able to?
Firstly, you'll need to have a default value specified for the facerecord.colorslibrary_ID since you will not 'know' what it is when inserting into the carslibrary table. That said you could alter your DDL for the facerecord table to be:
CREATE TABLE `facerecord` (
`carslibrary_ID` int(10) unsigned NOT NULL,
`colorslibrary_ID` int(10) unsigned NOT NULL DEFAULT '0',
KEY `carslibrary_ID` (`carslibrary_ID`),
KEY `colorslibrary_ID` (`colorslibrary_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I've also changed the datatype of the colorslibrary_ID column to match that of the colorslibrary.ColorID column in case you ever feel like setting up a foreign key between facerecord.colorslibrary_ID and colorslibrary.ColorID ;). For the sake of completeness you should insert a row into the colorslibrary table with a ColorID = 0. Hence:
insert into `colorslibrary` (ColorName) values ('unknown color');
update `colorslibrary` set ColorID = 0 where ColorName = 'unknown color';
Then you can go ahead and define your trigger to insert into the facerecord table:
delimiter $$
CREATE TRIGGER carslibrary_trigger
AFTER insert ON carslibrary
FOR EACH ROW
BEGIN
insert into facerecord (carslibrary_ID) values (new.CarID);
END$$
delimiter;
All new rows inserted into the facerecord table will then be inserted with a colorslibrary_ID that relates to the 'unknown color' colorslibrary.ColorName.You can then manually update the facerecord.colorslibrary_ID as and when you know it.
Good luck!
PS If you need to remove any existing AFTER insert triggers from the carslibrary table you can do so by firstly finding the existing triggers:
select trigger_name
from information_schema.triggers
where event_object_table = 'carslibrary'
and action_timing = 'AFTER'
and event_manipulation= 'INSERT';
Then take the name of the trigger returned by the above statement (lets say the string 'carslibrary_trigger' is returned) and run:
drop trigger carslibrary_trigger;
Then re-run the CREATE TRIGGER script.
Once a trigger is set up it will automatically perform the action you have specified when the trigger action you have specified occurs. In this case we are telling the database "after an insert happens into the carslibrary table automatically insert a row into the facerecord table using the CarID of the new carslibrary row to populate the facerecord.carslibrary_ID column". As with most things the best way is to try it! Once you have created the trigger manually insert a new row into the 'carslibrarytable. Now look at the data in thefacerecord` table - you should see a new row that has been inserted by the trigger firing.
It sounds like you would benefit from learning about triggers. I recommend the docs on the MySQL site because this answer is way longer than I first intended it to be!
You will need to use triggers. See http://dev.mysql.com/doc/refman/5.0/en/triggers.html