SQL语法标识符问题

时间:2017-09-14 10:18:37

标签: mysql sql

我正在进行SQL分配以创建/更新数据库和表。我收到错误,指出“标识符在此位置无效输入”和“意外标识符”。这是我的SQL:

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

DROP SCHEMA IF EXISTS SparkleDiamonds;    
CREATE SCHEMA IF NOT EXISTS SparkleDiamonds DEFAULT CHARACTER SET utf8 ;
USE SparkleDiamonds;

DROP TABLE IF EXISTS SparkleDiamonds.Customer;
CREATE TABLE IF NOT EXISTS SparkleDiamonds.Customer (
    cust_num INT NOT NULL,
    cust_fname VARCHAR(45) NULL,
    cust_initial VARCHAR(45) NULL,  
    cust_lname VARCHAR(45) NULL,
    cust_address VARCHAR(45) NULL,
    cust_address2 VARCHAR(45) NULL,
    cust_address3 VARCHAR(45) NULL,
    cust_Phone_No VARCHAR(45) NULL,
    location2_address VARCHAR(45) NULL,
    location2_address2 VARCHAR(45) NULL,
    location2_address3 VARCHAR(45) NULL,
  PRIMARY KEY (cust_num))
ENGINE = InnoDB;

DROP TABLE IF EXISTS SparkleDiamonds.Orders;
CREATE TABLE IF NOT EXISTS SparkleDiamonds.Orders (
    order_num INT NOT NULL, 
    cust_num INT NULL,
    order_tran_id INT NULL,
    order_emp_id INT NULL,
    order_transaction_time VARCHAR(45) NULL,
    order_location VARCHAR(45) NULL,
  PRIMARY KEY (order_num))
  *INDEX fk_Orders_Customer_idx (cust_num ASC),
  CONSTRAINT fk_Orders_Customer
    FOREIGN KEY cust_num
    REFERENCES SparkleDiamonds.Customer cust_num
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

DROP TABLE IF EXISTS SparkleDiamonds.Inventory;
CREATE TABLE IF NOT EXISTS SparkleDiamonds.Inventory (
    inv_diam_item_num VARCHAR(45) NOT NULL,
    order_tran_id INT NULL,
    inv_diam_cost DOUBLE NULL,
    inv_diam_price DOUBLE NULL,
    inv_diam_carat DOUBLE NULL,
    inv_diam_clarity VARCHAR(45) NULL,
    inv_diam_cut VARCHAR(45) NULL,
    inv_diam_color VARCHAR(45) NULL,
    inv_diam_on_hand  VARCHAR(45) NULL,
  PRIMARY KEY (inv_diam_item_num))
  *INDEX fk_Inventory_Orders_idx (order_tran_id ASC),
  CONSTRAINT fk_Inventory_Orders1
    FOREIGN KEY (order_tran_id)
    REFERENCES SparkleDiamonds.Inventory (order_tran_id)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

我在问题所在的位置放了一个星号,即索引语句。我是SQL的新手,我不确定什么是错的,代码应该在这些点上阅读。我的问题是什么?它应该读什么?什么代码可以解决这个问题?

1 个答案:

答案 0 :(得分:0)

我可以看到你有这样的语法错误:

CREATE TABLE IF NOT EXISTS SparkleDiamonds.Orders (
    order_num INT NOT NULL, 
    cust_num INT NULL,
    order_tran_id INT NULL,
    order_emp_id INT NULL,
    order_transaction_time VARCHAR(45) NULL,
    order_location VARCHAR(45) NULL,
    PRIMARY KEY (order_num) )
  *INDEX fk_Orders_Customer_idx (cust_num ASC),
    CONSTRAINT fk_Orders_Customer
        FOREIGN KEY cust_num
        REFERENCES SparkleDiamonds.Customer cust_num
        ON DELETE NO ACTION
        ON UPDATE NO ACTION
)

我想你打算:

CREATE TABLE IF NOT EXISTS SparkleDiamonds.Orders (
    order_num INT NOT NULL, 
    cust_num INT NULL,
    order_tran_id INT NULL,
    order_emp_id INT NULL,
    order_transaction_time VARCHAR(45) NULL,
    order_location VARCHAR(45) NULL,
    PRIMARY KEY (order_num),
    INDEX fk_Orders_Customer_idx (cust_num ASC),
    CONSTRAINT fk_Orders_Customer
        FOREIGN KEY (cust_num)
        REFERENCES SparkleDiamonds.Customer(cust_num)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION
);

的变化:

  • primary key
  • 之后的额外帮助
  • primary key声明后的逗号
  • 多余*
  • 外键约束的cust_num附近的括号

您在其他表中有类似的错误。你可能还有其他问题。