简体   繁体   English

SQL-无法添加外键约束

[英]SQL - Cannot add foreign key constraint

I am completely new to writing SQL code and I am attempting to run a simple table creation, however I cannot find where the error in my programming is, and as I am completely new I am struggling with this creation. 我对编写SQL代码是完全陌生的,并且试图运行一个简单的表创建,但是我找不到编程错误所在的位置,而且由于我是一个全新的人,因此我正为此创建而苦苦挣扎。

This is a school project that I am working on, and hoping anyone can help. 这是我正在研究的一个学校项目,希望任何人都可以提供帮助。

The error I am receiving in 'SQLFiddle' is "Cannot add foreign key constraint" on the following code: 我在“ SQLFiddle”中收到的错误是以下代码上的“无法添加外键约束”:

CREATE TABLE invoice(
  invoice_id INT NOT NULL,
  customer_id INT NOT NULL,
  order_date DATE NULL,
  spec_order_note VARCHAR(45) NULL,
  PRIMARY KEY(invoice_id, customer_id),
  FOREIGN KEY (customer_id)
  REFERENCES customer.customer_id
  ON DELETE CASCADE
  ON UPDATE CASCADE
);

CREATE TABLE line_item (
  invoice_id INT NOT NULL,
  donut_id INT NOT NULL,
  quantity INT NULL
  CONSTRAINT donut_invoice
  FOREIGN KEY invoice_id
  REFERENCES invoice.invoice_id
  ON DELETE RESTRICT
  ON UPDATE RESTRICT
)

CREATE TABLE donut (
  donut_id INT NOT NULL,
  donut_name VARCHAR(15) NULL,
  description VARCHAR(30) NULL,
  unit_price INT NULL
  PRIMARY KEY(donut_id),
)

CREATE TABLE customer (
  customer_id INT NOT NULL,
  last_name VARCHAR(15) NULL,
  first_name VARCHAR(10) NULL,
  street_add VARCHAR(20) NULL,
  apt_num INT NULL,
  city VARCHAR(20) NULL,
  state VARCHAR(15) NULL,
  zip_code INT NULL,
  home_phone VARCHAR(10) NULL,
  mobile_phone VARCHAR(10) NULL,
  other_phone VARCHAR(10) NULL,
  customer_notes VARCHAR(45) NULL
  PRIMARY KEY(customer_id),
)

Any help is greatly appreciated. 任何帮助是极大的赞赏。

You can only reference existing tables and columns in foreign constraints. 您只能在外部约束中引用现有的表和列。 So if you want to reference customer table in invoice 's foreign key, you need to either create customer before invoice or add the foreign key constrain additionally using ALTER TABLE . 因此,如果要在invoice的外键中引用customer表,则需要在invoice前创建customer ,或者使用ALTER TABLE添加外键约束。

Apart of that, there's couple syntax errors in your code like missing semicolons and misplaced (missing and additional) commas. 除此之外,您的代码中还存在一些语法错误,例如缺少分号和错位(缺少和附加的逗号)。

A working code: 工作代码:

CREATE TABLE customer (
  customer_id INT NOT NULL,
  last_name VARCHAR(15) NULL,
  first_name VARCHAR(10) NULL,
  street_add VARCHAR(20) NULL,
  apt_num INT NULL,
  city VARCHAR(20) NULL,
  state VARCHAR(15) NULL,
  zip_code INT NULL,
  home_phone VARCHAR(10) NULL,
  mobile_phone VARCHAR(10) NULL,
  other_phone VARCHAR(10) NULL,
  customer_notes VARCHAR(45) NULL,
  PRIMARY KEY(customer_id)
);

CREATE TABLE invoice(
  invoice_id INT NOT NULL,
  customer_id INT NOT NULL,
  order_date DATE NULL,
  spec_order_note VARCHAR(45) NULL,
  PRIMARY KEY(invoice_id, customer_id),
  FOREIGN KEY (customer_id)
  REFERENCES customer (customer_id)
  ON DELETE CASCADE
  ON UPDATE CASCADE
);

CREATE TABLE line_item (
  invoice_id INT NOT NULL,
  donut_id INT NOT NULL,
  quantity INT NULL,
  CONSTRAINT donut_invoice
  FOREIGN KEY (invoice_id)
  REFERENCES invoice (invoice_id)
  ON DELETE RESTRICT
  ON UPDATE RESTRICT
);

CREATE TABLE donut (
  donut_id INT NOT NULL,
  donut_name VARCHAR(15) NULL,
  description VARCHAR(30) NULL,
  unit_price INT NULL,
  PRIMARY KEY(donut_id)
);

http://sqlfiddle.com/#!9/36b044 http://sqlfiddle.com/#!9/36b044

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM