云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > DBDesigner+and+MySqlWorkbench+Relations+and+Relationships+implementation+01 - 图文

DBDesigner+and+MySqlWorkbench+Relations+and+Relationships+implementation+01 - 图文

  • 62 次阅读
  • 3 次下载
  • 2025/6/16 3:54:33

Many to Many

When making the Many to Many relationship, DBD4 automatically inserts an Association table between Table_01 and Table_02. It is named Table_01_has_Table_02.

The primary key from Table_01 and the primary key from Table_02 are inserted as both Primary and Foreign keys in the Association table.

The resulting SQL:

CREATE TABLE Table_02 (

d INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, e INTEGER UNSIGNED NULL, f INTEGER UNSIGNED NULL, PRIMARY KEY(d) )

TYPE=InnoDB;

CREATE TABLE Table_01 (

a INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, b INTEGER UNSIGNED NULL, c INTEGER UNSIGNED NULL, PRIMARY KEY(a) )

TYPE=InnoDB;

CREATE TABLE Table_01_has_Table_02 ( Table_01_a INTEGER UNSIGNED NOT NULL, Table_02_d INTEGER UNSIGNED NOT NULL, PRIMARY KEY(Table_01_a, Table_02_d), FOREIGN KEY(Table_01_a) REFERENCES Table_01(a) ON DELETE NO ACTION ON UPDATE NO ACTION, FOREIGN KEY(Table_02_d) REFERENCES Table_02(d) ON DELETE NO ACTION ON UPDATE NO ACTION )

TYPE=InnoDB;

Note: Table_02 in the figure has a mistake. The attributes are d, e and f, not a.

? Per Dahlstr?m 12-07-2013 Page 9 of 21

One to One

Note how d, e and f are all set to be non key attributes.

When making the One to One relationship, DBD4 automatically inserts the primary key from

Table_01 in Table_02 as a new attribute and uses it as the foreign key and primary key in Table_02 as well.

Values in column a in Table_01 can only occur once due to the PK constraint. Like vice values in column Table_01_a in Table_02 can only occur once due to the PK constraint and must be present in column a in Table_01 due to the FK constraint. The one and only one to one and only one relationship is thus assured.

CREATE TABLE Table_01 (

a INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, b INTEGER UNSIGNED NULL, c INTEGER UNSIGNED NULL, PRIMARY KEY(a) )

TYPE=InnoDB;

CREATE TABLE Table_02 (

d INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, Table_01_a INTEGER UNSIGNED NOT NULL, e INTEGER UNSIGNED NULL, f INTEGER UNSIGNED NULL, PRIMARY KEY(d, Table_01_a), FOREIGN KEY(Table_01_a) REFERENCES Table_01(a) ON DELETE NO ACTION ON UPDATE NO ACTION )

TYPE=InnoDB;

? Per Dahlstr?m 12-07-2013 Page 10 of 21

Self referencing relationship(s)

The intention is to create two self referencing relationship for Table_01. The following SQL statement is generated. It is a legal SQL statement.

CREATE TABLE Table_01 (

a INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, Table_01_a INTEGER UNSIGNED NOT NULL, b INTEGER UNSIGNED NULL, c INTEGER UNSIGNED NULL, PRIMARY KEY(a),

FOREIGN KEY(Table_01_a) REFERENCES Table_01(a), FOREIGN KEY(Table_01_a) REFERENCES Table_01(a) )

TYPE=InnoDB;

But the statement only creates one FK and do thus not created the wanted two constraints.

This can be fixed by hand:

CREATE TABLE Table_01 (

a INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, Table_01_x INTEGER UNSIGNED NOT NULL,

Table_01_y INTEGER UNSIGNED NOT NULL, /* added by hand */ b INTEGER UNSIGNED NULL, c INTEGER UNSIGNED NULL, PRIMARY KEY(a),

FOREIGN KEY(Table_01_x) REFERENCES Table_01(a), FOREIGN KEY(Table_01_y) REFERENCES Table_01(a) )

TYPE=InnoDB;

The attribute Table_01_a is renamed to Table_01_x and attribute Table_01_y is added by hand. They are then both declared as FK.

? Per Dahlstr?m 12-07-2013 Page 11 of 21

In Phpmyadmin the table ends up looking like this:

The underlined a is the PK.

When inserting a value into the FKs, Table_01_x and Table_01_y, this is only possible if a already contains the value.

E.g., if no values have yet been entered into the table, so attribute a holds no values, the following insert attempt fails as a is 10, and 11 is inserted into Table_01_y.

But this will work as a first entry:

? Per Dahlstr?m 12-07-2013 Page 12 of 21

搜索更多关于: DBDesigner+and+MySqlWorkbench+ 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

Many to Many When making the Many to Many relationship, DBD4 automatically inserts an Association table between Table_01 and Table_02. It is named Table_01_has_Table_02. The primary key from Table_01 and the primary key from Table_02 are inserted as both Primary and Foreign keys in the Association table. The resulting SQL: CREATE TABLE Table_02 ( <

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com