简体   繁体   English

将表中的两列添加为另一个表中的外键

[英]Add two columns from a table as a foreign key in another

I have students table as below 我有学生表如下

在此处输入图片说明

I want to create a new table certificates using SQL query where it will have certid as primary key auto incremented and rollno and marks should come from students table as foreign key (correct me if I am wrong) like below: 我想使用SQL查询创建一个新的表证书 ,其中的certid将作为主键自动递增,并且rollno和标记应来自学生表作为外键(如果我错了,请纠正我),如下所示:

在此处输入图片说明

You have to create table certificates like this, 您必须像这样创建表证书,

create table certificates (
    certId int auto_increment primary key,
    rollNo int,
    marks int,
    FOREIGN KEY (rollNo) REFERENCES students(rollNo)
);

Then using this command you can copy all data from students table to certificates table, 然后,您可以使用此命令将所有数据从学生表复制到证书表,

insert into certificates (rollNo,marks) select rollNo,marks from students;

Let me know if you needed this and have any issues doing it. 让我知道您是否需要这样做,是否有任何问题。

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

相关问题 一个表中的两列是否可以具有另一表中同一列的外键? - Can two columns from one table have a foreign key to the same column in another table? 如何从另一个表主键添加mysql外键? - How to Add mysql foreign key from another table primary key? 将两个相同的外键从同一个表转换为另一个表? - Two of the same foreign key from the same table into another table? 在另一个表的主键;外键列中插入记录 - Inserting records in a primary;foreign key columns from another table 按条件从一列到不同表的两列的外键 - foreign key from one column to two columns of different table by condition 两个不同外键列的同一个表的外键关系 - Foreign key relationships with the same table for two different foreign key columns 同一表中的两列和同一外键 - Two columns in same table and same foreign key Mysql将表1中的同一列作为外键引用到表2中的两列 - Mysql referencing the same column from table 1 as foreign key to two columns in table two SQL从一个表中获取所有列,以及从另一个表中通过外键关联的分组列的计数 - SQL get all columns from a table along with the count of a grouped column from another table related by a foreign key 如何从两个表中查询匹配表中的外键和另一个表中的主键 - How do I query from two tables matching the foreign key from table with the primary key from another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM