Question

11. For each song in the songs table, update the value of artist_id to the id of the corresponding artist in the artists tabl
-- --- - ---- - --- | Field | Type Null key | Default | Extra PRI | id | name auto_increment | int(11) NO varchar(50) | YES N
sql> select from songs; seconds | sales | genre artist_id | genre_id id title artist 1 minutes 1 | Lover | Taylor swift 3 Map
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the Update query for the question 11.1:

UPDATE songs
SET artist_id = (
SELECT artist.id
FROM artist
WHERE artist.name = songs.artist
);

Below is the Update query for the question 12.1;

UPDATE songs
SET genre_id = (
SELECT genre.id
FROM genre
WHERE genre.name = songs.genre
);

Explanation :

In the above queries the columns artist_id and genre_id of table songs is updated by using Update statement and SUBQUERY in which select statement is used to get the genre id and artist id from the genre and artist tables by using the condition where genre names and artist names are equal to respective names in the songs table.

Please refer to the snapshot below for the above update queries:

UPDATE songs SET artist_id =( SELECT artist.id FROM artist WHERE artist.name = songs.artist ); UPDATE songs SET genre_id = (

$sqlite3 database. sdb <main.sql 1 Love Taylor_swift|3|41|0.Pop|1|10 3 Maps Maroon5|35|0. Pop|3|10 4 Under Pressure Queen |3|

Explanation :

Three tables are created with the below description:

1) Table1 : artist

Column names: id , name

Query for creating and inserting the sample data in table artist :

CREATE TABLE artist(id integer PRIMARY KEY, name varchar(50));

INSERT INTO artist VALUES(1,'Taylor_swift');
INSERT INTO artist VALUES(3,'Maroon5');
INSERT INTO artist VALUES(4,'Queen');
INSERT INTO artist VALUES(5,'Strauss');

2) Table1 : genre

Column names: id , name

Query for creating and inserting the sample data in table genre:

CREATE TABLE genre(id integer PRIMARY KEY, name varchar(50));

INSERT INTO genre VALUES(10,'Pop');
INSERT INTO genre VALUES(30,'Rock');
INSERT INTO genre VALUES(40,'Classical');
INSERT INTO genre VALUES(50,'Funk');

2) Table1 : songs

Column names: id , title, artist, minutes, seconds, sales, genre, artist_id, genre_id

Query for creating and inserting the sample data in table songs:

CREATE TABLE songs(id integer PRIMARY KEY, title varchar(50), artist varchar(50), minutes integer, seconds integer, sales float, genre varchar(50), artist_id integer, genre_id integer);

INSERT INTO songs VALUES(1,'Love','Taylor_swift',3,41,0.00,'Pop',0,0);
INSERT INTO songs VALUES(3,'Maps','Maroon5',3,5,0.00,'Pop',0,0);
INSERT INTO songs VALUES(4,'Under_Pressure','Queen',3,46,0.00,'Rock',0,0);
INSERT INTO songs VALUES(5,'Blue_Danube','Strauss',3,58,0.00,'Pop',0,0);

Please refer to the snapshot of the SQL code and the execution result:

CREATE TABLE artist(id integer PRIMARY KEY, name varchar(50)); INSERT INTO artist VALUES (1, Taylor swift INSERT INTO artis


Add a comment
Know the answer?
Add Answer to:
11. For each song in the songs table, update the value of artist_id to the id...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi i need Help. i use MYSQL command line. THE QUESTION ARE BELOW please check the...

    Hi i need Help. i use MYSQL command line. THE QUESTION ARE BELOW please check the answer before you submit because sometimes query gives error thank you For Full database of SQL you can DOWNLOAD from this link: https://drive.google.com/file/d/1xh1TcBfMtvKoxvJr7Csgnts68fF53Q1t/view?usp=sharing ------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- What are the total sales for each year. Note that you will need to use the YEAR function. Order by year with the newest year first. ------------------------------------------------------- How many employees have no customers? ------------------------------------------------------------------ List the total sales for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT