简体   繁体   English

带有来自另一个表的主键的sql表

[英]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.

相关问题 SQL-显示表中的记录,其中外键值与另一个表中的主键值匹配 - SQL - Displaying records from a table where the foreign key value matches the primary key value in another table 根据另一个表的主键创建主键 - Creating primary key based off of a primary key from another table 在 SQL 中是否有任何约束,我在其中添加一个作为外键的值到一个表,它从另一个表中删除该值(主键)? - Is there any constraint in SQL where I add a value that is a foreign key to one table, it removes that value(primary key) from another table? MySQL - 将主键从一个表插入另一个表(外键) - MySQL - Inserting Primary Key from one table to another (Foreign Key) 如何从另一个表主键添加mysql外键? - How to Add mysql foreign key from another table primary key? SQL:仅基于主键列将缺失的行从一个表复制到另一个表 - SQL: Copy missing rows from one table to another based on primary key column only 标准化SQL-根据来自另一个表的匹配字符串将列设置为主键标识符 - Normalize SQL - Set column to primary key identifier based on matching string from another table 使用附加主键将行从一个表复制到另一个表 - Copy row from one table to another with a additional primary key 如何从一个表调用主键到另一个php mysql - how to call a primary key from a table to another one in php mysql MYSQL-使用复合主键从另一个表更新表 - MYSQL - update a table from another with composite primary key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM