資料庫一對一 一對多 多對多關係

2021-10-06 20:07:52 字數 2729 閱讀 2433

參考:

自己在專案中寫的例項:

『實體』和『公理』具有多對多關係,即乙個實體可以對應多個公理,乙個公理也可以包含多個實體。

多對多關係需要一張紐帶表來實現。

// 實體**

create table if not exists `entity_management`

( `id` int unsigned not null auto_increment comment 'id',

`entity_string` varchar(100) character set utf8 collate utf8_general_ci not null comment '實體字串',

`entity_type` varchar(100) character set utf8 collate utf8_general_ci not null comment '實體型別',

`create_time` timestamp not null default current_timestamp comment '建立時間',

`update_time` timestamp not null default current_timestamp comment '更新時間',

primary key (`id`),

unique key `entity_management_name_uindex` (`entity_string`)

) engine = innodb

default charset = utf8mb4;

# // 公理**,公理表示實體之間的關係。實體和公理之間存在多對多關係

create table if not exists `axiom_management`

( `id` int unsigned not null auto_increment comment 'id',

`axiom_string` varchar(100) character set utf8 collate utf8_general_ci not null comment '公理對應字串',

`create_time` timestamp not null default current_timestamp comment '建立時間',

`update_time` timestamp not null default current_timestamp comment '更新時間',

primary key (`id`),

unique key `entity_management_name_uindex` (`axiom_string`)

) engine = innodb

default charset = utf8mb4;

# // 實體和公理之間的紐帶表

create table if not exists `entitymapaxiom_management`

( `id` int unsigned not null auto_increment comment '關係id',

`entity_id` int unsigned not null auto_increment comment '實體id',

`axiom_id` int unsigned not null auto_increment comment '公理id',

`create_time` timestamp not null default current_timestamp comment '建立時間',

`update_time` timestamp not null default current_timestamp comment '更新時間',

primary key (`id`),

key `entity_id` (`entity_id`),

key `axiom_id`(`axiom_id`),

constraint `entity_id` foreign key (`entity_id`) references `entity_management`(`id`),

constraint `axiom_id` foreign key (`axiom_id`) references `axiom_management`(`id`)

) engine = innodb

default charset = utf8mb4;

資料庫一對一 一對多 多對多關係

資料庫一對 一 一對多 多對多關係 本來資料庫一對 一 一對多 多對多關係並不複雜,但是最近在理解的時候感覺又感覺多了寫新意,所以現在在來總結一下吧 一 首先給出三種關係的例項 1 一對一關係例項 乙個人對應一張身份證,一張身份證對應乙個人 2 一對多關係例項 乙個班級擁有多個學生,乙個學生只能夠屬...

資料庫一對一,一對多,多對多關係

關聯對映 一對多 多對一 存在最普遍的對映關係,簡單來講就如球員與球隊的關係 一對多 從球隊角度來說乙個球隊擁有多個球員 即為一對多 多對一 從球員角度來說多個球員屬於乙個球隊 即為多對一 資料表間一對多關係如下圖 關聯對映 一對一 一對一關係就如球隊與球隊所在位址之間的關係,一支球隊僅有乙個位址,...

efcore 一對一 一對多 多對多關係

public class user entity public string email public string phonenumber required datatype datatype.password public string password public datetime birt...