尝试更新作为主键和外键的数据库属性到其他表

zbq4xfa0  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(300)

尝试更新skill表sname列中的属性,但不允许更新,因为spossed和srequired表中存在外键约束。
这些外键也被声明为主键约束,因此不能为null。我该怎么办?
以下是表格:

CREATE TABLE SKILL(
sname         VARCHAR(30)     NOT NULL, /* Skill name */
CONSTRAINT SKILL_pkey PRIMARY KEY ( sname ) );

CREATE TABLE SREQUIRED(
sname         VARCHAR(30)     NOT NULL, /* Skill name*/
requires      VARCHAR(30)     NOT NULL, /* Skill required*/
slevel        DECIMAL(2)      NOT NULL, /* Level required*/
CONSTRAINT SREQUIRED_pkey PRIMARY KEY ( sname, requires ),
CONSTRAINT SREQUIRED_fkey1 FOREIGN KEY ( sname) REFERENCES SKILL( sname ),
CONSTRAINT SREQUIRED_fkey2 FOREIGN KEY ( requires ) REFERENCES SKILL( sname ) );

CREATE TABLE SPOSSESSED(
anumber     DECIMAL(6)  NOT NULL, /* Applicant number*/
sname       VARCHAR(30) NOT NULL, /* Skill name*/
slevel      DECIMAL(2)  NOT NULL, /* Skill level*/
CONSTRAINT SPOSSESSED_pkey PRIMARY KEY ( anumber, sname ), 
CONSTRAINT SPOSSESSED_fkey1 FOREIGN KEY ( anumber ) REFERENCES APPLICANT ( anumber ) ON DELETE CASCADE,
CONSTRAINT SPOSSESSED_fkey2 FOREIGN KEY ( sname )REFERENCES SKILL ( sname ),
CONSTRAINT SPOSSESSED_check1 CHECK ( slevel IN ( 1,2,3,4,5,6,7,8,9,10 ) ) );
ig9co6j1

ig9co6j11#

有两种可能的解决办法
在skill表中创建id列,并将其他表中的id列用作外键。
在外键上添加级联更新规则。

相关问题