简体   繁体   English

将三个SQL Server表连接在一起以显示图像

[英]Connecting Three SQL Server Tables Together to Display Images

I am trying to create a Photo Album/Collection for each student in my ASP.NET website, using SQL Server tables. 我正在尝试使用SQL Server表为ASP.NET网站中的每个学生创建一个相册/收藏集。

Here are my table structure: 这是我的表结构:

片段1

I want to store the image name within the Student_Images folder. 我想将图像名称存储在Student_Images文件夹中。 Then each image will be linked to a Photo Collection. 然后,每个图像都将链接到照片集。 Each Photo Collection is linked to a group. 每个照片集都链接到一个组。

Then I want to display each group, any photo collections which are associated with that group, and then any images associated with that collection. 然后,我要显示每个组,与该组关联的所有照片集,然后显示与该集相关的任何图像。

I have been able to display images related to each student, but I don't know how to add the 'Photo Collection' table between the Students and Images tables. 我已经能够显示与每个学生相关的图像,但是我不知道如何在“学生”和“图像”表之间添加“照片集”表。

Here is how I would like it to display: 这是我想要的显示方式:

片段2

Here is how the tables would look populated: 这是表格的填充方式:

SNIP

Can anyone advise me as to how to go about making this connection? 谁能建议我如何建立此连接?

Make some changes in your Photo_Collection table, create an FK within your Photo_Collection of Student_ID, then you'll be able to show the photo collection of all the students because through Student_ID, you can have access to student table as well as Student_Images. 在Photo_Collection表中进行一些更改,在Student_ID的Photo_Collection中创建FK,然后您将能够显示所有学生的照片集,因为通过Student_ID,您可以访问学生表和Student_Images。 Hope it helps. 希望能帮助到你。

There's an idea of a foreign key - where a row in a table refers to a row in another table. 有一个外键的想法-表中的行引用另一个表中的行。 You definitely want to do this for a number of reasons, to ensure data integrity and to document your relationships for whoever works on the site. 您肯定出于多种原因要这样做,以确保数据完整性并记录与站点上任何人的关系。

You don't quite have enough information to get where you want to get. 您没有足够的信息去到达想要的地方。 You don't have any linkage between photo albums and the images they contain. 相册与其包含的图像之间没有任何联系。 You'll need another table for that. 您将需要另一个表。

I kinda recommend that you use column names that don't repeat the table name. 我有点建议您使用不重复表名的列名。 You'll find you can then see patterns more clearly in your schema. 您会发现可以在架构中更清楚地看到模式。 For example Photo_Collection.Photo_Collection_Id should probably be just Photo_Collection.Id . 例如, Photo_Collection.Photo_Collection_Id可能应该仅仅是Photo_Collection.Id It's more concise, and it's obvious which Id you're talking about because you always use the table name when referring to that column. 这更加简洁,而且很明显您在谈论哪个ID,因为在引用该列时始终使用表名。

So, to get you nearer to where you need to be, I'd recommend something like this: 因此,为了使您更接近需要的位置,我建议使用以下方法:

create table dbo.Students
(
  ID
    int not null identity( 1, 1 )
    constraint [Students.ID.PrimaryKey]
      primary key clustered,

  Name
    nvarchar( 50 ) --> not very generous :-)
)
go
create index [Students.Name.Index] on dbo.Students( Name ) --> maybe unique?
go

create table dbo.Student_Images
(
  ID
    int not null identity( 1, 1 )
    constraint [Student_Images.ID.PrimaryKey]
      primary key clustered,

  Student_ID
    int not null
    constraint [Student_Images.to.Student]
      foreign key references dbo.Students( ID )

  Filename
    nvarchar( 250 ) null,  --> null? really? each image should have a unique file name, dont you think?

  Description
    nvarchar( 250 ) null
)
go
create index [Student_Images.to.Students.Index] on dbo.Student_Images( Student_ID )
go

create table dbo.Photo_Collection
(
  ID
    int not null identity( 1, 1 )
    constraint [Photo_Collection.ID.PrimaryKey]
      primary key clustered,

  Name
    nvarchar( 250 ) null --> null? hmmm...could be hard to use
)
go
create index [Photo_Collection.Name.Index] on dbo.Photo_Collection( Name ) --> consider unique??
go

create table dbo.Photo_Collection_Images
(
  Photo_Collection_ID
    int not null
    constraint [Photo_Collection_Images.to.Photo_Collection]
      foreign key references dbo.Photo_Collection( ID ),

  Student_Image_ID
    int not null
    constraint [Photo_Collection_Images.to.Student_Images]
      foreign key references dbo.Student_Images( ID )
)

You didn't really describe your groups...and there are lots of questions still unanswered. 您并没有真正描述您的小组……还有很多问题尚未得到解答。 For example, are Photo_Collections something that a student makes? 例如,Photo_Collections是学生制作的东西吗? If so, there should probably be a Student_ID in Photo_Collection with a foreign key to Students. 如果是这样,则在Photo_Collection中可能应该有一个Student_ID,带有指向Student的外键。

The table I added called Photo_Collection_Images relates your photo collections to the images. 我添加的名为Photo_Collection_Images的表将您的照片集与图像相关联。 You'd include this table in any query that needs to display all the images in a given photo collection. 您将在需要显示给定照片集中的所有图像的任何查询中包括该表。 I think that's the main missing bit for you. 我认为这是您的主要缺失点。 You'd do something similar for groups. 您会为小组做类似的事情。

Also - FYI, pasting images of your text is kinda aggravating. 另外-仅供参考,粘贴文字的图像有点麻烦。 Consider just pasting the text in and indenting it 4 spaces to get it to format correctly. 考虑仅将文本粘贴并缩进4个空格以使其正确格式化。

Edit : 编辑

To select, for example, all the images in a photo collection by student, you could do: 例如,要选择学生的照片集中的所有图像,您可以执行以下操作:

select
  pc.Name PhotoCollectionName,
  si.FileName FileName,
  si.Description FileDescription,
  s.Name StudentName
from
  dbo.Photo_Collection pc
  inner join
  dbo.Photo_Collection_Images pci
  on
    pc.Id = pci.Photo_Collection_ID
  inner join
  dbo.Student_Image si
  on
    pci.Student_Image_ID = si.ID
  inner join 
  dbo.Students s
  on 
    si.Student_Id = s.ID

This is pretty much the base query for everything. 这几乎是所有内容的基本查询。

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

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