简体   繁体   中英

Stored procedure for adding a movie

I'm up to creating a stored procedure for adding a movie (I'm working on an application which stores and lists movies with information), and before getting started I would like some input on how to create it.

I have the following tables:

  1. "Movie" (MovieID, Name, Year, Length, Summary)
  2. "Genre", which is a list of genres (GenreID, Genre)
  3. "MovieGenre", which is a list of the movies and their genre/s. (MovieGenreID, GenreID, MovieID)
  4. "MovieRole", which stores the name of the actors/directors etc (MovieRoleID, Name)
  5. "MovieRoleType", which stores different kinds of roles for a movie, like actor (MovieRoleTypeID, MovieRoleType)
  6. "Cast", which is a list of the movies cast's (CastID, MovieRoleTypeID, MovieID, MovieRoleID)

When adding a movie you must provide all the information about the movie, as well as at least one movie role (eg. an actor) and a genre. Should I create several SP:s and execute them from one SP, or how should I do?

NOTE: I'm not asking you to write the whole SP for me, just asking for some guideline.

Thanks in advance!

I would create several separate stored procedures and then use a transaction to make sure the inserts were committed together.

You are going to need separate SPs to add additional cast members, genres, etc. It doesn't make a lot of sense to me to have one large SP and all of the support code that goes into calling the SP to add a new movie + role + genre and then have separate SPs (and support code) to duplicate the adding of roles and genres. Using a transaction will effectively wrap all of your separate SPs into a single action making it appear almost like a single SP but giving you the flexibility to use the individual SPs as well.

You're going to need support code to call each of the individual SPs anyway. Code that will validate parameters, handle errors, set the SP's values, etc. So you'll need functions like AddRole, AddGenre, etc. in your application. I'd rather have one function in my code called AddMovie that started a transaction and then called each of these individual functions. Break the job into small, easily testable pieces and then build the larger functions (AddMovie) by calling the smaller pieces that you know work. It would be much more complicated to build an AddMovie function that called a different SP that basically does a lot of the same things that the pieces you already have do.

I Would create a sproc for each entity, and then possibly wrap those in a separate SP called "AddNewMovie" or whatever.

AddNewMovie would just call the other sprocs. For added bonus, wrap them in a transaction.

If you are not familiar with transactions, MSDN has a pretty good article

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM