Unknown column in 'where clause' while there`s no 'where clause' and the column isn`t mentioned in the query - mysql-error-1054

I`m new in SQL and now straggling with an error 1054 Unknown column in 'where clause' in MySQL.
There`s a simple table 'customer_orders' with 3 columns 'order_id', 'staff_id' and 'order_total'
I needed to insert a line. My query was
INSERT INTO customer_orders VALUES (25,1,10.50);
First that worked, then I realized a need to delete the row and insert with a slight change
Now it throws the error 'Error Code: 1054. Unknown column 'order_id' in 'where clause''
Theres no such column in the query and the column isnt mentioned.
I renamed the column into 'order_id_new' and explicitly specified the column names in the INSERT statement - the same error.
INSERT INTO customer_orders (order_id_new, staff_id, order_total) VALUES
(23,1,11.43);
When I created a copy of the table the same worked fine.
Any ideas what is wrong with the table?
Thanks in advance.

It turned out there was a Trigger which conflicted with the query. Strange that the error doesn`t refer to the trigger in any way. Once I fixed the trigger the query worked.

Related

SQLite - add a column if it does not exist

I am new to SQLite. I want to add a column if it does not exist.
How to check if the column name exists and then add if it does not?
I tried
ALTER TABLE table ADD COLUMN colname INTEGER ON CONFLICT IGNORE
But it shows an error
Result: near "ON": syntax error
Any advice how it can be achieved?
Thanks in advance.
First get a list of table column names - as list - with something like:
select group_concat(c.name) from pragma_table_info('table_name') c;
Then do a CASE expression on whether the new column name you want to add exists in the list above. More info at: https://www.sqlite.org/lang_expr.html

How to make postgres tell which column is causing an error

While inserting data into a table which has many columns:
INSERT INTO MyTable ("name", ..100+ columns)
VALUES ('Michel', ... 100+ values)
I made an error creating a specific value so PostgreSQL tells us:
ERROR: value too long for type character varying(2)
I would like to avoid going through the whole table schema to guess which column is failing.
Is there a way to configure PostgreSQL so it tells us which column is causing the error?
One quick way might be to query the information schema table for your database and look for character columns having a maximum width of 2 (to which your error is alluding):
SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'MyTable' AND
character_maximum_length = 2;

SQL Error: ORA-00913: too many values

Two tables are identical in terms of table name, column names, datatype and size. These tables are located in separate databases, but I am use to
current Log in in hr user.
insert into abc.employees select * from employees where employee_id=100;
I can not give use original query from corporate office.
Error starting at line 1 in command:
insert into abc.employees select * from employees where employee_id=100;
Error at Command Line:1 Column:25
Error report:
SQL Error: ORA-00913: too many values
00913. 00000 - "too many values"
*Cause:
*Action:
You should specify column names as below. It's good practice and probably solve your problem
insert into abc.employees (col1,col2)
select col1,col2 from employees where employee_id=100;
EDIT:
As you said employees has 112 columns (sic!) try to run below select to compare both tables' columns
select *
from ALL_TAB_COLUMNS ATC1
left join ALL_TAB_COLUMNS ATC2 on ATC1.COLUMN_NAME = ATC1.COLUMN_NAME
and ATC1.owner = UPPER('2nd owner')
where ATC1.owner = UPPER('abc')
and ATC2.COLUMN_NAME is null
AND ATC1.TABLE_NAME = 'employees'
and than you should upgrade your tables to have the same structure.
The 00947 message indicates that the record which you are trying to send to Oracle lacks one or more of the columns which was included at the time the table was created.
The 00913 message indicates that the record which you are trying to send to Oracle includes more columns than were included at the time the table was created.
You just need to check the number of columns and its type in both the tables
ie the tables that are involved in the sql.
If you are having 112 columns in one single table and you would like to insert data from source table, you could do as
create table employees as select * from source_employees where employee_id=100;
Or from sqlplus do as
copy from source_schema/password insert employees using select * from
source_employees where employee_id=100;
For me this works perfect
insert into oehr.employees select * from employees where employee_id=99
I am not sure why you get error. The nature of the error code you have produced is the columns didn't match.
One good approach will be to use the answer #Parodo specified
this is a bit late.. but i have seen this problem occurs when you want to insert or delete one line from/to DB but u put/pull more than one line or more than one value ,
E.g:
you want to delete one line from DB with a specific value such as id of an item but you've queried a list of ids then you will encounter the same exception message.
regards.

SQL INSERT with sub query

I have a table with 2 columns. I want to provide the 1st columns value but use a select statement to query another table to figure out the value that will go in the 2nd column of the first table.
Heres what I came up with but I know is wrong..
INSERT INTO VehicleModels_VehicleSubModels (VehicleModelId, VehicleSubModelYearId)
(SELECT #ModelId, VehicleSubModelYearId
FROM VehicleSubYearIntermediate
WHERE SubModelId=#SubModelId
AND YearId=#YearId)
Essentially I want to provide the value for VehicleModelId through #ModelId, but it won't let me use it outside of the select statement.
Try removing the brackets around the SELECT, as presumbably you're seeing an incorrect syntax error?
INSERT INTO VehicleModels_VehicleSubModels (VehicleModelId, VehicleSubModelYearId)
SELECT #ModelId,VehicleSubModelYearId
FROM VehicleSubYearIntermediate
WHERE SubModelId=#SubModelId
AND YearId=#YearId

mysql updating cell content issue

I am trying to update a value in my database but am recieving the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users' ('new_user') VALUES ('1') WHERE 'id'= 5' at line 1
I am trying to UPDATE the table 'users' in the column 'new_user' where the id is equal to $userid. But it don't work. Please help.
$newuservalue = '1';
$notnewuser ="UPDATE 'users' ('new_user') VALUES ('$newuservalue') WHERE 'id'= $userid ";
$query2 = mysql_query($notnewuser) or die(mysql_error());
Well, your syntax is wrong.
It should be:
UPDATE table_name SET field1=new-value1, field2=new-value2 [WHERE Clause]
So, remove the "values" part of your query and put in the "set" part.
Here's a link to the official documentation.
i have that problems some times, that's the code to insert a new row :d
It should be:
UPDATE users SET new_user='$newuservalue' WHERE id=$userid
you also don't need to put quotes around your column names, that might give some problems as well.