Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9181395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:18:14+00:00 2026-06-17T18:18:14+00:00

You can use an IF statement in a MySQL stored procedure to control the

  • 0

You can use an IF statement in a MySQL stored procedure to control the actions that are taken.
The syntax is as follows:

 IF condition THEN ... do something ... END IF;

The condition in your IF statements can be similar to the conditions in CASE statements. For additional information, check out MySQL’s web site.

You can also retrieve the auto increment ID after you perform an insert. Just use the following query:

 SELECT LAST_INSERT_ID() into artist_id;

For this project, create a new procedure called AddAlbum. It will take at least two parameters – NameOfArtist and AlbumName (just like in the last project).

Your procedure needs to check the Artists table to see if an NameOfArtist exists. If it does not exist, you’ll need to add a new row. Here are the steps:

  1. Use the COUNT aggregate on Artists to see how many rows exist for NameOfArtist. Store the count in a variable called artist_count.
  2. If artist_count is zero, insert a new row into Artists.

— Select the LAST_INSERT_ID() into a variable.

  1. If artist_count is one, lookup the ArtistID and store it in a variable.
  2. Insert a new row into the Albums table.

code:

CREATE PROCEDURE AddAlbum(

NameOfArtist varchar(50),

AlbumName varchar(50)

);
BEGIN

DECLARE artist_count INT;

DECLARE artist_id INT;

SELECT COUNT(ArtistName) INTO artist_count FROM Artists
WHERE ArtistName = NameOfArtist;

IF artist_count = 0
THEN SELECT LAST_INSERT_ID(NameOfArtist) INTO artist_id
AND INSERT INTO Artists (ArtistName)
VALUES (NameOfArtist)
END IF;

IF artist_count = 1
THEN SELECT ArtistID INTO artist_id
FROM Artists
WHERE ArtistName = NameOfArtist
END IF;

INSERT INTO Albums (ArtistID, Title)
VALUES (artist_id, AlbumName);
END;
//

This is what I’m trying, for the class I’m currently taking. Honestly, I’m not understanding how to write the code too well. I understand exactly what needs to be done, my brain for some reason is just not processing how to write the code correctly for some reason.
I’m fairly certain that I have a lot of errors, I’ve been working on it quiet some time, and just can’t seem to get it. Been re-thinking this database admin course I’m taking. But, I’m not going to give up.
Is there anyone on who can assist me with this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:18:16+00:00Added an answer on June 17, 2026 at 6:18 pm

    If you need to follow the exact steps, this would be your code:

    CREATE PROCEDURE AddAlbum(
    NameOfArtist varchar(50),
    AlbumName varchar(50)
    );
    
    BEGIN
      DECLARE artist_count INT;
      DECLARE artist_id INT;
    
      SELECT COUNT(ArtistName) INTO artist_count FROM Artists WHERE ArtistName = NameOfArtist;
      IF artist_count = 0 THEN 
        INSERT INTO Artists (ArtistName) VALUES (NameOfArtist);
        SELECT LAST_INSERT_ID(NameOfArtist) INTO artist_id; 
      ELSE IF artist_count = 1 THEN 
        SELECT ArtistID INTO artist_id FROM Artists WHERE ArtistName = NameOfArtist; 
      END IF;
    
      INSERT INTO Albums (ArtistID, Title) VALUES (artist_id, AlbumName); 
    END; //
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can we use Select statement inside a loop in a Mysql Stored Procedure? why
I can use the PRINT statement in a stored procedure to debug my code.
I know that I can use the statement: [string1 rangeOfString:compareString].location!=NSNotFound to check if a
Anyone have a PL-SQL statement that i can use to generate database & tables
This has me seriously stumped. I have a MySQL stored procedure that looks like
the MySQL manual says that a CASE statement (the WHEN part) can contain a
In C#, I can use the throw; statement to rethrow an exception while preserving
I want to use using statement purely for scope management. Can I omit holding
WHERE gets processed before GROUP BY in the SELECT statement. How can I use
I am trying to write a stored procedure that will allow me to write

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.