将一个表的id添加到另一个表中以获取特定值mysql

时间:2016-08-09 06:16:51

标签: php mysql

+----+----------+-----+-----------+----------+
| ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
|  2 | Khilan   |  25 | Delhi     |  1500.00 |
|  3 | kaushik  |  23 | Kota      |  2000.00 |
|  4 | Chaitali |  25 | Mumbai    |  6500.00 |
|  5 | Hardik   |  27 | Bhopal    |  8500.00 |
|  6 | Komal    |  22 | MP        |  4500.00 |
|  7 | Muffy    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+

+-----+---------------------+-------------+--------+
| OID | DATE                | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 |             |   3000 |
| 100 | 2009-10-08 00:00:00 |             |   1500 |
| 101 | 2009-11-20 00:00:00 |             |   1560 |
| 103 | 2008-05-20 00:00:00 |             |   2060 |
+-----+---------------------+-------------+--------+

这里我有一个两个表,我想在另一个表中添加一个表客户ID,请帮我这个

1 个答案:

答案 0 :(得分:0)

您可以使用ALTER TABLE 上的 SQL FOREIGN KEY约束来执行此操作。您可以看到文档here

ALTER TABLE first_table_name
ADD FOREIGN KEY (id)
REFERENCES second_table_name(CUSTOMER_ID)

它应该有用。