如何使用外键

时间:2015-08-11 06:48:14

标签: ios sqlite join foreign-keys

create table  studentsDetail (
    student_id integer primary key,
    rollno text,
    name text,
    stream text,
    school text
);

create table studentsInfo (
    studentinfo_id integer primary key,
    stdetail_info_id integer,
    fathername  text,
    mothername text,
    age text,
    address text,
    FOREIGN KEY(stdetail_info_id) REFERENCES studentsDetail(student_id)
);

我想使用外键从两个表中获取记录。我怎么能轻易做到?

1 个答案:

答案 0 :(得分:3)

这样的事情:

SELECT 
  studentsDetail.name, studentsDetail.school,
  studentsInfo.age, studentsInfo.address
FROM
  studentsDetail INNER JOIN studentsInfo
    ON studentsDetail.student_id = studentsInfo.stdetail_info_id