有没有办法在SQL数据库的字段中插入多个数据?

时间:2016-09-30 10:32:10

标签: mysql sql database

我想在let dict = self.arr.objectAtIndex(indexPath.row) as! NSMutableDictionary let 2ndview = self.storyboard?.instantiateViewControllerWithIdentifier("2ndview") as! viewcontroller 2ndview.dict = dict self.navigationController?.pushViewController(2ndview, animated: true) 列下的course字段中插入多个student_id,其中每个值都与course_taken分开。

','

有可能吗?

1 个答案:

答案 0 :(得分:4)

course_taken应该是M:Nstudent之间的course关系。它是在关系数据库中使用单独的表实现的,而不是内联数据。

简单地说:

CREATE TABLE course (
    id int NOT NULL PRIMARY KEY autoincrement,
    course_code varchar NOT NULL
);
CREATE TABLE student (
    id int NOT NULL PRIMARY KEY autoincrement,
    full_name varchar NOT NULL
);
CREATE TABLE course_taken (
    course_id int NOT NULL,
    student_id int NOT NULL,
    CONSTRAINT pk_course_taken
        PRIMARY KEY (course_id, student_id),
    CONSTRAINT fk_course_taken_course_id 
        FOREIGN KEY (course_id)
        REFERENCES course (id)
        ON DELETE CASCADE,
    CONSTRAINT fk_course_taken_student_id 
        FOREIGN KEY (student_id)
        REFERENCES student (id)
        ON DELETE CASCADE
);

要回答您的问题,是的,您可以将这些数据内联存储在某些文本字段中。但你真的不应该。