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