Don't allow reversed composite primary key in MySQL
I'm developing an application which needs to hold data about distances between two cities. I have created a distance table in the Mysql database which holds the name of the two cities and the distance between them. I have made the two town columns a composite primary key. I'd like the database to restrict the application from making duplicated reversed entries like shown on the screenshot to prevent having different distance values. What would be the best solution to solve this problem? You could create a stored procedure to insert into this table. DELIMITER $$ CREATE PROCEDURE insert_distance