我有2个表课程和课程主题课程硕士我有2列课程代码和课程名称和课程主题我有课程代码和主题代码。我的目标是,如果我删除课程代码在课程硕士不应该删除如果我更新它应该得到更新并反映在课程subject.pls告诉我如何写这个查询。
我的课程主表格结构
# 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
答案 0 :(得分: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)
);
答案 1 :(得分:0)
这似乎需要在表之间建立关系。
ALTER TABLE 'master' ADD PRIMARY KEY ('course_code');
ALTER TABLE 'subject' ADD FOREIGN KEY ('course_code') REFERENCES 'master' ('course_code');