Search code examples
mysqlsqlsyntax-errormysql-error-1064ddl

MySQL 5.7 Windows, table key issue?


What is wrong with this MySQL table script code?:

CREATE TABLE `securities_master`.`symbol` (
  `id` INT NOT NULL,
  `exchange_id` INT NULL,
  `ticker` VARCHAR(32) NOT NULL,
  `instrument` VARCHAR(64) NOT NULL,
  `name` VARCHAR(255) NULL,
  `sector` VARCHAR(255) NULL,
  `currency` VARCHAR(32) NULL,
  `created_date` DATETIME NOT NULL,
  `last_updated_date` DATETIME NOT NULL,
  PRIMARY KEY (`id`), FOREIGN KEY 'index_exchange_id' ('exchange_id'))
ENGINE = InnoDB AUTO_INCREMENT = 1
DEFAULT CHARACTER SET = utf8;

I think it is the "FOREIGN KEY" but I am not sure


Solution

  • Single quotes denote string literals in SQL. Object names (such as the constraint name and the columns it refers to) should be denoted with backticks, or nothing at all:

    CREATE TABLE `securities_master`.`symbol` (
      `id` INT NOT NULL,
      `exchange_id` INT NULL,
      `ticker` VARCHAR(32) NOT NULL,
      `instrument` VARCHAR(64) NOT NULL,
      `name` VARCHAR(255) NULL,
      `sector` VARCHAR(255) NULL,
      `currency` VARCHAR(32) NULL,
      `created_date` DATETIME NOT NULL,
      `last_updated_date` DATETIME NOT NULL,
      PRIMARY KEY (`id`), 
      FOREIGN KEY `index_exchange_id` (`exchange_id`)
      -- Here ----^-----------------^--^-----------^
    )
    ENGINE = InnoDB AUTO_INCREMENT = 1
    DEFAULT CHARACTER SET = utf8;