Select只有table的列名是继承SQL Server 2014中的table实现的
Select only the name of the column of the tables implemented by the inheritance of the table in SQL Server 2014
我有一个问题,关于 select 仅 name 的 table 的列的 table 继承 table ] 在 SQL Server 2014 中。
即有四种类型的人(学生、教师、员工和主管) 并且所有这些都包含一个 id
用于从 Person
table 继承的每种类型的人以及后来的 name 必须被 select 编辑和展示。
Table TypePerson
:
CREATE TABLE [TypesPerson]
(
[typePersonID] INT NOT NULL,
[typePerson] VARCHAR(50) NOT NULL,
CONSTRAINT [PK_TypesPerson] PRIMARY KEY CLUSTERED ([typePersonID] ASC)
);
Insert
人的类型:
INSERT INTO [TypesPerson]
SELECT 1,'students' UNION ALL
SELECT 2,'teachers' UNION ALL
SELECT 3,'employer' UNION ALL
SELECT 4,'supervisor'
Table Person
:
CREATE TABLE [Person]
(
[personID] INT IDENTITY (1, 1) NOT NULL ,
[typePersonID] INT NOT NULL,
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED ([PersonID] ASC),
CONSTRAINT [PK_UN_Person_Type] UNIQUE ([typePersonID])
);
Table Students
扩展 Person
:
CREATE TABLE [Students]
(
[personID] INT IDENTITY (1, 1) NOT NULL ,
[typePersonID] AS 1 PERSISTED, --type person = 1
[name] VARCHAR(50) NOT NULL,
CONSTRAINT [PK_Students] PRIMARY KEY CLUSTERED ([PersonID] ASC),
CONSTRAINT [FK_Students] FOREIGN KEY ([typePersonID])
REFERENCES [Person] ([typePersonID]),
);
Table Teachers
扩展 Person
:
CREATE TABLE [Teachers]
(
[PersonID] INT IDENTITY (1, 1) NOT NULL ,
[typePersonID] AS 2 PERSISTED, --type person = 2
[name] VARCHAR(50) NOT NULL,
CONSTRAINT [PK_Teachers ] PRIMARY KEY CLUSTERED ([PersonID] ASC),
CONSTRAINT [FK_Teachers ] FOREIGN KEY ([typePersonID])
REFERENCES [Person] ([typePersonID]),
);
Table Employers
扩展 Person
:
CREATE TABLE [Employers]
(
[personID] INT IDENTITY (1, 1) NOT NULL ,
[typePersonID] AS 3 PERSISTED, --type person = 3
[name] VARCHAR(50) NOT NULL,
CONSTRAINT [PK_Employers] PRIMARY KEY CLUSTERED ([PersonID] ASC),
CONSTRAINT [FK_Employers] FOREIGN KEY ([typePersonID])
REFERENCES [Person] ([typePersonID]),
);
Insert
数据创建类型为:
INSERT INTO [Person] ([typePersonID]) VALUES (1); --students
INSERT INTO [Person] ([typePersonID]) VALUES (2); --teachers
INSERT INTO [Person] ([typePersonID]) VALUES (3); --employer
INSERT INTO [Person] ([typePersonID]) VALUES (4); --supervisor
插入数据创建学生、教师和雇主:
INSERT INTO [Students] ([name]) VALUES ('John'); --students
INSERT INTO [Teachers] ([name]) VALUES ('Robert'); --teachers
INSERT INTO [Employers] ([name]) VALUES ('Ciara'); --employer
INSERT INTO [Supervisor] ([name]) VALUES ('Renata'); --supervisor
当我select只是使用这个脚本的名字
SELECT st.name, ts.name, es.name, sp.name
FROM Person p
INNER JOIN Students st ON p.personID= st.personID
INNER JOIN Teachers ts ON p.personID= ts.personID
INNER JOIN Employers es ON p.personID= es.personID
INNER JOIN Supervisor sp ON p.personID= sp.personID;
结果是
我想在 ListView 中显示所有这些 names 当我使用 <asp:SqlDataSource ID="SqlDataSource1" runat="server" />
从数据库中获取数据并使用 <asp:ListView ID="ListView1" DataSourceID="SqlDataSource1" runat="server" />
显示所有names在水平位置,如下图
有什么建议吗?
当你尝试这个时你会感到惊讶:
SELECT t.typePerson, sp.name
FROM Person p
INNER JOIN [TypesPerson] as t ON p.[typePersonID] = t.[typePersonID]
INNER JOIN Supervisor sp ON p.personID= sp.personID;
问题是你有两个问题:
1. 数据重复:typePersonID
为同一个人存储了两次。
2. 你在Person
中的personID
与其他table中的personID
没有任何关联。
本质上,最好只有 ONLY ONE Person
table,只需在此处添加名称列即可。它会解决你所有的问题。
此外,而不是
CONSTRAINT [PK_UN_Person_Type] UNIQUE ([typePersonID])
放
CONSTRAINT [FK_Person_TypesPerson] FOREIGN KEY ([typePersonID])
REFERENCES [TypesPerson] ([typePersonID]),
通过使用 Pivot
你可以达到你的结果。但简单的方法你可以试试。
SELECT st.name Name
FROM Person p
INNER JOIN Students st ON p.personID= st.personID
union
SELECT ts.name Name
FROM Person p
INNER JOIN Teachers ts ON p.personID= ts.personID
union
SELECT es.name Name
FROM Person p
INNER JOIN Employers es ON p.personID= es.personID
union
SELECT sp.name Name
FROM Person p
INNER JOIN Supervisor sp ON p.personID= sp.personID
我有一个问题,关于 select 仅 name 的 table 的列的 table 继承 table ] 在 SQL Server 2014 中。
即有四种类型的人(学生、教师、员工和主管) 并且所有这些都包含一个 id
用于从 Person
table 继承的每种类型的人以及后来的 name 必须被 select 编辑和展示。
Table TypePerson
:
CREATE TABLE [TypesPerson]
(
[typePersonID] INT NOT NULL,
[typePerson] VARCHAR(50) NOT NULL,
CONSTRAINT [PK_TypesPerson] PRIMARY KEY CLUSTERED ([typePersonID] ASC)
);
Insert
人的类型:
INSERT INTO [TypesPerson]
SELECT 1,'students' UNION ALL
SELECT 2,'teachers' UNION ALL
SELECT 3,'employer' UNION ALL
SELECT 4,'supervisor'
Table Person
:
CREATE TABLE [Person]
(
[personID] INT IDENTITY (1, 1) NOT NULL ,
[typePersonID] INT NOT NULL,
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED ([PersonID] ASC),
CONSTRAINT [PK_UN_Person_Type] UNIQUE ([typePersonID])
);
Table Students
扩展 Person
:
CREATE TABLE [Students]
(
[personID] INT IDENTITY (1, 1) NOT NULL ,
[typePersonID] AS 1 PERSISTED, --type person = 1
[name] VARCHAR(50) NOT NULL,
CONSTRAINT [PK_Students] PRIMARY KEY CLUSTERED ([PersonID] ASC),
CONSTRAINT [FK_Students] FOREIGN KEY ([typePersonID])
REFERENCES [Person] ([typePersonID]),
);
Table Teachers
扩展 Person
:
CREATE TABLE [Teachers]
(
[PersonID] INT IDENTITY (1, 1) NOT NULL ,
[typePersonID] AS 2 PERSISTED, --type person = 2
[name] VARCHAR(50) NOT NULL,
CONSTRAINT [PK_Teachers ] PRIMARY KEY CLUSTERED ([PersonID] ASC),
CONSTRAINT [FK_Teachers ] FOREIGN KEY ([typePersonID])
REFERENCES [Person] ([typePersonID]),
);
Table Employers
扩展 Person
:
CREATE TABLE [Employers]
(
[personID] INT IDENTITY (1, 1) NOT NULL ,
[typePersonID] AS 3 PERSISTED, --type person = 3
[name] VARCHAR(50) NOT NULL,
CONSTRAINT [PK_Employers] PRIMARY KEY CLUSTERED ([PersonID] ASC),
CONSTRAINT [FK_Employers] FOREIGN KEY ([typePersonID])
REFERENCES [Person] ([typePersonID]),
);
Insert
数据创建类型为:
INSERT INTO [Person] ([typePersonID]) VALUES (1); --students
INSERT INTO [Person] ([typePersonID]) VALUES (2); --teachers
INSERT INTO [Person] ([typePersonID]) VALUES (3); --employer
INSERT INTO [Person] ([typePersonID]) VALUES (4); --supervisor
插入数据创建学生、教师和雇主:
INSERT INTO [Students] ([name]) VALUES ('John'); --students
INSERT INTO [Teachers] ([name]) VALUES ('Robert'); --teachers
INSERT INTO [Employers] ([name]) VALUES ('Ciara'); --employer
INSERT INTO [Supervisor] ([name]) VALUES ('Renata'); --supervisor
当我select只是使用这个脚本的名字
SELECT st.name, ts.name, es.name, sp.name
FROM Person p
INNER JOIN Students st ON p.personID= st.personID
INNER JOIN Teachers ts ON p.personID= ts.personID
INNER JOIN Employers es ON p.personID= es.personID
INNER JOIN Supervisor sp ON p.personID= sp.personID;
结果是
我想在 ListView 中显示所有这些 names 当我使用 <asp:SqlDataSource ID="SqlDataSource1" runat="server" />
从数据库中获取数据并使用 <asp:ListView ID="ListView1" DataSourceID="SqlDataSource1" runat="server" />
显示所有names在水平位置,如下图
有什么建议吗?
当你尝试这个时你会感到惊讶:
SELECT t.typePerson, sp.name
FROM Person p
INNER JOIN [TypesPerson] as t ON p.[typePersonID] = t.[typePersonID]
INNER JOIN Supervisor sp ON p.personID= sp.personID;
问题是你有两个问题:
1. 数据重复:typePersonID
为同一个人存储了两次。
2. 你在Person
中的personID
与其他table中的personID
没有任何关联。
本质上,最好只有 ONLY ONE Person
table,只需在此处添加名称列即可。它会解决你所有的问题。
此外,而不是
CONSTRAINT [PK_UN_Person_Type] UNIQUE ([typePersonID])
放
CONSTRAINT [FK_Person_TypesPerson] FOREIGN KEY ([typePersonID])
REFERENCES [TypesPerson] ([typePersonID]),
通过使用 Pivot
你可以达到你的结果。但简单的方法你可以试试。
SELECT st.name Name
FROM Person p
INNER JOIN Students st ON p.personID= st.personID
union
SELECT ts.name Name
FROM Person p
INNER JOIN Teachers ts ON p.personID= ts.personID
union
SELECT es.name Name
FROM Person p
INNER JOIN Employers es ON p.personID= es.personID
union
SELECT sp.name Name
FROM Person p
INNER JOIN Supervisor sp ON p.personID= sp.personID