I'm fairly new to SQL and I have the following question:
I'm making an android app that stores 3 tables into a database.
-
Movies, associated with a custom class I called Movie
-
Trailers, associated with a custom class I called Trailer
-
Reviews, associated with a custom class I called Review
When an event is triggered by the app, I want to create a SQL statement that will delete all movies that are not marked as favorite. Also, it has to delete every trailer and review associated with a movie that is not marked as favourite. To mark a movie as favorite, I added an integer column that can have 3 values: (0,1,2). When the movie entry has a value of 2, it is a favorite and, therefore, must NOT be deleted.
This is the structure of my tables:
//SQL statement to create the movies table
final String SQL_CREATE_FAVOURITES_TABLE = "CREATE TABLE " +
MoviesEntry.TABLE_NAME + " (" +
MoviesEntry.COLUMN_MOVIE_ID + " INTEGER PRIMARY KEY," +
MoviesEntry.COLUMN_POSTER_URL + " TEXT NOT NULL," +
MoviesEntry.COLUMN_TITLE + " TEXT NOT NULL," +
MoviesEntry.COLUMN_PLOT + " TEXT NOT NULL," +
MoviesEntry.COLUMN_RATING_AVERAGE + " REAL NOT NULL," +
MoviesEntry.COLUMN_RATING_COUNT + " INTEGER NOT NULL," +
MoviesEntry.COLUMN_RELEASE_DATE + " TEXT NOT NULL, " +
MoviesEntry.COLUMN_MOVIE_TYPE + " INTEGER NOT NULL, " +
"CHECK (" + MoviesEntry.COLUMN_MOVIE_TYPE + " IN (0,1,2))" + " );";
//SQL statement to create the trailers table
final String SQL_CREATE_TRAILERS_TABLE = "CREATE TABLE " +
TrailersEntry.TABLE_NAME + " (" +
TrailersEntry.COLUMN_VIDEO_ID + " TEXT PRIMARY KEY," +
TrailersEntry.COLUMN_YOUTUBE_PATH + " TEXT NOT NULL," +
TrailersEntry.COLUMN_VIDEO_NAME + " TEXT NOT NULL," +
TrailersEntry.COLUMN_MOVIE_ID + " INTEGER NOT NULL, "
//COLUMN_MOVIE_ID is a foreign key to favourites table
+ " FOREIGN KEY (" + TrailersEntry.COLUMN_MOVIE_ID + ") REFERENCES " +
MoviesEntry.TABLE_NAME + " (" + MoviesEntry.COLUMN_MOVIE_ID + "), " +
//Each trailer for a movie has to be unique
" UNIQUE (" + TrailersEntry.COLUMN_VIDEO_ID + ", " +
TrailersEntry.COLUMN_MOVIE_ID + ") ON CONFLICT REPLACE);";
//SQL statement to create the reviews table
final String SQL_CREATE_REVIEWS_TABLE = "CREATE TABLE " +
ReviewsEntry.TABLE_NAME + " (" +
ReviewsEntry.COLUMN_REVIEW_ID + " TEXT PRIMARY KEY," +
ReviewsEntry.COLUMN_AUTHOR + " TEXT NOT NULL," +
ReviewsEntry.COLUMN_CONTENT + " TEXT NOT NULL," +
ReviewsEntry.COLUMN_MOVIE_ID + " INTEGER NOT NULL, " +
//COLUMN_MOVIE_ID is a foreign key to the favourites table
" FOREIGN KEY (" + ReviewsEntry.COLUMN_MOVIE_ID + ") REFERENCES " +
MoviesEntry.TABLE_NAME + " (" + MoviesEntry.COLUMN_MOVIE_ID + "), " +
//Each review for a movie has to be unique
" UNIQUE (" + ReviewsEntry.COLUMN_REVIEW_ID + ", " +
ReviewsEntry.COLUMN_MOVIE_ID + ") ON CONFLICT REPLACE);";
db.execSQL(SQL_CREATE_FAVOURITES_TABLE);
db.execSQL(SQL_CREATE_TRAILERS_TABLE);
db.execSQL(SQL_CREATE_REVIEWS_TABLE);
Can you help me to figure out how to construct the delete statement that will accomplish this?
Thank you very much.
Aucun commentaire:
Enregistrer un commentaire