简体   繁体   中英

SQL Server - why the foreign keys aren't connecting to the primary key

I want to insert the primary keys to all their foreign keys. I changed my procedure and still get nulls or empty rows:

dbo.Stories-空 [ 这是桌子 1

The other connected tables are all empty.

My procedure:

CREATE PROCEDURE insertText    
    @UserID [INT],
    @story [NVARCHAR](120),
    @img [VARCHAR](MAX)
AS
BEGIN
    INSERT INTO Stories ([StoryText]) 
    VALUES (@story)

    INSERT INTO Images ([img]) 
    VALUES (@img)

    SELECT        
        dbo.Table_Users.UserID , dbo.Stories.StoryID, dbo.Images.imgID
    FROM            
        dbo.Images 
    INNER JOIN
        dbo.imagesInStories ON dbo.Images.imgID = dbo.imagesInStories.imgID3   
    INNER JOIN
        dbo.Stories ON dbo.Images.imgID = dbo.Stories.imgID2 
                    AND dbo.imagesInStories.StoryID3 = dbo.Stories.StoryID 
    INNER JOIN
        dbo.Users_Stories ON dbo.Stories.StoryID = dbo.Users_Stories.StoryID2 
    INNER JOIN
        dbo.Table_Users ON dbo.Users_Stories.ID2 = dbo.Table_Users.UserID
    WHERE 
        dbo.Table_Users.UserID = @UserID
END
GO

-- exec UserStory 3, 'today nothing worked ???', 'meyesterday.jpg'

I hope you understand.

Can't find any answer anywhere

And tried for days.

You are inserting two single values in two different tables, but your query seems to show that you need more information to "connect" the inserted values. The inserted Story will receive a StoryID and the Image receives an imgID . Supposed that your query is correct, you will have to change your code as follows:

CREATE PROCEDURE insertText    
    @UserID [INT],
    @story [NVARCHAR](120),
    @img [VARCHAR](MAX)
AS
BEGIN

    DECLARE @StoryID int, @ImageID int;

    INSERT INTO dbo.Images ([img]) 
    VALUES (@img);

    SET @ImageID = SCOPE_IDENTITY();

    INSERT INTO dbo.Stories ([StoryText], imgID2) 
    VALUES (@story, @ImageID);

    SET @StoryID = SCOPE_IDENTITY();

    INSERT INTO dbo.imagesInStories (StoryID3, imgID3)
    VALUES (@StoryID, @ImageID);

    INSERT INTO dbo.Users_Stories (ID2, StoryID2)
    VALUES (@UserID, @StoryID);

    SELECT  Table_Users.UserID , Stories.StoryID, Images.imgID
    FROM  dbo.Images 
    INNER JOIN
        dbo.imagesInStories ON Images.imgID = imagesInStories.imgID3   
    INNER JOIN
        dbo.Stories ON Images.imgID = Stories.imgID2 
                    AND imagesInStories.StoryID3 = Stories.StoryID 
    INNER JOIN
        dbo.Users_Stories ON Stories.StoryID = Users_Stories.StoryID2 
    INNER JOIN
        dbo.Table_Users ON Users_Stories.ID2 = Table_Users.UserID
    WHERE 
        Table_Users.UserID = @UserID
END

You should maybe re-design your table structure: the stories and images seem to be linkd in two ways: 1:n directly and m:n via table imagesInStories .

Added:

I declared the variables @StoryID and @ImageID to store the values of the identity column of both tables right after a new record is inserted using the SCOPE_IDENTITY() system function. The new @ImageID is needed when inserting the Story that the Image belongs to, and the new @StoryID is needed when adding an image-story-connection (to the imagesInStories table) and when adding the new story to the stories of the user (table Users_Stories ).

Your original query joins Images and Stories in two ways:

  1. using the imgID2 column in the Stories table
  2. using both imgID3 and StoryID3 column in the imagesInStories table.

I think that one way to connect images and stories is enough, that's why I was talking about re-design. Does each Story have at most one Image ? If so, remove the table imagesInStories , and if not, remove the imgID2 column from the Stories table. However, your query uses both ways, which will return incomplete results when there are multiple images linked to a story, because the query will only return the one image who's ID is saved in the imgID2 column of the Stories table.

I deleted all this part from the code and it did worked perfectly the same !!

 SELECT  Table_Users.UserID , Stories.StoryID, Images.imgID
FROM  dbo.Images 
INNER JOIN
    dbo.imagesInStories ON Images.imgID = imagesInStories.imgID3   
INNER JOIN
    dbo.Stories ON Images.imgID = Stories.imgID2 
                AND imagesInStories.StoryID3 = Stories.StoryID 
INNER JOIN
    dbo.Users_Stories ON Stories.StoryID = Users_Stories.StoryID2 
INNER JOIN
    dbo.Table_Users ON Users_Stories.ID2 = Table_Users.UserID
WHERE 
    Table_Users.UserID = @UserID

was it necessary? PS What did you mean by:" You should maybe re-design your table structure: the stories and images seem to be linkd in two ways: 1:n directly and m:n via table imagesInStories" - I don't understand, can you please explain me?

I'm just so new in this SQL. Thanks for the answers

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