I am trying to import some fields into a MySQL database, here is my import file...
CREATE TABLE mytable(
zip VARCHAR(7) NOT NULL PRIMARY KEY
,lat DECIMAL(10,8) NOT NULL
,long DECIMAL(11,8) NOT NULL
,data VARCHAR(3) NOT NULL
);
It is giving me the following error...
check the manual that corresponds to your MySQL server version for the right syntax to use near 'long DECIMAL(11,8) NOT NULL ,data VARCHAR(3) NOT NULL )' at line 4
It is not liking the long field that I am trying to set as DECIMAL type
Where am I going wrong?
The long is the reserved keyword in MySQL. Try using escaping like this:
CREATE TABLE mytable
(zip VARCHAR(7) NOT NULL PRIMARY KEY
,lat DECIMAL(10,8) NOT NULL
,`long` DECIMAL(11,8) NOT NULL
,data VARCHAR(3) NOT NULL
);
Or, what is better, try using another name. For latitude and longitude this might be lat and lng or lat and lon.
Related
CREATE TABLE 'behandelingen' (
'behandeling_id' int(10) NOT NULL auto_increment,
'behandeling' varchar(35) NOT NULL default '',
'kosten' float NOT NULL default '0',
'bank_reknr' varchar(20) NOT NULL default '',
PRIMARY KEY ('behandeling_id'),
UNIQUE KEY 'behandeling' ('behandeling')
);
Trying to import a database / tables to a my local server using phpmyadmin. I keep coming up with 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 ''behandelingen' ( 'behandeling_id' int(10) NOT NULL
auto_increment, 'behan' at line 1
Static analysis:
4 errors were found during analysis.
A symbol name was expected! (near "'behandeling_id'" at position 34)
At least one column definition was expected. (near "'behandeling_id'" at position 34)
Unexpected beginning of statement. (near "10" at position 55)
Unrecognized statement type. (near "NOT NULL" at position 59)
can some one shed some light on it ... I am using Server version: 5.7.14 - MySQL Community Server (GPL)
Use backticks instead of single quotes on the table name and column names. See below:
CREATE TABLE `behandelingen` (
`behandeling_id` int(10) NOT NULL auto_increment,
`behandeling` varchar(35) NOT NULL default '',
`kosten` float NOT NULL default '0',
`bank_reknr` varchar(20) NOT NULL default '',
PRIMARY KEY (`behandeling_id`),
UNIQUE KEY `behandeling` (`behandeling`)
);
CREATE TABLE behandelingen ( behandeling_id int(10) NOT NULL auto_increment, behandeling varchar(35) NOT NULL default 'default' , kosten float NOT NULL default 0, bank_reknr varchar(20) NOT NULL default 'default' , PRIMARY KEY (behandeling_id), UNIQUE KEY behandeling (behandeling) );
I can't test it right now, but I think that you shouldn't be using single quotes to define the name of the column: e.g.
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
I am trying to save a 15 by 15 letters board for a word game as a 225 characters string in MySQL 5.6 database by using the following table:
create table games (
gid integer primary key auto_increment,
player1 integer references users(uid) on delete cascade,
player2 integer references users(uid) on delete cascade,
stamp1 integer not null default 0,
stamp2 integer not null default 0,
letters1 varchar(7) not null,
letters2 varchar(7) not null,
letters varchar(116) not null,
board varchar(225) not null default space(225)
);
Unfortunately, this returns an error:
ERROR 1064 (42000): 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 'space(225))'
I am just trying to initialize the game board with 15 x 15 spaces - and would like to modify that board with each move later.
Could you please recommend me a better way for doing that?
You cannot use a function as a default value:
Can I use a function for a default value in MySql?
so you can just define your default values as a string:
board varchar(225) not null default ".................up to 225...."
or you can use a trigger.
create table foo(
id UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
barbaz VARCHAR(50) CHARACTER SET utf8
)
[Edit]: OK I had accidentally removed the INT and I didn't realize it (these statements are generated automatically).[/Edit]
This gives me this error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
barbaz VARCHAR(50) CHARACTER SET '
Yeah, I'm using Maria DB as it replaces mysql in Fedora, but supposedly it's exactly the same syntax as Mysql.
Thanks for your help.
UNSIGNED is not a type. You need to specify the type (most likely INT):
id INT UNSIGNED ...
Try this:
CREATE TABLE `foo`(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`barbaz` VARCHAR(50) CHARSET utf8,
PRIMARY KEY (`id`)
);
In mysql I have a varchar containing latitude and longitude provided by Google maps.
I need to be able to query based on a bounding box value, but have no need for the geo features now available. I'm trying to populate 2 new Decimal fields with the Decimal values found in the varchar. Here is the query that I'm trying to use, but the result are all rounded values in the new fields.
Sample data:
'45.390746926938185, -122.75535710155964',
'45.416444621636415, -122.63058006763458'
Create Table:
CREATE TABLE IF NOT EXISTS `cameras` (
`id` int(11) NOT NULL auto_increment,
`user_id` int(75) NOT NULL,
`position` varchar(75) NOT NULL,
`latitude` decimal(17,15) default NULL,
`longitude` decimal(18,15) default NULL,
`address` varchar(75) NOT NULL,<br />
`date` varchar(11) NOT NULL,<br />
`status` int(1) NOT NULL default '1',
`duplicate_report` int(11) NOT NULL default '0',
`missing_report` int(11) NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1050 ;
SQL:
UPDATE cameras
SET latitude = CAST( (substring(position,1,locate(',',position))) AS DECIMAL(17,15) ),
longitude = CAST( (substring(position,locate(',',position)+1)) AS DECIMAL(18,15) )
SQL Alternate attempt:
UPDATE cameras
SET latitude = CONVERT( (substring(position,1,locate(',',position))), DECIMAL(17,15) ),
longitude = CONVERT( (substring(position,locate(', ',position)+1)), DECIMAL(18,15) )
The resulting field values are for both scenarios:
45.000000000000000 and -122.000000000000000
AND
45.000000000000000 and -122.000000000000000
Can anyone see what I'm doing wrong?
Thanks.
Both the CAST and CONVERT forms seem to be correct.
SELECT CAST((SUBSTRING(t.position,1,LOCATE(',',t.position))) AS DECIMAL(17,15)) AS lat_
, CONVERT(SUBSTRING(t.position,LOCATE(', ',t.position)+1),DECIMAL(18,15)) AS long_
FROM (SELECT '45.390746926938185, -122.75535710155964' AS `position`) t
lat_ long_
------------------ ----------------------
45.390746926938185 -122.755357101559640
I think position is a reserved word, but I don't think that matters in this case. But it wouldn't hurt to assign a table alias and qualify all column references
UPDATE cameras c
SET c.latitude = CAST((SUBSTRING(c.position,1,LOCATE(',',c.position))) AS DECIMAL(17,15))
, c.longitude = CAST((SUBSTRING(c.position,LOCATE(',',c.position)+1)) AS DECIMAL(18,15))
But I suspect that won't resolve the problem.
One thing to check is for a before or after update trigger defined on the table, which is rounding/modifying the values assigned to the latitude and longitude columns?
I suggest you try running just a query.
SELECT CAST((SUBSTRING(c.position,1,LOCATE(',',c.position))) AS DECIMAL(17,15)) AS lat_
, CAST((SUBSTRING(c.position,LOCATE(',',c.position)+1)) AS DECIMAL(18,15)) AS lon_
FROM cameras c
and verify that produces the decimal values you expect.
A dot character should be recognized as a decimal point. Does the position column contain some other special characters, like a space or something?
From what you posted, it looks like the CAST and CONVERT working on the integer portion up to the decimal point. (There shouldn't be an implicit convert to signed integer in there, so it's not clear why the characters following the decimal point aren't being included.)
If you can figure out what character(s) are being used to represent the decimal point, then you could use a MySQL REPLACE() function to replace those with a simple dot character.
This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
Is there something wrong with this code? I'm running MYSQL 5 I keep getting this 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 'desc BLOB, review BLOB, url BLOB )'
Here's my query:
mysql_query("CREATE TABLE videos(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
title VARCHAR(50),
desc BLOB,
review BLOB,
url BLOB
)
") or die(mysql_error());
It looks alright to me. At first I thought it was the "BLOB" datatype but then I tried "TEXT" and it still messed up so I'm not quite sure.
desc is a reserved keyword, you need to escape it:
mysql_query("CREATE TABLE videos(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
title VARCHAR(50),
`desc` BLOB,
review BLOB,
url BLOB
)
")or die(mysql_error());
For the full list of reserved keywords, see 8.3. Reserved Words
desc ise reserved keyword for MySQL, you should cover it with backticks:
CREATE TABLE videos (
id INT NOT NULL AUTO_INCREMENT,
`title` VARCHAR(50),
`desc` BLOB,
`review` BLOB,
`url` BLOB,
PRIMARY KEY (`id`)
)
try with:
mysql_query("CREATE TABLE videos(
`id` INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
`title` VARCHAR(50) NULL,
`desc` BLOB NULL,
`review` BLOB NULL,
`url` BLOB NULL
)
")or die(mysql_error());