-1

我有 2 个表 coursemaster 和 course subject 在 course master 我有 2 列 course code 和 course name 在 course subject 我有 course code 和 subject code。我的目标是如果我删除 course master 中的课程代码它不应该删除,如果我更新它应该得到更新并反映在课程主题中。请告诉我如何为此编写查询。

我的课程主表结构

#   Name    Type    Collation   Attributes  Null    Default Extra   Action
     1  id  int(11)         No  None         Change Change   Drop Drop   Browse distinct values Browse distinct values   Primary Primary     Unique Unique   Index Index    Spatial Spatial Fulltext Fulltext
     2  course_code varchar(255)    latin1_swedish_ci       No  None         Change Change   Drop Drop   Browse distinct values Browse distinct values  Primary Primary  Unique Unique   Index Index    Spatial Spatial Fulltext Fulltext
     3  course_name varchar(255)    latin1_swedish_ci       No  None         Change Change   Drop Drop   Browse distinct values Browse distinct values   Primary Primary     Unique Unique   Index Index    Spatial Spatial Fulltext Fulltext

课程科目结构

Name    Type    Collation   Attributes  Null    Default Extra   Action
 1  id  int(11)         No  None    AUTO_INCREMENT   Change Change   Drop Drop   Browse distinct values Browse distinct values  Primary Primary  Unique Unique   Index Index    Show more actions More
 2  course_code varchar(255)    latin1_swedish_ci       No  None         Change Change   Drop Drop   Browse distinct values Browse distinct values   Primary Primary     Unique Unique   Index Index    Show more actions More
 3  subject_code    varchar(255)    latin1_swedish_ci       No  None         Change Change   Drop Drop   Browse distinct values Browse distinct values   Primary Primary     Unique Unique   Index Index    Show more actions More
4

2 回答 2

0

看来您需要在表之间建立关系。

ALTER TABLE 'master' ADD PRIMARY KEY ('course_code');
ALTER TABLE 'subject' ADD FOREIGN KEY ('course_code') REFERENCES 'master' ('course_code');
于 2013-11-02T05:31:27.893 回答
0
CREATE TABLE Course
(
id int NOT NULL,
name varchar(255) NOT NULL,
code varchar(255) NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE Subject
(
id int NOT NULL,
code varchar(255) NOT NULL,
CourseId int NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (CourseId) REFERENCES Course(id)
);
于 2013-11-02T05:25:29.593 回答