Mysql error code 1222 with a single column selected? - mysql

Can someone explain why I get an error Code 1222 (The used SELECT statements have a different number of columns) after I run this query?
INSERT IGNORE INTO table1(id1)
SELECT id2 FROM table2;
It's pretty obvious that number of columns are the same, so real issue must be somewhere else. But where? Fields are the exact same:
`id2` int(11) NOT NULL COMMENT 'blabla'
Only difference is the DB engine (MyISAM on table2, InnoDB on table1), but it can't be linked, because it works like a charm if I add more columns in my INSERT/SELECT without this one.
Any ideas? Thx.

As Tim mentioned, please provide us with table scheme and more information, because I don't see an error in the above.
For example:
INSERT IGNORE INTO test_cachetags (tag, invalidations) SELECT tag, invalidations FROM cachetags;
This query works, where TAG is varchar(255) and invalidations is integer.

OK, I finally found what was going wrong: a trigger. Basically the number of variables I had at some point did not match the number of columns. (https://dev.mysql.com/doc/refman/5.5/en/stored-program-variables.html).
It seems pretty obvious now that I found the cause, but honestly, MySQL could be more explicit. It's not like I have a single and/or simple query triggered behind the scene (this one was especially a bit complex actually).
For the record error occurred on this field, because that's the one I had forgotten to declare.
Thx for your time guys.

Related

MySQL: ERROR 1146 table doesn't exist...although it does

I am following an example in a textbook that seems to gloss over the portion about setting up a simple MySQL. (I've noted some errors but I believe I've been able to correct them). Despite seemingly entering everything correctly in the terminal and the table existing, I get the 1146 error every time I attempt to describe the table.
NOTE: I am running MySQL v5.5.15 on Windows 7 VM in VirtualBox (as per the example I'm following in my book). This is a FRESH SQL DB here, just so we're clear.
CREATE DATABASE moviedb;
USE moviedb;
CREATE TABLE creditcards(
-->id varchar(20) DEFAULT NULL,
-->first_name varchar(50) DEFAULT NULL,
-->last_name varchar(50) DEFAULT NULL,
-->expiration date DEFAULT NULL);
DESCRIBE moviedb;
ERROR 1146 (42S02): Table 'moviedb.moviedb' doesn't exist
Now, the first thing that jumps out at me is that it says 'moviedb.moviedb' doesn't exist, which leads me to believe that it's looking for an element in 'moviedb' called 'moviedb' which I imagine shouldn't exist because I didn't create it. Do I need to "go back" to the "root" in order to display the contents of the moviedb? Although I'm following an example from a book, I'm also following along an arbitrary YouTube video that didn't have to enter any commands between CREATE TABLE and DESCRIBE.
If I attempt to move to a different table and then describe the moviedb, I have the same issue.
I also ran SELECT * FROM moviedb; and that didn't seem to change anything.
Here is the full screenshot from my terminal, just for transparency. I know that I'm not using proper syntax, but...I'm not a perfect man. If there's any chance that THIS is what is causing the error, I'll gladly redo it correctly, however I used proper syntax the first time and got the same error. This is take 2:
I imagine I'm doing something silly because I'm on an old version of MySQL and syntax might be slightly different.
Thank you all in advance for any input you have.
-Joe
The DESCRIBE command takes a table name and you are giving it a database name. Of course, MySQL doesn't know that and thinks you are looking for a table with the name moviesb. You need to use DESCRIBE creditcards instead.
For future google searches.
When your database is on the server and if you are controlling your database through cPanel, You gotta care about the case (Eg:- sells and SELLS are not the same). Try to match the database table name cases always.

MySQL inserting with select max(`Column`)+1 on possibly empty results

I've seen a lot of questions along the same-ish lines as mine, but they're just not quite exactly the same issue as me.
Here's my query:
insert into`quotes`(`QuoteID`,`QuoteRequestID`,`Number`,`UserID`,`Viewed`,
`_Latest`)
select uuid(),uuid(),
ifnull(max(`quotes`.`Number`),0)+1,'Some User ID',1,1
from`quotes`
join`quoterequests`using(`QuoteRequestID`)
where`quoterequests`.`UserID`='Some User Other ID';
And this is the error I'm getting:
Error Code: 1048. Column 'QuoteID' cannot be null
So my guess is that the select statement isn't returning anything, but how can that be since I'm using max() in my query?
Sure enough, if I remove the insert part and just run the select statement by itself, then I do get the expected results, with the new UUIDs (definitely not a null value) and all. What is happening here? Is this a bug with MySQL? My MySQL version is 5.7.14;
EDIT
So I have figured out if I wrap the select in another select, it now works as expected. Not sure if that's a solution or a workaround though, but it did get my query working.
it looks like you might have some trigger which generates the error - that is the only explanation I can think about.

MySQL Create Table Statement Strange Errors

I am trying to run some basic CREATE TABLE statements for my Databases course project and am getting some strange errors.
When I create the table Manuf it runs fine, but when I try to create the next table, Order, using the same syntax, it does not work.
Also, when I try to create this table, Items, I get an errno: 150. I believe this has to do with my foreign key creation, but I am not exactly sure. Here is a screenshot of that.
I am fairly new to using MySQL so any advice would be greatly appreciated, thank you.
The error on the Order table is caused by ORDER being a reserved word. You can specify it as `Order` with the backticks, but it's better if you choose a different name altogether.
The error 150 is related to the foreign key. The keys must be absolutely identical - the exact same definition, or the FK will fail with error 150.
Also, there must be an available index with that key definition or one compatible (see Kai Baku's example in the comment on the MySQL manual page). The same fields indexed in a different order will fail.
To begin with, check how those keys are defined in the origin tables. For example:
test1 varchar(50) not null
test2 varchar(50)
will not be compatible. I think that even a different collation is enough to throw FK off kilter (but this I haven't checked. The rest I'm sure of, from my personal bitter unexperience).
UPDATE: I forgot to mention, if you use InnoDB tables and issue the SHOW ENGINE INNODB STATUS, the blurb that comes out will contain a much better explanation of why the FK failed, somewhere about one third from top.

INSERT INTO statement in MySQL

I'm trying to work with YEAR function on one column in the DB and then add the results to a different table in the DWH.
What am I doing wrong?
INSERT INTO example_dwh1.dim_time (date_year)
SELECT YEAR(time_taken)
FROM exampledb.photos;
When removing the INSERT INTO line, I get the results I want, but I'm not able to insert them into the dwh table.
Thanks for your help!
The following select works, but I don't see the data in the table after the insert:
INSERT INTO example_dwh1.dim_time (date_year)
SELECT YEAR(time_taken)
FROM exampledb.photos;
There is rather broad. Assuming you have no errors in the insert, you might have:
You are incorrectly querying dim_time, so the data is there but your check is wrong.
You are inserting into dim_time in one database but querying it in another.
Assuming you have errors but are missing them, here are some possibilities:
The database does not exist.
The table does not exist.
The column is misnamed.
Other columns are declared NOT NULL.
Triggers defined on the table are preventing the insert.
Unique constraints/indexes on the table are preventing the insert.
Your question does not provide enough information to be more specific. However, it seems highly suspicious to be inserting a bunch of years -- which might include many duplicates -- into a dimension table.

Easy mysql question regarding primary keys and an insert

In mysql, how do I get the primary key used for an insert operation, when it is autoincrementing.
Basically, i want the new autoincremented value to be returned when the statement completes.
Thanks!
Your clarification comment says that you're interested in making sure that LAST_INSERT_ID() doesn't give the wrong result if another concurrent INSERT happens. Rest assured that it is safe to use LAST_INSERT_ID() regardless of other concurrent activity. LAST_INSERT_ID() returns only the most recent ID generated during the current session.
You can try it yourself:
Open two shell windows, run mysql
client in each and connect to
database.
Shell 1: INSERT into a table with an
AUTO_INCREMENT key.
Shell 1: SELECT LAST_INSERT_ID(),
see result.
Shell 2: INSERT into the same table.
Shell 2: SELECT LAST_INSERT_ID(),
see result different from shell 1.
Shell 1: SELECT LAST_INSERT_ID()
again, see a repeat of earlier
result.
If you think about it, this is the only way that makes sense. All databases that support auto-incrementing key mechanisms must act this way. If the result depends on a race condition with other clients possibly INSERTing concurrently, then there would be no dependable way to get the last inserted ID value in your current session.
MySQL's LAST_INSERT_ID()
The MySQL Docs describe the function: LAST_INSERT_ID()
[select max(primary_key_column_name) from table_name]
Ahhh not nessecarily. I am not an MySQL guy but there are specific way to get the last inserted id for the last completed action that are a little more robust than this. What if an insert has happened between you writing to the table and querying it? I know about because it stung me many moons ago (so yeah it does happen).
If all else fails read the manual: http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html