Question

I have tried various ways. This is the one that makes sense to me, however when I try to execute the insert into listsandplayers, it tells me no column listID exists. I tried a couple of different things with my insert into listsandplayers statement and continued to get that error.

I have posted here how I feel the statement should work best. It is the last statement shown here.

CREATE TABLE IF NOT EXISTS 
   lists (listID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, listName VARCHAR(30));

CREATE TABLE IF NOT EXISTS 
   players(playerID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, playerName VARCHAR);

CREATE TABLE IF NOT EXISTS 
    listsandplayers(listsandplayersID INTEGER NOT NULL, 
                    listID INTEGER REFERENCES lists (listID),  
                    playerID INTEGER REFERENCES players (playerID));

INSERT INTO lists (listName) VALUES('Los Angeles Dodgers');

INSERT INTO players (playerName) VALUES('Clayton Kershaw');

INSERT INTO listsandplayers (listID, playerID) VALUES(listID, playerID)   
WHERE listName = 'Los Angeles Dodgers'  
AND playerName = 'Clayton Kershaw'  
NATURAL JOIN lists  
NATURAL JOIN players;
Was it helpful?

Solution

This is not valid syntax for an INSERT command. Try this:

INSERT INTO ListsAndPlayers (ListsAndPlayersID, ListID, PlayerID)
VALUES (42,
        (SELECT ListID FROM Lists WHERE ListName = 'Los Angeles Dodgers'),
        (SELECT PlayerID FROM Players WHERE PlayerName = 'Clayton Kershaw'));

If you had declared the ListsAndPlayersID column as INTEGER PRIMARY KEY, SQLite would automatically generate a value. (You could also drop the ListsAndPlayersID column and just use the combination of ListID and PlayerID as primary key.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top