Mysql: Limit record recording based on value from another table

Asked

Viewed 501 times

1

I have a table A with a field start_date and another end_date.

I have a table B with a field date.

Is there any way to ensure that a record is only saved on B when B.date be among A.start_date and A.end_date?

I know how to handle insertion via PHP. But I want to know if there is something that can be done in the Bank that guarantees this type and integrity. I’m looking for something in the constraints' line of action (but any solution is welcome!)

  • 2

    Search how to create a TRIGGER in UPDATE

  • 1

    Via Trigger is possible, but I believe you will need to use the 3 (Insert, delete and update) to ensure that none operation is made in case of non-compliance with these rules. Advice: If this rule is something related to 'closing' control (retroactive or out-of-jurisdiction drives) run away from the bank and deal with the application, since you have more. Otherwise you will become the trusted database squire for the rest of this system’s life

  • 2

    @Diegorafaelsouza totally agree on the application part. I think delegating too much intelligence to DB is losing control of the situation. By the way, if there’s one thing I see ugly in some common systems here in the region (especially Delphi + Pgsql) is that the guys put so much logic in DB, that the thing has side effect all the time.

  • 1

    @Bacco Yes, it is a very common practice in legacy systems and a problem that if we do not take care of ends up spreading in the current ones. The technology (and techniques) that we have available today have come to solve highly hairy problems of these types of conduits that were adopted in the older systems... if we don’t watch out, we’ll just keep wiping ice with hi-tec wipes -.-'

  • @Diegorafaelsouza It is a simple operation and will not backdate. When creating an event, I need to ensure that it is between the availability interval.

1 answer

4


There is, you can make one INSERT from a SELECT where in your clause WHERE shall be determined that the date has to be between the tabela_a then just limit the query to 1 to enter only 1 time the record, even finding more occurrences, for example:

CREATE TABLE tabela_a(
    start_date DATE, 
    end_date DATE
);
CREATE TABLE tabela_b(
    campo1 varchar(255), 
    campo2 varchar(255), 
    campo3 varchar(255), 
`date` DATE
);

INSERT INTO tabela_a(
    start_date, 
    end_date) 
VALUES
    ('2018-03-01', '2018-03-30'),
    ('2018-03-01', '2018-03-29'),
    ('2018-03-01', '2018-03-05');

Here will enter the records "valor1, valor2, Valor3, 2018-03-27", as found in tabela_a occurrences.

INSERT INTO tabela_b(campo1, campo2, campo3, `date`)
SELECT 
    'valor1',
    'valor2',
    'valor3',
    '2018-03-27'
FROM tabela_a
WHERE '2018-03-27' between start_date and end_date
LIMIT 1;

SELECT
    *
FROM tabela_b;

Here is a print of the query of what was inserted in tabela_b:

inserir a descrição da imagem aqui

I think it meets your need.

  • Very well thought out use of INSERT .. SELECT. +1

  • Using a select for Insert !? No need for values ?

  • Wow!!!!!!!!!!!!

  • @RBZ, there is no need.

  • Cool, that way I had never seen ! + 1 (actually I already gave the +1) rs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.