从MySQL中的多个表创建视图

时间:2014-07-25 16:07:34

标签: mysql sql database create-view

我正在尝试从下表中创建一个视图,该视图提供了classId,semester,教授它的名称,以及该学期提供的部分数量。我尝试使用SQL Query创建视图:

CREATE SQL SECURITY INVOKER VIEW ClassesView AS SELECT Qualified.ClassId, Teaches.Semester, CONCAT(Professor.FName, Professor.LName) AS Name, Teaches.Section FROM Teaches, Professor, Qualified WHERE Qualified.EmpId = Professor.EmpId, Qualified.ClassId = Teaches.Class;

但那并没有奏效。我如何从下表中创建所需的视图?

CREATE TABLE Class
(
    ClassId VARCHAR(6),
    Description VARCHAR(30),
    NumCredits Integer,
    Prereq VARCHAR(20),
    PRIMARY KEY(ClassId)
);

CREATE TABLE Section
(
    ClassId VARCHAR(6),
    SecNo CHAR(10),
    Semester CHAR(4),
    ClassRoom VARCHAR(6),
    TimeOffered VARCHAR(18),
    PRIMARY KEY(SecNo),
    FOREIGN KEY(ClassId) REFERENCES Class(ClassId)  
);

CREATE TABLE Professor
(
    EmpId INTEGER,
    FName VARCHAR(10),
    LName VARCHAR(10),
    Dept VARCHAR(2),
    QualClass VARCHAR(40),
    PRIMARY KEY (EmpId)
);

CREATE TABLE Teaches
(
    Class VARCHAR(5),
    Section INTEGER,
    Semester CHAR(4),
    EmpId INTEGER,
    FOREIGN KEY (EmpId) REFERENCES Professor(EmpId)
);

CREATE TABLE Qualified
(
    EmpId INTEGER,
    ClassId VARCHAR(5)
);

谢谢。任何帮助都会非常感激,因为它将帮助我学习MySQL。

1 个答案:

答案 0 :(得分:0)

语法错误(用逗号替换逗号)

CREATE SQL SECURITY INVOKER VIEW ClassesView AS 
SELECT Qualified.ClassId, Teaches.Semester, CONCAT(Professor.FName, Professor.LName) AS Name, Teaches.Section 
FROM Teaches, Professor, Qualified 
WHERE Qualified.EmpId = Professor.EmpId AND Qualified.ClassId = Teaches.Class;