Good day. Let there is a table vehicle (id), route (id). Each trans. the facility is assigned to one route. Is it possible to create a separate vehicle_route table (id, v_id, r_id), so that id is AUTO_INCREMNT PRIMARY KEY, and it was impossible to enter a record with an already existing v_id? The variant with linking the route in the table of the vehicle by route_id is not appropriate, a separate table is needed (in the future I want to create a table in which the connections between driver and vehicle_route will be written).

  • Use unique key . - Qwertiy
  • @Qwertiy, please write an example for this case - Muscled Boy

1 answer 1

http://sqlfiddle.com/#!9/26e922

 create table tbl ( a int not null primary key auto_increment, b int not null unique, c int not null ); insert into tbl(b, c) values (7, 9); insert into tbl(b, c) values (16, 3); insert into tbl(b, c) values (78, 42); 

If add

 insert into tbl(b, c) values (7, 101); 

you get an error:

Duplicate entry '7' for key 'b'

  • Brilliant, thank you. You helped me a lot - Muscled Boy
  • another question. If the skill has a table reference with one field, for example, the table marks (id) is the brand of a car with a field of possible marks. They are not repetitive and unique. The id should be only - PK, if you intend to use the id in another table as a foreign key, because simply unique will not give such an opportunity, and PK is unique in itself? - Muscled Boy
  • @MuscledBoy, unique and foreign key can be used simultaneously on the same column, isn't it? - Qwertiy
  • or rather primary key and unique, is it possible at the same time? - Muscled Boy
  • one
    primary is unique in itself. - Qwertiy