Sql DB2子表不工作-创建表
我面前有一项任务。(DB2数据库) 我需要创建一个表,它将是一个子表(这就是SQL中所称的表吗?) 我需要它,以便它对我的另一个表具有外键约束,因此当父表被修改(记录被删除)时,子表也会丢失该记录 一旦我有了这个表,我还需要用另一个表中的数据填充它(如果有一种简单的方法来更新这个表的话) 如果你能给我指出正确的方向,这会很有帮助,因为我甚至不知道该找什么语法 我的表格如下:Sql DB2子表不工作-创建表,sql,db2,Sql,Db2,我面前有一项任务。(DB2数据库) 我需要创建一个表,它将是一个子表(这就是SQL中所称的表吗?) 我需要它,以便它对我的另一个表具有外键约束,因此当父表被修改(记录被删除)时,子表也会丢失该记录 一旦我有了这个表,我还需要用另一个表中的数据填充它(如果有一种简单的方法来更新这个表的话) 如果你能给我指出正确的方向,这会很有帮助,因为我甚至不知道该找什么语法 我的表格如下: create table titleauthors ( au_id char(11), tit
create table titleauthors (
au_id char(11),
title_id char(6),
au_ord integer,
royaltyshare decimal(5,2));
我正在创建的表:
create table titles (
title_id char(6),
title varchar(80),
type varchar(12),
pub_id char(4),
price decimal(9,2),
advance decimal(9,2),
ytd_sales integer,
contract integer,
notes varchar(200),
pubdate date);
我需要将title_id与父表中的title_id匹配,并在从中删除该表时使用ON DELETE级联语法进行删除
我的尝试:
CREATE TABLE BookTitles (
title_id char(6) NOT NULL CONSTRAINT BookTitles_title_id_pk REFERENCES titleauthors(title_id) ON DELETE CASCADE,
title varchar(80) NOT NULL,
type varchar(12),
pub_id char(4),
price decimal(9,2),
advance decimal(9,2),
ytd_sales integer,
contract integer,
notes varchar(200),
pubdate date)
;
首先,有几点意见:
假设您希望颠倒父/子关系(书名为父,标题作者为子): 如果从书名中删除标题,这将导致标题作者中的记录被删除
CREATE TABLE BookTitles (
title_id char(6) NOT NULL,
title varchar(80) NOT NULL,
type varchar(12),
pub_id char(4),
price decimal(9,2),
advance decimal(9,2),
ytd_sales integer,
contract integer,
notes varchar(200),
pubdate date
);
alter table booktitles
add primary key (title_id);
alter table titleauthors
add constraint fk_booktitles
foreign key (title_id)
references booktitles (title_id)
on delete cascade;