简体   繁体   English

SQL一对多关系

[英]SQL One-To-Many relationship

I'm new to SQL and I want to create a One-To-Many relationship between two tables. 我是SQL新手,我想在两个表之间创建One-To-Many关系。 I have these two tables created with the following queries: 我使用以下查询创建了这两个表:

CREATE TABLE Customers
(
    CustomerId INT NOT NULL AUTO_INCREMENT,
    FirstName VARCHAR(255) NOT NULL,
    LastName VARCHAR(255) NOT NULL,
    Email VARCHAR(255) NOT NULL,
    Address VARCHAR(255) NOT NULL,
    PRIMARY KEY(CustomerId)
);

CREATE TABLE Orders
(
    OrderId INT NOT NULL AUTO_INCREMENT,
    Date DATE NOT NULL,
    Quantity INT NOT NULL,
    TotalDue FLOAT NOT NULL,
    CustomerId INT NOT NULL,
    PRIMARY KEY(OrderId),
    FOREIGN KEY(CustomerId) REFERENCES Customers(CustomerId)
);

However even though I set CustomerId as a foreign key for the Orders table I'm still able to add rows in the Orders table with a CustomerId that is not present in the Customers table. 然而,即使我设置CustomerId作为一个外键Orders表,我仍然能够在添加行Orders表与CustomerId是不存在的Customers表。 Why is that and how can I create a real link between the tables? 为什么会这样,如何在表之间创建真正的链接?

This is what the tables look like(copy/paste from my mysql client): 这是表的样子(从我的mysql客户端复制/粘贴):

mysql> select * from Customers;
+------------+-----------+----------+-------------------+------------------------+
| CustomerId | FirstName | LastName | Email             | Address                |
+------------+-----------+----------+-------------------+------------------------+
|          1 | Jacks     | James    | james98@yahoo.com | Str. Moony, No. 9      |
|          2 | Mock      | Grad     | rrfuX@yahoo.com   | Str. Mars, No. 91      |
|          3 | James     | Geremy   | gv@yahoo.com      | Str. Monday, No. 12    |
|          4 | Joana     | Joan     | iiogn@yahoo.com   | Str. Comete, No. 19    |
|          5 | Granicer  | James    | gtuawr@yahoo.com  | Str. Sydney, No. 651   |
+------------+-----------+----------+-------------------+------------------------+
5 rows in set (0.00 sec)

mysql> select * from Orders;
+---------+------------+----------+----------+------------+
| OrderId | Date       | Quantity | TotalDue | CustomerId |
+---------+------------+----------+----------+------------+
|       1 | 2014-01-09 |       10 |      340 |          3 |
|       2 | 2014-01-09 |        1 |       50 |          3 |
|       3 | 2014-01-09 |       11 |       55 |          5 |
|       4 | 2014-01-09 |       11 |       55 |         51 |
+---------+------------+----------+----------+------------+
4 rows in set (0.00 sec)

As you can see OrderId 4 contains a customer with CustomerId 51 which is not in the Customers table. 如您所见, OrderId 4包含一个CustomerId 51的客户,不在Customers表中。

You need to set the storage engine...change your query to... 您需要设置存储引擎...将查询更改为...

CREATE TABLE Orders
(
    OrderId INT NOT NULL AUTO_INCREMENT,
    Date DATE NOT NULL,
    Quantity INT NOT NULL,
    TotalDue FLOAT NOT NULL,
    CustomerId INT NOT NULL,
    PRIMARY KEY(OrderId),
    FOREIGN KEY(CustomerId) REFERENCES Customers(CustomerId)
)ENGINE=INNODB;

将InnoDB用于表引擎。

ALTER TABLE table_name ENGINE=InnoDB;

我放弃并开始使用MySQL的Web客户端...

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

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