使用外键更新表
Update tables with foreign keys
我是 mysql 的新手,为了创建一个简单的数据库,我已经为 3 table 制定了以下规范:
DROP TABLE IF EXISTS representative;
CREATE TABLE representative(
id_representative VARCHAR(15),
name VARCHAR(15),
PRIMARY KEY(id_representative)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS congress;
CREATE TABLE congress(
id_congress INT,
PRIMARY KEY(id_congress)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS representative_congress;
CREATE TABLE representative_congress(
id_ai INT AUTO_INCREMENT,
id_representative VARCHAR(15),
id_congress INT,
party VARCHAR(2),
PRIMARY KEY(id_ai),
FOREIGN KEY(id_congress) REFERENCES congress(id_congress) ON UPDATE CASCADE,
FOREIGN KEY(id_representative) REFERENCES representative(id_representative) ON UPDATE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
代表和国会 table 中的信息一旦插入就不会改变(对于两个不同的代表,我不会有两个相等的 id_representative,或者两个相等的 id_congress 两次不同的大会)。但是 representative_congress table 用于以下情况:在同一个大会(时间跨度为 2 年)中,给定的代表将更换政党(或者甚至可以添加更多字段到 table 在未来),所以我会为同一个大会将相同的 id_representative 链接到不同的政党,甚至更多的领域。
考虑到这一点,我有 2 个问题:
我是否需要向 table 的规范或插入查询添加任何约束?请记住,信息将被添加到 table 中一次,只有当 tables/database 被删除时才会被更改。
有没有办法只用一个 INSERT 查询,插入
同时属于两个不同 table 的信息,例如某种级联命令?比如id_representative,它既属于代表又属于representative_congresstable
提前致谢
约束很好,representative和congress表的主键保证了信息的完整性,representative_congress中的外键防止了错误的关系,唯一的考虑是不能' t insert into representative_congress before to insert the id's in the previous tables.
在两个(或更多)表中插入数据的方法只有一个插入语句是使用触发器,但是您需要定义一个默认插入或插入逻辑,例如
CREATE TRIGGER trigger_name
AFTER INSERT
ON representative FOR EACH ROW
insert into representative_congress (id_representative, id_congress, party)
values (NEW.id_representative, 1, '01')
我是 mysql 的新手,为了创建一个简单的数据库,我已经为 3 table 制定了以下规范:
DROP TABLE IF EXISTS representative;
CREATE TABLE representative(
id_representative VARCHAR(15),
name VARCHAR(15),
PRIMARY KEY(id_representative)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS congress;
CREATE TABLE congress(
id_congress INT,
PRIMARY KEY(id_congress)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS representative_congress;
CREATE TABLE representative_congress(
id_ai INT AUTO_INCREMENT,
id_representative VARCHAR(15),
id_congress INT,
party VARCHAR(2),
PRIMARY KEY(id_ai),
FOREIGN KEY(id_congress) REFERENCES congress(id_congress) ON UPDATE CASCADE,
FOREIGN KEY(id_representative) REFERENCES representative(id_representative) ON UPDATE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
代表和国会 table 中的信息一旦插入就不会改变(对于两个不同的代表,我不会有两个相等的 id_representative,或者两个相等的 id_congress 两次不同的大会)。但是 representative_congress table 用于以下情况:在同一个大会(时间跨度为 2 年)中,给定的代表将更换政党(或者甚至可以添加更多字段到 table 在未来),所以我会为同一个大会将相同的 id_representative 链接到不同的政党,甚至更多的领域。
考虑到这一点,我有 2 个问题:
我是否需要向 table 的规范或插入查询添加任何约束?请记住,信息将被添加到 table 中一次,只有当 tables/database 被删除时才会被更改。
有没有办法只用一个 INSERT 查询,插入 同时属于两个不同 table 的信息,例如某种级联命令?比如id_representative,它既属于代表又属于representative_congresstable
提前致谢
约束很好,representative和congress表的主键保证了信息的完整性,representative_congress中的外键防止了错误的关系,唯一的考虑是不能' t insert into representative_congress before to insert the id's in the previous tables.
在两个(或更多)表中插入数据的方法只有一个插入语句是使用触发器,但是您需要定义一个默认插入或插入逻辑,例如
CREATE TRIGGER trigger_name
AFTER INSERT
ON representative FOR EACH ROW
insert into representative_congress (id_representative, id_congress, party)
values (NEW.id_representative, 1, '01')