Mail.ruПочтаМой МирОдноклассникиВКонтактеИгрыЗнакомстваНовостиКалендарьОблакоЗаметкиВсе проекты

Помогите PostgresSQL, не создаются оставшиеся таблицы, что не так в коде помогите пожалуйста

Kotova Ученик (20), на голосовании 8 месяцев назад
CREATE TABLE IF NOT EXISTS Employee (
ID_Employee INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
LastName VARCHAR(100) NOT NULL,
FirstName VARCHAR(100) NOT NULL,
MiddleName VARCHAR(100),
Positions VARCHAR(100) NOT NULL,
DateOfBirth DATE NOT NULL,
ContactInformation VARCHAR(200) NOT NULL,
ID_EmployeeStatus INT NOT NULL REFERENCES EmployeeStatus (ID_EmployeeStatus)
);
CREATE TABLE IF NOT EXISTS TrainingEvaluation (
ID_TrainingEvaluation INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
Evaluation INT NOT NULL,
Commentss TEXT NOT NULL,
DateOfEvaluation DATE NOT NULL
);
CREATE TABLE IF NOT EXISTS Certificate (
ID_Certificate INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
Title VARCHAR(100) NOT NULL,
DateOfIssue DATE NOT NULL,
ID_Employee_TrainingEvent INT NOT NULL REFERENCES EmployeeTrainingEvent (ID_Employee_TrainingEvent)
);
CREATE TABLE IF NOT EXISTS TrainingEvent (
ID_TrainingEvent INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
Title VARCHAR(100) NOT NULL,
Topic VARCHAR(100) NOT NULL,
Duration TIME NOT NULL,
StartDate DATE NOT NULL,
Description VARCHAR(100) NOT NULL,
ID_Department INT NOT NULL REFERENCES Department (ID_Department),
ID_EventType INT NOT NULL REFERENCES EventType (ID_EventType),
ID_Instructor INT NOT NULL REFERENCES Instructor (ID_Instructor),
ID_TrainingProgram INT NOT NULL REFERENCES TrainingProgram (ID_TrainingProgram)
);
CREATE TABLE IF NOT EXISTS Department (
ID_Department INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
Title VARCHAR(100) NOT NULL,
Address VARCHAR(100) NOT NULL,
ContactInformation VARCHAR(100) NOT NULL
);
CREATE TABLE IF NOT EXISTS EventType (
ID_EventType INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
Title VARCHAR(100) NOT NULL
);
CREATE TABLE IF NOT EXISTS TrainingProgram (
ID_TrainingProgram INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
Title VARCHAR(100),
Description TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS Instructor (
ID_Instructor INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
FullName VARCHAR(100) NOT NULL,
Positions VARCHAR(100) NOT NULL,
ContactInformation VARCHAR(200) NOT NULL
);
CREATE TABLE IF NOT EXISTS EmployeeTrainingEvent (
ID_Employee_TrainingEvent INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
ID_Employee INT NOT NULL REFERENCES Employee (ID_Employee),
ID_TrainingEvent INT NOT NULL REFERENCES TrainingEvent (ID_TrainingEvent),
ID_TrainingEvaluation INT NOT NULL REFERENCES TrainingEvaluation (ID_TrainingEvaluation),
DateOfParticipation DATE NOT NULL
);
CREATE TABLE IF NOT EXISTS EmployeeStatus (
ID_EmployeeStatus INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
StatusName VARCHAR(100) NOT null
);
Голосование за лучший ответ
Анонимус Мудрец (14718) 9 месяцев назад
Проблема в вашем коде заключается в том, что вы создаете таблицы в неправильном порядке. Определенные таблицы должны существовать, прежде чем вы ссылаетесь на них во внешних ключах других таблиц.

 CREATE TABLE IF NOT EXISTS Department ( 
ID_Department INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
Title VARCHAR(100) NOT NULL,
Address VARCHAR(100) NOT NULL,
ContactInformation VARCHAR(100) NOT NULL
);

CREATE TABLE IF NOT EXISTS EventType (
ID_EventType INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
Title VARCHAR(100) NOT NULL
);

CREATE TABLE IF NOT EXISTS TrainingProgram (
ID_TrainingProgram INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
Title VARCHAR(100),
Description TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS Instructor (
ID_Instructor INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
FullName VARCHAR(100) NOT NULL,
Positions VARCHAR(100) NOT NULL,
ContactInformation VARCHAR(200) NOT NULL
);

CREATE TABLE IF NOT EXISTS EmployeeStatus (
ID_EmployeeStatus INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
StatusName VARCHAR(100) NOT null
);

CREATE TABLE IF NOT EXISTS Employee (
ID_Employee INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
LastName VARCHAR(100) NOT NULL,
FirstName VARCHAR(100) NOT NULL,
MiddleName VARCHAR(100),
Positions VARCHAR(100) NOT NULL,
DateOfBirth DATE NOT NULL,
ContactInformation VARCHAR(200) NOT NULL,
ID_EmployeeStatus INT NOT NULL REFERENCES EmployeeStatus (ID_EmployeeStatus)
);

CREATE TABLE IF NOT EXISTS TrainingEvaluation (
ID_TrainingEvaluation INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
Evaluation INT NOT NULL,
Commentss TEXT NOT NULL,
DateOfEvaluation DATE NOT NULL
);

CREATE TABLE IF NOT EXISTS TrainingEvent (
ID_TrainingEvent INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
Title VARCHAR(100) NOT NULL,
Topic VARCHAR(100) NOT NULL,
Duration TIME NOT NULL,
StartDate DATE NOT NULL,
Description VARCHAR(100) NOT NULL,
ID_Department INT NOT NULL REFERENCES Department (ID_Department),
ID_EventType INT NOT NULL REFERENCES EventType (ID_EventType),
ID_Instructor INT NOT NULL REFERENCES Instructor (ID_Instructor),
ID_TrainingProgram INT NOT NULL REFERENCES TrainingProgram (ID_TrainingProgram)
);

CREATE TABLE IF NOT EXISTS EmployeeTrainingEvent (
ID_Employee_TrainingEvent INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
ID_Employee INT NOT NULL REFERENCES Employee (ID_Employee),
ID_TrainingEvent INT NOT NULL REFERENCES TrainingEvent (ID_TrainingEvent),
ID_TrainingEvaluation INT NOT NULL REFERENCES TrainingEvaluation (ID_TrainingEvaluation),
DateOfParticipation DATE NOT NULL
);

CREATE TABLE IF NOT EXISTS Certificate (
ID_Certificate INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
Title VARCHAR(100) NOT NULL,
DateOfIssue DATE NOT NULL,
ID_Employee_TrainingEvent INT NOT NULL REFERENCES EmployeeTrainingEvent (ID_Employee_TrainingEvent)
);
Похожие вопросы