简体   繁体   English

父子表sql问题

[英]parent child table sql issue

i have 3 tables below which have many to many relation...In student table i have multiple foreign keys sec_id,ad_id but i dont know how to add foreign key as parent-child relation please help me out.. 我下面有3个表,其中有许多关系...在学生表中,我有多个外键sec_id,ad_id但我不知道如何添加外键作为父子关系,请帮帮我。

CREATE TABLE student(
s_id int AUTO_INCREMENT,
name varchar(30) NOT NULL,
PRIMARY KEY(s_id)
)

CREATE TABLE section(
sec_id int AUTO_INCREMENT,
name varchar(2) NOT NULL,
PRIMARY KEY(sec_id)
)


CREATE TABLE advisor(
ad_id int AUTO_INCREMENT,
name varchar(2) NOT NULL,
PRIMARY KEY(ad_id)
)

If a student can have at most one advisor , then it's a one-to-many relationship. 如果一个学生最多只能有一位 顾问 ,那就是一对多的关系。

The normal pattern for implementing that relationship is to define a foreign key column in the child table, the table on the "many" side of the relationship. 实现该关系的正常模式是在子表中定义外键列,该表位于关系的“许多”侧。 For example: 例如:

ALTER TABLE student ADD ad_id INT COMMENT 'fk ref advisor';

Further, some storage engines, like InnoDB, support and enforce foreign keys. 此外,某些存储引擎(例如InnoDB)支持并强制使用外键。 That is, we can have the database enforce restrictions (constraints) on values that can be stored in columns defined as foreign keys. 也就是说,我们可以让数据库对可以存储在定义为外键的列中的值实施限制(约束)。

For example, we can establish a rule that says that a value stored in the ad_id column in the student table... must be found in a row in the advisor table, in the ad_id column. 例如,我们可以建立一条规则,说存储在student表的ad_id列中的值... 必须advisor表的ad_id列中的一行中找到。

For example: 例如:

ALTER TABLE student ADD CONSTRAINT fk_student_advisor 
  FOREIGN KEY (ad_id) REFERENCES advisor(ad_id) 
  ON DELETE RESTRICT ON UPDATE CASCADE 

This also enforces a rule that a row from advisor table cannot be deleted if there are rows in student table that reference it. 这也强制执行一个规则,即如果student表中有引用它的行,则不能删除advisor表中的行。

That same pattern can be repeated for any one to-to-many relationship. 对于任何一对多关系,都可以重复相同的模式。 For example, if a student can be related to at most one section we can add a foreign key in the same way. 例如,如果一个学生可以与最多一个部分 ,我们可以添加以同样的方式外键。

If there's a many-to-many relationship, then we introduce a relationship table that has foreign keys referencing the two related entity tables. 如果存在多对多关系,则我们将引入一个关系表,该表具有引用两个相关实体表的外键。

If a student can be related to zero, one or more section , and a section can be related to zero, one or more student , that's an example of a many-to-many relationship. 如果一个学生可以与零个,一个或多个部分相关 ,而一个部分可以与零个,一个或多个学生相关 ,这就是多对多关系的一个示例。

We can introduce a new table like this (as an example): 我们可以像这样引入一个新表(作为示例):

CREATE TABLE student_section 
( student_id  INT NOT NULL COMMENT 'pk, fk ref student'
, section_id  INT NOT NULL COMMENT 'pk, fk ref section'
, PRIMARY KEY (student_id, section_id)
, CONSTRAINT fk_student_section_student
    FOREIGN KEY student_id REFERENCES student(s_id) 
    ON DELETE CASCADE ON UPDATE CASCADE
, CONSTRAINT fk_student_section_section
    FOREIGN KEY section_id REFERENCES section(sec_id) 
    ON DELETE CASCADE ON UPDATE CASCADE
)

With that in place, to establish a relationship between a student and a section , we insert a row to the new student_section table. 有了这个位置,要在学生之间建立关系,我们在新的student_section表中插入一行。 If the student is related to another section , we add another row to the table, referencing the same student, but a different section. 如果该学生与另一部分相关,我们将在表中添加另一行,以引用同一学生,但引用另一部分。

The value stored in the student_id column refers to a row in the student table; student_id列中存储的值引用了student表中的一行; and a value stored in the section_id column refers to a row in the section table. section_id列中存储的值引用section表中的一行。

(The many-to-many relationship is really composed of rows in a new table that has a one-to-many relationship to two other tables.) (多对多关系实际上是由新表中的行组成的,该新表与其他两个表具有一对多关系。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM