简体   繁体   English

sql中创建主键的问题

[英]Problems with creating primary keys in sql

I have table that has null type of data and it need to be null我有一个数据类型为空的表,它需要为空

replacement parts 0,m-> Repair更换零件 0,m-> 修理

So replacement parts can be part of repair but every repair don't need to have replacement parts and when I am creating tables I putted for replacement parts id null, but problem is when I am creating primary key it can be only with not null.因此,更换零件可以是维修的一部分,但每次维修都不需要更换零件,当我创建表时,我将更换零件 ID 置为空,但问题是当我创建主键时,它只能不为空。 What to do?该怎么办?

The question describes only one half of the relation between repairs and replacement parts.该问题仅描述了维修和更换零件之间关系的一半。 You stated that a repair may consist of zero ore more replacement parts.您表示维修可能包括零个或多个更换零件。

But (1) can the same replacement part be taken in zero or more repairs, or (2) is a replacement part consumed by a repair such that it can occur only zero or one time in a repair?但是 (1) 可以在零次或多次维修中使用相同的更换零件,或者 (2) 维修消耗的更换零件是否只能在维修中出现零次或一次?

It is necessary to make this clear, because (1) and (2) lead to different database schemes:有必要说清楚,因为(1)和(2)导致不同的数据库方案:

(1) is a many - to - many relationship and leads to a separate table connecting parts and repairs: (1) 是多对多的关系并导致单独的表连接零件和维修:

create table part (
    partId int primary key
);

create table repair (
    repairId int primary key
    );

create table part_repair (
    partId int references part (partId),
    repairId int references repair (repairId)
    primary key (partId, repairId)
    );

(2) part may be used in zero or one repair; (2) 部分可用于零次或一次修复; store repair-id in part:部分存储修复 ID:

create table repair (
    repairId int primary key
    );

create table part (
    partId int primary key,
    repairId int references repair(repairId)
);

Note that repairId in part may take on NULL values for those parts that are not assigned to a repair.请注意,对于未分配给修复的那些部分,部分repairId可能会采用NULL值。

Hope that helps a bit.希望那些对你有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM