У меня проблемы с созданием отношений таблиц. Я могу распечатать одну сущность, но остальные не запустятся? mySQL 5,7 на unix терминале - PullRequest
0 голосов
/ 19 апреля 2020
--------------
CREATE DATABASE kiros_ACMEOnline
--------------

Query OK, 1 row affected (0.00 sec)

Database changed
--------------
CREATE TABLE ITEM(
Item_Number INT UNSIGNED AUTO_INCREMENT, 
Item_Name VARCHAR(35) NOT NULL,
Description VARCHAR(255),
Model VARCHAR(50) NOT NULL,
Price VARCHAR(9) NOT NULL,
CONSTRAINT item_pk PRIMARY KEY (Item_Number))
--------------

Query OK, 0 rows affected (0.01 sec)

--------------
DESCRIBE ITEM
--------------

+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| Item_Number | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| Item_Name   | varchar(35)      | NO   |     | NULL    |                |
| Description | varchar(255)     | YES  |     | NULL    |                |
| Model       | varchar(50)      | NO   |     | NULL    |                |
| Price       | varchar(9)       | NO   |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

--------------
CREATE TABLE CUSTOMER(
CustomerID INT UNSIGNED NOT NULL,
Address VARCHAR(150) NOT NULL,
Email VARCHAR(80),
bussiness_or_home ENUM('b', 'h'),
CONSTRAINT customer_pk PRIMARY KEY (CustomerID))
--------------

Query OK, 0 rows affected (0.02 sec)

--------------
DESCRIBE CUSTOMER
--------------

+-------------------+------------------+------+-----+---------+-------+
| Field             | Type             | Null | Key | Default | Extra |
+-------------------+------------------+------+-----+---------+-------+
| CustomerID        | int(10) unsigned | NO   | PRI | NULL    |       |
| Address           | varchar(150)     | NO   |     | NULL    |       |
| Email             | varchar(80)      | YES  |     | NULL    |       |
| bussiness_or_home | enum('b','h')    | YES  |     | NULL    |       |
+-------------------+------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

--------------
CREATE TABLE ORDERED(
OrderID INT UNSIGNED AUTO_INCREMENT, 
total_cost VARCHAR(11),
CONSTRAINT ordered_pk PRIMARY KEY(OrderID),
CONSTRAINT ordered_CustomerID_fk FOREIGN KEY(CustomerID) 
REFERENCES CUSTOMER(CustomerID))
--------------

ERROR 1072 (42000): Key column 'CustomerID' doesn't exist in table
--------------
DESCRIBE ORDERED
--------------

ERROR 1146 (42S02): Table 'kiros_acmeonline.ordered' doesn't exist
--------------
CREATE TABLE LINE_ITEM(
Item_Number INT UNSIGNED, 
quantity TINYINT(255),
CONSTRAINT line_item_pk PRIMARY KEY (Item_Number, OrderID),
CONSTRAINT line_item_fk FOREIGN KEY (Item_Number) REFERENCES ITEM(Item_Number),
CONSTRAINT line_item_fk FOREIGN KEY (OrderID) REFERENCES ORDERED(OrderID))
--------------

ERROR 1072 (42000): Key column 'OrderID' doesn't exist in table
--------------
DESCRIBE LINE_ITEM
--------------

ERROR 1146 (42S02): Table 'kiros_acmeonline.line_item' doesn't exist
--------------
CREATE TABLE HOME(
CreditCardNum char(16) NOT NULL,
CreditCardExpirationDate char(6) NOT NULL,
CONSTRAINT home_pk PRIMARY KEY (CustomerID),  
CONSTRAINT home_fk FOREIGN KEY (CustomerID)
REFERENCES CUSTOMER(CustomerID))
--------------

ERROR 1072 (42000): Key column 'CustomerID' doesn't exist in table
--------------
DESCRIBE HOME
--------------

ERROR 1146 (42S02): Table 'kiros_acmeonline.home' doesn't exist
--------------
CREATE TABLE BUSINESS(
PaymentTerms VARCHAR(50) NOT NULL,
CONSTRAINT business_pk PRIMARY KEY (CustomerID),
CONSTRAINT business_fk FOREIGN KEY (CustomerID)
REFERENCES CUSTOMER(CustomerID))
--------------

ERROR 1072 (42000): Key column 'CustomerID' doesn't exist in table
--------------
DESCRIBE BUSINESS
--------------

ERROR 1146 (42S02): Table 'kiros_acmeonline.business' doesn't exist
mysql> 

1 Ответ

0 голосов
/ 19 апреля 2020

При создании внешнего ключа необходимо указать столбец в дополнение к ограничению внешнего ключа. Например, при создании таблицы ORDERED вы должны использовать следующий оператор SQL:

CREATE TABLE ORDERED (
  OrderID INT UNSIGNED AUTO_INCREMENT, 
  total_cost VARCHAR(11),
  CONSTRAINT ordered_pk PRIMARY KEY(OrderID),
  CustomerId int, -- added the column
  CONSTRAINT ordered_CustomerID_fk FOREIGN KEY (CustomerID)
    REFERENCES CUSTOMER(CustomerID)
)

Как вы видите в своем коде, вы забыли добавить столбец CustomerId. * 1006. *

Та же ошибка присутствует в последующих ограничениях.

...