代码之家  ›  专栏  ›  技术社区  ›  Ankit4mjis

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

  •  0
  • Ankit4mjis  · 技术社区  · 6 年前

    我有如下表格

    我想使用SQL查询创建一个新表,其中它将具有作为主键自动递增的 certid as primary key auto incremented和rollno and marks should come from students table as foreign key(correct me if I am wrong),如下所示:

    .

    我想创建一个新表 证书 使用SQL查询 宫颈癌 作为主键,自动递增,rollno和标记应作为外键(如果我错了,请更正我)从学生表中获得,如下所示:

    1 回复  |  直到 6 年前
        1
  •  1
  •   Pushpesh Kumar Rajwanshi    6 年前

    您必须创建这样的表证书,

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

    然后使用此命令,您可以将所有数据从students表复制到certificates表,

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

    如果你需要的话,告诉我,如果有什么问题的话。