[英]sql table with primary key from another table
This question are going to probably be very basic but I am new to SQL. 这个问题可能很基本,但是我是SQL新手。 I am trying to create a new table based of a previous table that will use the primary key from the orinal table and display the value of that id.
我试图基于先前的表创建一个新表,该表将使用原始表中的主键并显示该ID的值。
Table_1: 表格1:
Field Type Null Key Default Extra
----- ---- ---- --- ------- -----
ID int(11) NO PRI auto_increment
fruit varchar(255) NO
The results are: 结果是:
ID fruit
-- -----
1 Apple
2 Orange
3 Pear
4 Grape
5 BlueBerry
6 StrwBerry
7 Kiwi
For table two I would like to be able to have a primary_id, table_1_id, table_1_name, color, location. 对于第二个表,我希望能够具有primary_id,table_1_id,table_1_name,颜色,位置。
Something like this: 像这样:
ID table_1_id table_1_fruit_name color location
-- ---------- ------------------ ----- --------
1 1 Apple Red Farm
2 1 Apple Green Store
3 2 Orange Orange Store
4 4 Grape Green Farm
5 4 Grape Green Store
I am getting lost on how to create this. 我迷路于如何创建它。 I have tried using constraints ( assuming that is what I should use ).
我尝试使用约束(假设这是我应该使用的)。
Something like this 像这样
CREATE TABLE mynewtable
(
location varchar(255),
color varchar(255),
PRIMARY KEY (id),
FOREIGN KEY (table_1_id) REFERENCES table_1(id),
FOREIGN KEY (table_1_name) REFERENCES table_1(name),
)
you can do like this 你可以这样
CREATE TABLE secondtable
(
PRIMARY KEY (id),
location varchar(50),
color varchar(50),
FOREIGN KEY (table1_id) REFERENCES table1(id),
FOREIGN KEY (table1_name) REFERENCES table1(name),
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.