MySql CONSTRAINT问题

时间:2018-12-14 06:40:01

标签: mysql

以最终形式获取后视图定义。

没什么可提取的

在服务器中执行SQL脚本会导致:

ERROR: 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 'CONSTRAINT `fk_employee_department`
FOREIGN KEY (`dpt_id`)
REFERENCES `r' at line 16

SQL代码:

    CREATE TABLE IF NOT EXISTS `rrm17b`.`employee` (
      `emp_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
      `dpt_id` INT UNSIGNED NULL,
      `emp_first` VARCHAR(20) NOT NULL,
      `emp_last` VARCHAR(20) NOT NULL,
      `emp_type` ENUM('f', 'p') NOT NULL,
      `emp_street` VARCHAR(30) NOT NULL,
      `emp_city` VARCHAR(20) NOT NULL,
      `emp_state` CHAR(2) NOT NULL,
      `emp_zip` INT UNSIGNED NOT NULL,
      `emp_phone` BIGINT UNSIGNED NOT NULL,
      `emp_email` VARCHAR(45) NOT NULL,
      `emp_notes` VARCHAR(100) NULL,
      PRIMARY KEY (`emp_id`),
      INDEX `fk_employee_department_idx` (`dpt_id` ASC) 
      CONSTRAINT `fk_employee_department`
        FOREIGN KEY (`dpt_id`)
        REFERENCES `rrm17b`.`department` (`dpt_id`)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION)
    ENGINE = InnoDB

SQL脚本执行完成:语句:成功9次,失败1次

以最终形式获取后视图定义。 什么都没拿

1 个答案:

答案 0 :(得分:1)

请尝试以下操作:

CREATE TABLE IF NOT EXISTS rrm17b.employee(
emp_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
dpt_id INT UNSIGNED NULL,
emp_first VARCHAR(20) NOT NULL,
emp_last VARCHAR(20) NOT NULL,
emp_type ENUM('f', 'p') NOT NULL,
emp_street VARCHAR(30) NOT NULL,
emp_city VARCHAR(20) NOT NULL,
emp_state CHAR(2) NOT NULL,
emp_zip INT UNSIGNED NOT NULL,
emp_phone BIGINT UNSIGNED NOT NULL,
emp_email VARCHAR(45) NOT NULL,
emp_notes VARCHAR(100) NULL,
      PRIMARY KEY (emp_id),
      INDEX fk_employee_department_idx(dpt_id ASC),
      CONSTRAINT fk_employee_department
      FOREIGN KEY (dpt_id)
      REFERENCES rrm17b.department(dpt_id) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB

修复了多项问题:

-在INDEX之后添加逗号

-删除了dpt_id之后的“`”