我有一个包含6列的表。我有另外6个表有两列。这6个表中的每一个都有id列和值列。这6个表是我的应用程序中的下拉列表,我现在想要从下拉列表中的每个值,例如从这6个表中的一个表以某种方式引用第一个表有6列,应该是这6列ID的ID。我尝试了几个小时但我不能引用这些表。当用户从下拉列表中选择一些值时,如何用六列的第一个表来知道选择了什么值从另一张桌子。可以有人帮我吗?谢谢
答案 0 :(得分:1)
欢迎使用StackOverflow!
如果我找到了你(我希望如此),你想要创建一个包含引用Dropdown值的外键列的表。因此,每行包含6个下拉值。
这是你正在寻找的吗?
--Data Tables
CREATE TABLE [DropDownTable1]
(
[DropDownTable1Id] INT Identity(1,1) NOT NULL,
[Name] VARCHAR(32) NOT NULL,
CONSTRAINT [pk__dropdowntable1] PRIMARY KEY CLUSTERED ([DropDownTable1Id] ASC)
);
CREATE TABLE [DropDownTable2]
(
[DropDownTable2Id] INT Identity(1,1) NOT NULL,
[Name] VARCHAR(32) NOT NULL,
CONSTRAINT [pk__dropdowntable2] PRIMARY KEY CLUSTERED ([DropDownTable2Id] ASC)
);
CREATE TABLE [DropDownTable3]
(
[DropDownTable3Id] INT Identity(1,1) NOT NULL,
[Name] VARCHAR(32) NOT NULL,
CONSTRAINT [pk__dropdowntable3] PRIMARY KEY CLUSTERED ([DropDownTable3Id] ASC)
);
CREATE TABLE [DropDownTable4]
(
[DropDownTable4Id] INT Identity(1,1) NOT NULL,
[Name] VARCHAR(32) NOT NULL,
CONSTRAINT [pk__dropdowntable4] PRIMARY KEY CLUSTERED ([DropDownTable4Id] ASC)
);
CREATE TABLE [DropDownTable5]
(
[DropDownTable5Id] INT Identity(1,1) NOT NULL,
[Name] VARCHAR(32) NOT NULL,
CONSTRAINT [pk__dropdowntable5] PRIMARY KEY CLUSTERED ([DropDownTable5Id] ASC)
);
CREATE TABLE [DropDownTable6]
(
[DropDownTable6Id] INT Identity(1,1) NOT NULL,
[Name] VARCHAR(32) NOT NULL,
CONSTRAINT [pk__dropdowntable6] PRIMARY KEY CLUSTERED ([DropDownTable6Id] ASC)
);
--Table with relations
CREATE TABLE [TbWithSixColumns]
(
[TbWithSixColumnsId] INT Identity(1,1) NOT NULL,
[DropDownTable1Id] INT NOT NULL,
[DropDownTable2Id] INT NOT NULL,
[DropDownTable3Id] INT NOT NULL,
[DropDownTable4Id] INT NOT NULL,
[DropDownTable5Id] INT NOT NULL,
[DropDownTable6Id] INT NOT NULL,
CONSTRAINT [pk__tbwithsixcolumns] PRIMARY KEY CLUSTERED ([TbWithSixColumnsId] ASC),
CONSTRAINT [fk__tbwithsixcolumns__dropdowntable1id] FOREIGN KEY([DropDownTable1Id]) REFERENCES [DropDownTable1]([DropDownTable1Id]),
CONSTRAINT [fk__tbwithsixcolumns__dropdowntable2id] FOREIGN KEY([DropDownTable2Id]) REFERENCES [DropDownTable2]([DropDownTable2Id]),
CONSTRAINT [fk__tbwithsixcolumns__dropdowntable3id] FOREIGN KEY([DropDownTable3Id]) REFERENCES [DropDownTable3]([DropDownTable3Id]),
CONSTRAINT [fk__tbwithsixcolumns__dropdowntable4id] FOREIGN KEY([DropDownTable4Id]) REFERENCES [DropDownTable4]([DropDownTable4Id]),
CONSTRAINT [fk__tbwithsixcolumns__dropdowntable5id] FOREIGN KEY([DropDownTable5Id]) REFERENCES [DropDownTable5]([DropDownTable5Id]),
CONSTRAINT [fk__tbwithsixcolumns__dropdowntable6id] FOREIGN KEY([DropDownTable6Id]) REFERENCES [DropDownTable6]([DropDownTable6Id])
);
--Populate Test-Data
INSERT INTO [DropDownTable1] ([Name]) VALUES ('Dropdownentry 1.1')
INSERT INTO [DropDownTable2] ([Name]) VALUES ('Dropdownentry 2.1')
INSERT INTO [DropDownTable3] ([Name]) VALUES ('Dropdownentry 3.1')
INSERT INTO [DropDownTable4] ([Name]) VALUES ('Dropdownentry 4.1')
INSERT INTO [DropDownTable5] ([Name]) VALUES ('Dropdownentry 5.1')
INSERT INTO [DropDownTable6] ([Name]) VALUES ('Dropdownentry 6.1')
INSERT INTO [TbWithSixColumns] ([DropDownTable1Id],[DropDownTable2Id],[DropDownTable3Id],[DropDownTable4Id],[DropDownTable5Id],[DropDownTable6Id]) VALUES (1,1,1,1,1,1);
--Query the dropdown data
SELECT t1.Name, t2.Name, t3.Name, t4.Name, t5.Name, t6.Name
FROM [TbWithSixColumns] relationTb
INNER JOIN [DropDownTable1] t1 ON relationTb.DropDownTable1Id = t1.DropDownTable1Id
INNER JOIN [DropDownTable2] t2 ON relationTb.DropDownTable2Id = t2.DropDownTable2Id
INNER JOIN [DropDownTable3] t3 ON relationTb.DropDownTable3Id = t3.DropDownTable3Id
INNER JOIN [DropDownTable4] t4 ON relationTb.DropDownTable4Id = t4.DropDownTable4Id
INNER JOIN [DropDownTable5] t5 ON relationTb.DropDownTable5Id = t5.DropDownTable5Id
INNER JOIN [DropDownTable6] t6 ON relationTb.DropDownTable6Id = t6.DropDownTable6Id
请注意: 在提出问题时,尽可能具体,并提供尽可能详细的信息(我们非常感谢有助于描述您的问题的代码片段,数据模型和图像)。您提供的详细信息越多,我们就越能够完全了解您遇到的问题。此外,如果我们不必从头开始构建整个解决方案,对我们来说就容易多了。 (而且,由于开发人员很懒惰,你也增加了有人回答的可能性;)
我希望我能帮到你。