Hello. There is a table in the database - products . Each product has a country of origin, for normalizing the database, made a table of countries where there are fields id and name . Next, I make the table M: ​​M - products_has_countries , where I need to store in each record product id and country id in order to link them. Question: Is it possible not to create a field with primary key?

  • why don't you want to create a primary key? You probably have the requirement not to keep the connection of one country with one product more than once. And if so, then you have the primary key dictated by the very nature of the data, consisting of these two fields ( primary key(id-продукта,id-страны) ). And yes, of course, no additional fields are needed - Mike
  • @Mike was a misconception of databases, because of what I did not really understand what a primary key is and that it can consist of 2 columns. - Alexxosipov
  • And why do you need many to many connections? Do you have the same product can be produced in different countries? Can it be easier to add a country column to the product? - Andrey NOP
  • @Andrey And duplicate products produced in several countries? not really an idea, frankly. - Akina
  • whether it is possible not to create a field with primary key? Better create. At the very least, this guarantees the absence of duplicate records - one should not rely on client logic in this matter. - Akina

2 answers 2

It depends on what you need. If products_has_countries is an auxiliary table with many-to-many links, then of course the primary key is optional.

But it seems to me that your products cannot have two producing countries. And country_id should be right in the products table.

    The 21st century the same product can be produced in different countries, for example, machines, processors.

    A table without a primary key can be created and in this case it is not needed in the classical form. You can create a composite primary key for 2 fields to avoid duplication.