mysqlcreate-tablesql-types

mySQL table creation fails - error 1064


I am using the mysql admin GUI tool to create a table. Here is the query language that results in an error:

CREATE TABLE `environment`.`moisture` (
  `city_id` INT,
  `date_holding` VARCHAR,
  `time_holding` VARCHAR,
  `open` REAL,
  `high` REAL,
  `low` REAL,
  `close` REAL,
)
CHARACTER SET utf8;

My date and time are not of the format mysql likes so I selected VARCHAR as the type for those columns and referred to them as holding columns until I can run queries to make the necessary conversions.

When I try to execute this via the GUI I get the following:

Error executing SQL commands to create table.
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 'NOT NULL,
`time_holding` VARCHAR NOT NULL,
`open` REAL NOT NULL,
`high` RE' at line 3 (error 1064)

Any idea why I am getting this? I have checked to see if I am using any reserved words but that does not seem to be the case.

Your help would be greatly appreciated.


Solution

  • The first error you have is due to that VARCHAR fields need a length like this: VARCHAR(30).

    The second error (you'll get if you fix the 2 varchars) is the comma before the last parenthesis.

    CREATE TABLE `environment`.`moisture` (
      `city_id` INT,
      `date_holding` VARCHAR(30),  --or whatever length you want
      `time_holding` VARCHAR(20),  --same
      `open` REAL,
      `high` REAL,
      `low` REAL,
      `close` REAL  --- no comma here
    )
    CHARACTER SET utf8;
    

    Side note: You do know that MySQL has specific types to handle dates and times, don't you? So you could have:

    CREATE TABLE `environment`.`moisture` (
      `city_id` INT,
      `date_holding` date,
      `time_holding` time,
      ...