Jump to content
Sign in to follow this  
R0DEmental-

MySQL limitēt ierakstus pēd foreign key

Recommended Posts

Sveiki! Īsumā

image.png.202a96ac3aeafb2fc8e1190a807f396e.png

Kā izveidot ierakstu limitu, balstoties pec tk_turnira_id. Pieņēmsim, man ir divi ieraksti ar vienādu turnīra id, gribu uzlikt limitāciju līdz 8 ierakstiem, lai vairāk par 8 ierakstiem ar vienādu tk_turnira_id viņš neļautu pievienot. PHP līmenī zinu, kā realizēt, bet vajag DB līmenī. 

Paldies.

Share this post


Link to post
Share on other sites

Var mēģināt izmantot mysql before insert triggeri, saskaitīt ierakstus un nofailot queriju, ja ir jau 8 ieraksti. Bet labāk padomā vai tev to vispār vajag darīt, izkausās mazliet savādi.

Edited by LoyC
  • Like 1

Share this post


Link to post
Share on other sites

Še procedūru: 

CREATE PROCEDURE InsertIfNotMoreThanEight(
    IN komandas_id INT, 
    IN turnira_id INT
)
BEGIN
    DECLARE current_count INT DEFAULT 0;

    SELECT COUNT(tk_turnira_id) INTO current_count FROM test WHERE tk_turnira_id = turnira_id;

    IF current_count < 8 THEN
        INSERT INTO test (tk_komandas_id, tk_turnira_id) VALUES (komandas_id, turnira_id);
    END IF;
END

Pēc tam izmanto šādi: 

CALL InsertIfNotMoreThanEight(1, 2);

 

  • Thanks 2

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.