0

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

Ankit4mjis
  • 75
  • 3
  • 10
  • Possible duplicate of [Multiple-column foreign key in MySQL?](https://stackoverflow.com/questions/953035/multiple-column-foreign-key-in-mysql) – Robert Kock Oct 11 '18 at 16:39
  • Making marks as foreign key does not make sense – Madhur Bhaiya Oct 11 '18 at 16:41
  • Can you post your certificates table columns and data? I think all you need is a simple join on rollNo whilst you will have to keep rollNo field as FK in certificate table – Pushpesh Kumar Rajwanshi Oct 11 '18 at 16:45
  • @MadhurBhaiya I might be wrong in terms of sql stuff but I want both the data coming together. – Ankit4mjis Oct 11 '18 at 16:49
  • @PushpeshKumarRajwanshi check the data – Ankit4mjis Oct 11 '18 at 16:59
  • @Ankit4mjis Why would `marks` be in `Students` table? What happen if a student takes more than one course? Which mark are you going to put in `Students` table? It doesn't make sense to put `marks` in `Students` table. – Eric Oct 11 '18 at 17:04
  • @Eric consider that marks as full marks. and I only want to understand that my above requirement is possible or not. Or am I missing anything?? I'm a bit learning the SQL, so please don't mind it. – Ankit4mjis Oct 11 '18 at 17:29

1 Answers1

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.

Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36