简体   繁体   English

SQL Server-为什么外键没有连接到主键

[英]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: 我更改了程序,但仍然得到null或空行:

dbo.Stories-空 [ [ 这是桌子 1 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 . 插入的Story将接收一个StoryID ,而Image将接收一个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 . 您可能应该重新设计表结构:故事和图像似乎以两种方式链接:直接1:n和通过表imagesInStories m:n。

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. 我声明了变量@StoryID@ImageID以在使用SCOPE_IDENTITY()系统函数插入新记录后立即存储两个表的标识列的值。 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 ). @ImageID插入故事时,该图像属于是必要的,并且新@StoryID添加图像层的连接(到当需要imagesInStories表)并添加新的故事时,用户的故事(表Users_Stories )。

Your original query joins Images and Stories in two ways: 您的原始查询以两种方式将图像故事连接起来:

  1. using the imgID2 column in the Stories table 使用“ Stories表中的imgID2
  2. using both imgID3 and StoryID3 column in the imagesInStories table. 同时使用imgID3StoryID3imagesInStories表。

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. 如果是这样,则删除表imagesInStories ,否则,从Stories表中删除imgID2列。 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. 但是,您的查询使用两种方法,当有多个图像链接到一个故事时,这将返回不完整的结果,因为查询将仅返回ID已保存在“ Stories表的imgID2列中的一个图像。

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? PS :您的意思是:“您可能应该重新设计表结构:故事和图像似乎以两种方式链接:直接通过1:image进行n:n,通过表imagesInStories通过m:n进行链接”-我不明白你能解释一下吗?

I'm just so new in this SQL. 我对这个SQL还是那么陌生。 Thanks for the answers 感谢您的回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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