i getting this error: The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "foreign_key1" - sql

CREATE TABLE singer(
name varchar(50) primary key,
email varchar(50) not null,
account_number varchar(50) not null,
balance int not null
);
create table song(
id int primary key,
name varchar(50) not null,
singer varchar(50) foreign key references singer,
producer varchar(50) not null,
album varchar(50) not null,
total_sell int not null
);
CREATE TABLE producer(name VARCHAR(20) PRIMARY KEY);
ALTER TABLE song ALTER COLUMN producer VARCHAR(20);
ALTER TABLE song ADD CONSTRAINT foreign_key1 FOREIGN KEY(producer) REFERENCES producer;

In MySQL you can use this:
CREATE TABLE producer(
name varchar(20),
PRIMARY KEY (name)
);
CREATE TABLE song(
id int,
name varchar(50) not null,
singer varchar(50) not null,
producer varchar(50) not null,
album varchar(50) not null,
total_sell int not null,
PRIMARY KEY (`id`),
CONSTRAINT `foreign_key1` FOREIGN KEY (`producer`) REFERENCES `producer` (`name`)
);
OR:
ALTER TABLE `song`
ADD CONSTRAINT `foreign_key1`
FOREIGN KEY (`producer`)
REFERENCES `producer` (`name`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
you can see Here.
I can advise you to declare the same length as the name column. es. varchar(50)

Related

Dbeaver: Why does code not work correctly?

It doesn't create all my tables and also says I have syntax errors. Can anyone help??
CREATE table project (
project_id int not null,
budget_id int not null,
division_id int not null,
project_name varchar(40),
project_begin_date date,
project_end_date date
);
ALTER table project
add constraint pk_project_project_id
primary key(project_id)
;
ALTER table project
add constraint fk_project_budget_id
foreign key(budget_id) references budget(budget_id)
;
ALTER table project
add constraint fk_project_division_id
foreign key (division_id) references division(division_id)
;
CREATE table division(
division_id int not null,
division_name varchar(40),
constraint pk_division_division_id primary key(division_id)
);
CREATE table committee(
committee id int not null,
committee_name varchar(40),
constraint pk_committee_committee_id primary key(committe_id)
);
create table employee(
employee_id int not null,
given_name varchar(40),
middle_name varchar(40),
family_name varchar(40),
form_of_address varchar(10),
name_suffix varchar(25),
work_phone_number varchar(20),
hourly_budget_rate numeric(8,2),
constraint pk_employee_employee_id primary key(employee_id)
);
CREATE table budget(
budget_id int not null,
budget_description varchar(100),
constraint pk_budget_budget_id primary key(budget_id)
);
CREATE table section(
section_id int not null,
division_id int,
budget_id int,
section_name varchar(40),
constraint pk_section_section_id primary key(section_id),
constraint pk_section_buget_id foreign key(budget_id) references budget(budget_id),
constraint pk_section_division_id foreign key(division_id) references division(division_id)
);
CREATE table employee_job_assignment(
job_assignment_id int not null,
employee_id int not null,
section_id int not null,
begin_datetime date,
end_datetime date,
job_title varchar(100),
constraint pk_employee_job_assignment primary key(job_assignment_id),
constraint pk_employee_job_assignment foreign key(employee_id) references employee(employee_id),
constraint pk_employee_job_assignment foreign key(section_id) references section(section_id)
);
CREATE table budget_category(
budget_category_code char(5),
budget_category_description char(100),
constraint pk_budget_category primary key(budget_category_code)
);
CREATE table budget_charge(
transaction_id int not null,
budget_category_code char(5),
budget_id int not null,
transaction_date date,
transaction_description varchar(100),
transaction_type char(5),
constraint pk_budget_charge_transaction_id primary key(transaction_id),
constraint pk_budget_charge_budget_category_code foreign key(budget_category_code) references budget_category(budget_category_code),
constraint pk_budget_charge_budget_id foreign key(budget_id) references budget(budget)
);
CREATE office(
office_id int not null,
office_type char(1),
office_building char(5),
office_location varchar(20),
constraint pk_office primary key(office_id)
);
CREATE table employee_office(
office id int not null,
employee_id int not null,
constraint pk_employee_office_office_id foreign key(office_id) references office(office_id),
constraint pk_employee_office_employee_id foreign key(employee id) references employee(employee_id)
);
CREATE table project_employee(
employee_id int not null,
project_id int not null,
project_role varchar(25),
constraint pk_project_employee_employee_id foreign key(employee_id) references employee(employee_id),
constraint pk_project_employee_project_id foreign key(project_id) references project(project_id)
);
CREATE table purchase_expense_charge(
transaction_id int not null,
purchase_expense_dollars numeric(8,2),
constraint pk_purchase_expense_charge_transaction_id primary key(transaction_id),
constraint pk_purchase_expense_charge_transaction_id foreign key(transaction_id) references budget_charge(transaction_id)
);
CREATE table hours_charge(
transaction_id int not null,
employee_id int not null,
hours_charged numeric(7,2),
time_period_begin_date date,
time_period_end_date date,
constraint pk_hours_charge_transaction_id primary key(transaction_id),
constraint pk_hours_charge_transaction_id foreign key(transaction_id) references budget_charge(transaction_id),
constraint pk_hours_charge_employee_id foreign key(employee_id) references employee(employee_id)
);
CREATE table budget_line_item(
budget_category_code char(5),
budget_id int not null,
line_item_budget_dollars numeric(8,2),
constraint pk_budget_line_item_budget_category_code foreign key(budget_category_code) references budget_category(budget_category_code),
constraint pk_budget_line_item_budget_category_code foreign key(budget_id) references budget(budget_id)
);
CREATE table travel_expense_charge(
transaction_id int not null,
employee_id int not null,
trip_expense_dollars NUMERIC(8,2),
trip_destination VARCHAR(100),
trip_begin_date date,
trip_end_date date,
constraint pk_travel_expense_charge_transaction_id primary key (transaction_id),
constraint pk_travel_expense_charge_transaction_id foreign key (transaction_id) references budget_charge(transaction_id),
constraint pk_travel_expense_charge_employee_id foreign key (employee_id) references employee(employee_id)
);
CREATE table committee_member(
committee_id int not null,
employee_id int not null,
committee_role varchar(25),
constraint pk_committee_member_committee_id foreign key(committee_id) references committee(committee_id)
constraint pk_comittee_member_employee_id foreign key(employee id) references employee(employee_id)
);

There is no unique constraint matching given keys for referenced table " exam_subjects"

CREATE TABLE student
(
student_id INT PRIMARY KEY,
first_name VARCHAR(40) NOT NULL,
last_name VARCHAR(40) NOT NULL,
birth_day DATE NOT NULL,
sex VARCHAR(1) NOT NULL,
student_email_address VARCHAR(40) NOT NULL UNIQUE,
student_password VARCHAR(10) NOT NULL UNIQUE,
student_nick_name VARCHAR(10) NOT NULL,
student_qualification VARCHAR(10) NOT NULL,
student_documents VARCHAR(255) NOT NULL,
student_image VARCHAR(100) NOT NULL
);
CREATE TABLE student_feedback
(
sr_no BIGSERIAL PRIMARY KEY,
student_id INT NOT NULL,
feedback_type VARCHAR(10) NOT NULL,
feedback_text VARCHAR(200) NOT NULL,
FOREIGN KEY (student_id) REFERENCES student(student_id)
);
CREATE TABLE online_exam
(
exam_id INT PRIMARY KEY,
exam_title VARCHAR(20) UNIQUE NOT NULL,
exam_duration_min INT NOT NULL,
total_questions INT NOT NULL,
marks_per_right_answer INT NOT NULL,
marks_per_wrong_answer INT NOT NULL,
passing_marks INT NOT NULL,
exam_status VARCHAR(2)
);
CREATE TABLE exam_subjects
(
sub_id INT,
exam_id INT,
sub_name VARCHAR(20) NOT NULL,
sub_desc VARCHAR(20) NOT NULL,
UNIQUE(sub_id,exam_id),
PRIMARY KEY(sub_id,exam_id),
FOREIGN KEY (exam_id) REFERENCES online_exam(exam_id) ON DELETE CASCADE
);
CREATE TABLE sub_questions
(
sub_id1 INT,
ques_id INT,
ques_text VARCHAR(150) NOT NULL,
ques_attachments VARCHAR(255),
option_1 VARCHAR(20) NOT NULL,
option_2 VARCHAR(20) NOT NULL,
option_3 VARCHAR(20) NOT NULL,
option_4 VARCHAR(20) NOT NULL,
UNIQUE(sub_id1,ques_id),
PRIMARY KEY (sub_id1,ques_id),
FOREIGN KEY (sub_id1) REFERENCES exam_subjects(sub_id) ON DELETE CASCADE
);
CREATE TABLE ques_answers
(
ans_id INT,
ques_id INT,
ans VARCHAR(20) NOT NULL,
PRIMARY KEY (ans_id,ques_id),
FOREIGN KEY (ques_id) REFERENCES sub_questions(ques_id) ON DELETE CASCADE
);
CREATE TABLE exam_registration
(
student_id INT,
exam_id INT,
exam_date_time TIMESTAMP NOT NULL,
PRIMARY KEY (student_id,exam_id),
FOREIGN KEY (student_id) REFERENCES student(student_id) ON DELETE CASCADE,
FOREIGN KEY (exam_id) REFERENCES online_exam(exam_id) ON DELETE CASCADE
);
CREATE TABLE exam_result
(
student_id INT,
exam_id INT,
sub_id INT,
marks_scored INT NOT NULL,
correct_ques INT NOT NULL,
wrong_ques INT NOT NULL,
grade VARCHAR(10) NOT NULL,
PRIMARY KEY(student_id,exam_id,sub_id),
FOREIGN KEY (student_id) REFERENCES student(student_id) ON DELETE CASCADE,
FOREIGN KEY (exam_id) REFERENCES online_exam(exam_id) ON DELETE CASCADE,
FOREIGN KEY (sub_id) REFERENCES exam_subjects(sub_id) ON DELETE CASCADE
);
Output :
CREATE TABLE,
CREATE TABLE,
CREATE TABLE,
CREATE TABLE,
There is no unique constraint matching given keys for referenced table "exam_subjects", relation "sub_questions" do not exist,
CREATE TABLE,
There is no unique constraint matching given keys for referenced table "exam_subjects".
Why am I getting "there is no unique..." error in postgresql? How to solve it? Please help.
In your table exam_subjects the primary key is a compossed key (PRIMARY KEY(sub_id, exam_id))
Therefore, your table sub_questions must have both columns to create the constraint, otherwise you get the error about unique constraints (or you can create a unique field acting as primary key, and create a unique index on both columns sub_id and exam_id)

SQL Missing left/right parenthesis

I keep getting a missing right or left parenthesis error when I try to create my tables with a foreign key, but when I remove the foreign key my code has no problem creating the tables. I need the code to run with the foreign keys.
--deletes respective tables previously created
drop table DEPARTMENT;
drop table POSITION;
drop table EMPLOYEE;
drop table COMPANY;
drop table DEGREE;
drop table GRAD_INFO;
drop table STUDENT;
CREATE TABLE Department
(
Dept_Name varchar(255) PRIMARY KEY, --primary key
Dept_Phone_Num numeric(35) not null
);
CREATE TABLE Position
(
Position_Name varchar(255) NOT NULL PRIMARY KEY, --primary key
Position_Salary decimal(10, 2) not null,
Employee_num numeric(35) not null,
CONSTRAINT fk_Dept_Name
FOREIGN KEY Dept_Name REFERENCES Department (Dept_Name) --foreign key
);
CREATE TABLE Employee
(
EmployeeID INT NOT NULL PRIMARY KEY, --primary key
Employee_Salary DECIMAL(10,2) DEFAULT NULL,
Employee_FName VARCHAR(30) DEFAULT NULL,
Employee_LName VARCHAR(30) NOT NULL,
Employee_Address VARCHAR(30) DEFAULT NULL,
Employee_Phone_Num INT(10) DEFAULT NULL,
Employee_Email VARCHAR(30) NOT NULL,
Employee_Status VARCHAR(10) NOT NULL,
CONSTRAINT Position_Name
FOREIGN KEY Position_Name REFERENCES Position(Position_Name), --foreign key
CONSTRAINT fk_Dept_Name
FOREIGN KEY Dept_Name REFERENCES Department(Dept_Name) --foreign key
);
CREATE TABLE Company
(
Comp_Name VARCHAR(20) NOT NULL PRIMARY KEY, --primary key
Position_Offer VARCHAR(30) NOT NULL,
Salary_Offer DECIMAL(10,2) NOT NULL,
Signing_Bonus DECIMAL(10,2) DEFAULT NULL,
Comp_Phone_Num INT(10) NOT NULL,
Comp_Address VARCHAR(30) NOT NULL,
CONSTRAINT fk_Employee_ID
FOREIGN KEY Employee_ID REFERENCES Employee (Employee_ID), --foreign key
CONSTRAINT fk_Dept_Name
FOREIGN KEY Dept_Name REFERENCES Department (Dept_Name) --foreign key
);
CREATE TABLE Degree
(
Degree_Type varchar(255) PRIMARY KEY, --primary key
Academic_Status varchar(255) not null,
Academic_Level varchar(255) not null,
Major varchar(255) not null,
Minor varchar(255) default null
);
CREATE TABLE Gead_Info
(
Grad_Status VARCHAR(255) NOT NULL PRIMARY KEY, --primary key
College_Name VARCHAR(255) NOT NULL,
Num_Of_Degrees NUMERIC(10) NOT NULL,
Grad_Date NUMERIC(6) NOT NULL,
CONSTRAINT fk_Degree_Type
FOREIGN KEY Degree_Type REFERENCES Degree(Degree_Type) --foreign key
);
CREATE TABLE Student
(
Student_ID NUMERIC(10) NOT NULL PRIMARY KEY, --primary key
Student_FName VARCHAR(20) NOT NULL,
Student_LName VARCHAR(20) NOT NULL,
Student_Address VARCHAR(20) NOT NULL,
Student_Email VARCHAR(20) NOT NULL,
Student_Phone_Num VARCHAR(10) NOT NULL,
CONSTRAINT fk_Comp_Name
FOREIGN KEY Comp_Name REFERENCES Company(Comp_Name), --foreign key
CONSTRAINT fk_Grad_Status
FOREIGN KEY Grad_Status REFERENCES Gead_Info(Grad_Status) --foreign key
);
The degree table is the only table where I don't get an error.
Should I use an alter table instead after I create all the tables or is there another way to solve my problem?
edit: updated code
I think your most immediate issue is with the Department table not having a comma between the two CONSTRAINT clauses. You might also run into issues with your Position table's CONSTRAINT clause referencing the Position table instead of the Employee table, although honestly I don't think you need that relationship defined at all since the Employee table already has a foreign key constraint with Position. And like what was stated earlier, your table definitions aren't in the right order so you have some tables referencing other tables before those others are created.
Give this a shot to see if the syntax works. I've added the column definition for the foreign key in Position, put parentheses around the foreign key column name in the CONSTRAINT clause, and removed the space between Department and the left paren in the CONSTRAINT clause. If this script works, make the same changes to your other table defs.
CREATE TABLE Department(
Dept_Name varchar(255) Primary Key, --primary key
Dept_Phone_Num numeric(35) not null
);
CREATE TABLE Position(
Position_Name varchar(255) not null Primary Key, --primary key
Position_Salary decimal(10, 2) not null,
Employee_num numeric(35) not null,
Dept_Name varchar(255) not null,
CONSTRAINT fk_Dept_Name FOREIGN KEY (Dept_Name) REFERENCES Department(Dept_Name) --foreign key
);

Foreign key references invalid column error

I'm trying to run this query but SQL -erver stopped me with this error:
Foreign key 'FK__food__groupid__3F115E1A' references invalid column 'groupid' in referenced table 'sub'.
This is my query:
create table menu
(
valedid int primary key not null,
name nvarchar(50) not null,
)
create table sub
(
qroupid int primary key not null,
groupname nvarchar(50) not null,
valedid int not null,
foreign key(valedid) references menu (valedid),
)
create table food
(
foodid int primary key not null,
radif int identity(1,1) not null,
qeymat int not null,
name nvarchar(100) not null,
groupid int not null,
foreign key(groupid) references sub(groupid),
)
You create the column with name qroupid and not groupid in table sub

SQL error 150 on table creation

I know the error 150 is related to foreign keys, but i can't figure out what wrong here.
CREATE TABLE IF NOT EXISTS User(
id_user INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
nom VARCHAR(255) NOT NULL,
prenom VARCHAR(255) NOT NULL,
naissance DATE NOT NULL,
email VARCHAR(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS Livre(
id_livre INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
titre VARCHAR(255) NOT NULL,
parution DATE NOT NULL,
id_edit INT NOT NULL,
id_user INT NOT NULL,
FOREIGN KEY ( id_edit)
REFERENCES Editeur(id_edit)
ON DELETE CASCADE,
FOREIGN KEY ( id_user)
REFERENCES User(id_user)
ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS Publication(
id_pub INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
texte TEXT NOT NULL,
date_pub DATE NOT NULL,
titre_pub VARCHAR(255) NOT NULL,
id_livre INT NOT NULL,
id_user INT NOT NULL,
FOREIGN KEY ( id_user)
REFERENCES User(id_user)
ON DELETE CASCADE,
FOREIGN KEY (id_livre)
REFERENCES Livre(id_livre)
ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS Editeur(
id_edit INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
nom VARCHAR(255) NOT NULL,
pays VARCHAR(255) NOT NULL,
adresse VARCHAR(255) NOT NULL,
tel VARCHAR(255) NOT NULL
);
The error occurs when i try to create tables Livre and Publication.
Types are the same so it doesn't seem to be the problem.
By mindful of the order in which you create the tables.
For instance, your table creation statement for Livre defines a foreign key on Editeur. But the table Editeur has not been created yet.
Adjust the create table statement ordering as required.