{question}
Does SingleStore support database triggers?
{question}
{answer}
Currently, traditional triggers are not supported in SingleStore. SingleStore doesn’t have any direct alternatives for triggers today. In a Sharded/Distributed system, triggers on update/delete are a potentially expensive and performance-undermining operation.
The closest feature we have would be PIPELINES into STORED PROCEDURES which let you run some logic as data is loaded via the pipeline.
Click here to learn about SingleStore Pipelines to Stored Procedure and also click here to learn about writing efficient stored procedures for the pipeline.
For relatively simple triggers, something like the ON DUPLICATE KEY UPDATE clause can do the job.
Basic Example of ON DUPLICATE KEY UPDATE:
> SELECT * FROM cust;
+-------+------+--------+
| NAME | ID | ORDERS |
+-------+------+--------+
| Chris | 7214 | 2 |
| Elen | 8301 | 4 |
| Adam | 3412 | 5 |
+-------+------+--------+
INSERT INTO cust (ID, ORDERS) VALUES (7214, 3) ON DUPLICATE KEY UPDATE ORDERS=3;
> SELECT * FROM cust;
+-------+------+--------+
| NAME | ID | ORDERS |
+-------+------+--------+
| Chris | 7214 | 3 |
| Elen | 8301 | 4 |
| Adam | 3412 | 5 |
+-------+------+--------+
Click here to learn more about performing upserts.
In some specific use cases, computed columns might also be an alternative (if the second row to be inserted by the trigger can be refactored into another column in the same table). Also, any triggers can be configured at the application end. Click here to learn about Persisted computed columns and to learn about Stored Procedures click here.
{answer}