MySQL before insert trigger that drops insert if condition

MySQL before insert trigger that drops insert if condition

I have the following tables:

I have the following tables:

slds:

+-----------+-----------+
|    id     | sld_name  |  
+-----------+-----------+
|     1     |  google   |  
+-----------+-----------+
|     2     |  github   |
+-----------+-----------+

paths:

+-----------+------------+----------+
|    id     | path_name  |  sld_id  |   
+-----------+------------+----------+
|    101    |  /cats/    |    1     |
+-----------+------------+----------+
|    102    |  /dogs/    |    2     |
+-----------+------------+----------+

Note that the sld_name is a unique index, and sld_id is the foreign key of slds.

INSERT IGNORE INTO paths (path_name, sld_id)
VALUES ('/dogs/', 1), ('/dogs/', 2) ... this can be hundreds of rows long

When the example above happens, I need a way to remove/prevent the insert of the additional ('/dogs/', 2) row, since that path already exists where sld_id=2, and not prevent the ('/dogs/', 1) row, since sld_id=1 doesn't have a /dogs/ path yet.

To implement this, I tried using this trigger:

delimiter $$
create trigger after_insert_paths
after insert on paths 
for each row begin
declare path_check INT;

set path_check := ( select sld_id from paths where path_name=new.path_name and sld_id=new.sld_id ); if path_check is not null then set new.path_name = null; end if;

end $$ delimiter ;

All this did was prevent inserts from happening at all.

Is there something specific that is wrong with the trigger? or does this strategy not work in general?

Is there a better approach to this that I'm missing?

Any advice would really be appreciated! :)

sql mysql

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

Best MySQL DigitalOcean Performance – ScaleGrid vs. DigitalOcean Managed Databases

Compare ScaleGrid MySQL vs. DigitalOcean Managed Databases - See which offers the best MySQL throughput, latency, and pricing on DigitalOcean across workloads.

Welcome Back the T-SQL Debugger with SQL Complete – SQL Debugger

Debug SQL stored procedures and develop your SQL database project with dbForge SQL Complete, a new add-in for Visual Studio and SSMS. When you develop large chunks of T-SQL code with the help of the SQL Server Management Studio tool, it is essential to test the “Live” behavior of your code by making sure that each small piece of code works fine and being able to allocate any error message that may cause a failure within that code.

SQL LOWER Function Example | Lower() In MySQL and SQL Server

SQL LOWER Function Example | Lower() In MySQL and SQL Server. The SQL LOWER function converts all the characters in a string into lowercase.