我需要创建一个名为 phonelist 的视图,其中列出了每个球员、他们的监护人、监护人的电话号码和他们所在的球队。我将如何编码。您应该在下面找到您需要的所有信息:
这些是我的表:
CREATE TABLE Person
(
personID INT NOT NULL,
name VARCHAR(50),
address VARCHAR(70),
phone VARCHAR(15),
email VARCHAR(30),
year INT,
PRIMARY KEY (personID)
);
CREATE TABLE Player
(
personID INT NOT NULL,
dateOfBirth DATE,
school VARCHAR(30),
PRIMARY KEY (personID),
FOREIGN KEY (personID) REFERENCES Person (personID)
);
CREATE TABLE Team
(
teamID INT NOT NULL,
tName VARCHAR(70),
ageRange VARCHAR(30),
PRIMARY KEY (teamID)
);
CREATE TABLE Coach
(
personID INT NOT NULL,
dateBeganCoaching DATE,
PRIMARY KEY (personID),
FOREIGN KEY (personID) REFERENCES Person (personID)
);
CREATE TABLE coachTeam
(
personID INT NOT NULL,
teamID INT NOT NULL,
PRIMARY KEY (personID, teamID),
FOREIGN KEY (personID) REFERENCES Coach (personID),
FOREIGN KEY (teamID) REFERENCES Team (teamID)
);
CREATE TABLE playerTeam
(
personID INT NOT NULL,
teamID INT NOT NULL,
PRIMARY KEY (personID, teamID),
FOREIGN KEY (personID) REFERENCES Player (personID),
FOREIGN KEY (teamID) REFERENCES Team (teamID)
);
CREATE TABLE Guardian
(
parentID INT NOT NULL,
childID INT NOT NULL,
PRIMARY KEY (parentID, childID),
FOREIGN KEY (parentID) REFERENCES Person (personID),
FOREIGN KEY (childID) REFERENCES Person (personID)
);
CREATE TABLE Qualification
(
qualificationID INT NOT NULL,
qName VARCHAR(30),
PRIMARY KEY (qualificationID)
);
CREATE TABLE coachQualification
(
qualificationID INT NOT NULL,
personID INT NOT NULL,
PRIMARY KEY (personID, qualificationID),
FOREIGN KEY (qualificationID) REFERENCES Qualification (qualificationID),
FOREIGN KEY (personID) REFERENCES Coach (personID)
);
我将如何做到这一点,以便我仅使用 person 表来引用玩家和监护人,以及我将如何做到这一点,以便如果玩家有多个监护人,两个监护人将被放置在同一行而不是创建输出中的一个全新行显示两位监护人的感谢。
我尝试了以下方法,但出现了错误:
CREATE VIEW phonelist AS
SELECT Child.name, tName, Parent.name, Parent.phone
FROM playerTeam INNER JOIN Person AS Child ON playerTeam.personID = Child.personID
INNER JOIN Team ON playerTeam.teamID = Team.teamID
INNER JOIN Person AS Parent ON Guardian.parentID = Parent.personID;
Select * FROM phonelist ORDER BY name ASC;