Why i need to define table name everytime in WHERE clause? - mysql

i was testing a software on my computer , i have a mysql installed on it . now i have transfered this software to a server . no any mysql command is working
for example this is the command that was working on my computer
SELECT COUNT(*) FROM camera WHERE stored=3;
when i try to execute it on my server here is the result
1064 - 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 'stored=3' at line 1
but this one is working
SELECT COUNT(*) FROM camera WHERE camera.stored=3;
is there any way to restore it without needing to define the table name ?

Stored is a MySQL Reserved Word, so MySQL thinks you are intending to use that Reserved Word.
When you qualify it with the Table Name, then it knows what you are talking about.
UPDATE: You could wrap stored in back ticks (shown below) so it's treated as text and not a Reserved Word.
SELECT COUNT(*) FROM camera WHERE `stored`=3;

Related

Escaping database name in prepared statement with Node

I am trying to use mysql library to request my database.
I need to use the syntax with query placeholders, so I tried a simple request:
connection.query('DROP DATABASE IF EXISTS ?;', ['mydb']);
But this leads to :
'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 \'\'mydb\'\' at line 1'
The query is indeed:
'DROP DATABASE IF EXISTS \'mydb\';'
So how is this supposed to work actually ?
The MySQL notation for this is:
DROP DATABASE IF EXISTS `mydb`
Since you're escaping it as a string, that's not a database reference it can drop.
Some drivers support alternate placeholders for this very reason:
DROP DATABASE IF EXISTS ??
Normally you can't use placeholder values for things like databases, columns or tables as these are treated differently. This is a limitation of the driver.
You just need to be careful on user-supplied values and names with irregular characters in them. These need to be escaped according to MySQL schema identifier rules.

SQL "use" command not working in MySQL 5.7

I am having problems getting the SQL "use" command to work in MySQL 5.7.
I have a database 'mydb' with a 'character' table. (At the moment, the table is empty, but I don't think that should matter here).
Explicitly using "use" gives me an error:
use mydb;
select count(*) from character;
Error Code: 1064. 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 'character' at line 1 0.000 sec
However, the following line, which does not use "use", works okay:
select count(*) from mydb.character;
Does anyone know what I am doing wrong? Admittedly, I am quite new to SQL. The full output is shown below:
character is a reserved keyword in MySQL. Hence you get the error when you select from that table. Escape with `` to avoid this. Or use names other than reserved keywords for your tablenames.
Reserved Keywords in MySQL

Issue with MySQL and phpAdmin

I am using MySQL 5.1.5 on a Yahoo Sight Server, with phpMyAdmin as the databse interface.
I use the following Query
UPDATE table_family SET last_name='Smith' WHERE id=1;
Then I get the following error:
Error There seems to be an error in your SQL query. The MySQL server
error output below, if there is any, may also help you in diagnosing
the problem
ERROR: Unknown Punctuation String # 34 STR: =\ SQL: UPDATE
table_family SET last_name=\'Smith\' WHERE id=1;
SQL query:
UPDATE table_family SET last_name=\'Smith\' WHERE id=1;
MySQL said: Documentation
1064 - 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 '\'Smith\' WHERE id=1' at line 1
Ideas? I feel like its an issue with the database, not with my code. the last_name field is a varchar(50). I actually opened a ticket with support on this issue, but it happened to me on two different domains I have with Yahoo, so that makes me think there is more than I know is going on. I have done this with integer fields that don't require the (') single quote and have had no issues. I have also run my syntax through a local access database I created just to make sure it wan't a syntax issue. Worked perfectly first time. Then I had 2 database guys I know look at it. They think its good too. So now I am lost.
Thanks for any help you can provide.
Andy

How to insert images in blob in mysql table using only sql syntax (without PHP)?

Hi,I am new to SQL and I wanted to store images in the database.I already created a column with blob data type and tried to execute the following statement as given here
INSERT INTO `abc`
(`img`)
SELECT
BulkColumn FROM OPENROWSET(
Bulk 'C:\Users\adity\Desktop\New folder\a.png', SINGLE_BLOB) AS BLOB
which gives 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 '(
Bulk C:\Users\name\Desktop\New folder\a.png, SINGLE_BLOB) AS BLOB' at line 4
I also tried following code as given here
insert into table `abc`(`img`) values('C:\Users\name\Desktop\New folder\an.jpg') where id=1;
which gives the error
#1064 - 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 'table abc(img) values('C:\Users\adity\Desktop\New folder\an.jpg') where id=1' at line 1
So please suggest me how to store images in a blob without using php,etc and simply using simple sql insert statement.I am using wamp server for my database.
I know that I should use file system for images instead of using database.But what does a file system actually mean.Does it mean a file or image hosting site whose address will be stored in database.
I think that command is a MSSQL syntax. Try this command:
INSERT INTO `abc`
(`img`)
VALUES
(LOAD_FILE('C:/Users/adity/Desktop/New folder/a.png'))
This command stores image as a BLOB
Through Mysql workbench, its very easy to load images into database using the following steps.
Right click on the value of the (blob)column in the table and select "Load value from file".
Then we can provide the image path in the system.
Then it will converted into byte array and stored it automatically.
finally save the changes of the table.
Below works for me,
However, I was able to get it done by moving the image(fileName.jpg) file first in to below folder(in my case) C:\ProgramData\MySQL\MySQL Server 5.7\Uploads and then I executed below command and it works for me,
INSERT INTO `abc`
(`img`)
VALUES
(LOAD_FILE('C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/an.jpg'));
Hope this helps.

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

I am using Sybase Power Designer to create a database from a physical data model (Sybase creates an SQL file) . When i import the SQL file with phpMyAdmin I have the following error:
#1064 - 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 'if exists(select 1 from sys.sysforeignkey where role='FK_ARTWORK_CREATES_ARTIST'' at line 7 .
Any ideas? Could this error appear due to errors on the physical model or there is another problem?
This is the code :
if exists(select 1 from sys.sysforeignkey where role='FK_ARTWORK_HAS_BUY') then
alter table artwork
delete foreign key FK_ARTWORK_HAS_BUY
end if;
The error you are getting is from MySQL. Regardless of the tool used to generate SQL, the database seems to be MySQL (or something is terribly wrong with your systems if they are confused and think they are MySQL).
The MySQL if statement (documented here) has to be inside a stored program. That means that this code only compiles inside a stored procedure, user defined function, or trigger. It doesn't "just work" on its own.
In addition, MySQL doesn't have sys tables. It uses information_schema tables. My strongest suggestion is to use tools appropriate for your actual database. If you are using a tool to generate Sybase, then use Sybase as the destination database. If you are using MySQL, then use a tool to generate MySQL code. Or, better yet, learn how to write the commands yourself.
Finally, if you intend to use Sybase, then connect to the correct database and your problem should be fixed.
As I can't post here, and sqlfiddle.com temporary unavailable, I decided to post code with correct syntax for Sybase on the PasteBin