简体   繁体   中英

Foreign key issues with id “ALL”

I have 2 table

Table 1:

  ID_A| ClmnA
     1 | A
     2 | B
     3 | C

Table 2:

ID_B|ID_A
  H  | 3
  V  | 1
  K  | 1 

There is a relation between table 1 and table 2. The thing is all the id in the first table have one index "ALL" so it's going to be like this in the second table.

    ID_B |ID_A
      H  | 3
      V  | 1
      K  | 1 
     All | 1
     All | 2
     All | 3 

So the problem is I can't add them because of 2 things:

  1. The ID_B is a primary key so I can't add multiple "All" in it.

  2. Even if I delete the primary key in ID_B I can't add the because I have a thousand of rows.

Use a query to get the results:

select id_b, id_a
from table2
union all
select 'All', id_a
from table1;

For your application, you can wrap this in a view:

create view v_table2 as
    select id_b, id_a
    from table2
    union all
    select 'All', id_a
    from table1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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