Add two columns from a table as a foreign key in another

Ankit4mjis Source

I have students table as below

enter image description here

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:

enter image description here

mysqlsqldatabase

Answers

answered 6 days ago Pushpesh Kumar Rajwanshi #1

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.

comments powered by Disqus