Ankit4mjis
Ankit4mjis

Reputation: 75

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

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

Upvotes: 0

Views: 38

Answers (1)

Pushpesh Kumar Rajwanshi
Pushpesh Kumar Rajwanshi

Reputation: 18357

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.

Upvotes: 1

Related Questions