Foreign key 'id_client' references invalid column 'id_client' in referencing table 'nrcomanda' - sql

Foreign key 'id_client' references invalid column 'id_client' in
referencing table 'nrcomanda'.
use Logistica
create table client
(
id_client int primary key identity(1,1),
nume varchar(20) not null,
prenume varchar(20) not null,
id_nrc int foreign key references nrcomanda(id_nrc)
)
create table categorie
(
id_categorie int primary key identity(1,1),
categorie varchar(50),
)
create table pachet
(
id_pachet int primary key identity(1,1),
tip_pachet varchar(50)
)
create table transport
(
id_transport int primary key identity(1,1),
tip_transport varchar(20)
)
create table nrcomanda
(
id_nrc int primary key identity(1,1),
nrcomanda varchar not null,
greutate decimal(7,2),
asigurare varchar(50),
foreign key(id_client) references client(id_client),
foreign key(id_categorie) references categorie(id_categorie),
foreign key(id_pachet) references pachet(id_pachet),
foreign key(id_transport) references transport(id_transport),
foreign key(id_adresa) references adresa(id_adresa)
)
create table raion
(
id_raion int primary key identity(1,1),
nume varchar(50)
)
create table localitate
(
id_loc int primary key identity(1,1),
id_raion int,
nume varchar(50)
foreign key(id_raion) references raion(id_raion)
)
create table adresa
(
id_adresa int primary key identity(1,1),
id_raion int foreign key references raion(id_raion),
id_loc int foreign key references localitate(id_loc),
id_nrc int foreign key references nrcomanda(id_nrc),
id_tara int foreign key references tara(id_tara),
strada varchar(30),
nr varchar(6),
ap varchar(6),
bloc varchar(6),
activ bit default 1
)
create table tara
(
id_tara int primary key identity(1,1),
nume varchar(20)
)

i think you have miss some column definitions in your create table :
create table nrcomanda
(
id_nrc int primary key identity(1,1),
nrcomanda varchar not null,
greutate decimal(7,2),
asigurare varchar(50),
Id_client int,
Id_categorie int,
...
Id_adresa int,
foreign key(id_client) references client(id_client),
foreign key(id_categorie) references categorie(id_categorie),
foreign key(id_pachet) references pachet(id_pachet),
foreign key(id_transport) references transport(id_transport),
foreign key(id_adresa) references adresa(id_adresa)
)

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)
);

Sql Fiddle errors

I am trying to create SQL statements for an assignment in SQL Fiddle and I keep getting the error "Cannot add foreign key constraint". I have tried various things but I recieve different errors when I change things. What am I doing wrong?
CREATE TABLE Person (
ID int NOT NULL,
FName varchar(255),
LName varchar(255),
Preferred_Name varchar(255),
PRIMARY KEY (ID)
)
;
CREATE TABLE Song (
ID varchar(255) NOT NULL,
Title varchar(255),
Run_Time varchar(255),
Lyrics varchar(255),
LeadID int,
FOREIGN KEY (LeadID) REFERENCES Person(ID),
PRIMARY KEY (ID)
)
;
CREATE TABLE Album (
Title varchar(255) NOT NULL,
Run_Time int,
Release_Year TIMESTAMP,
PRIMARY KEY (Title)
)
;
CREATE TABLE Has (
Album_Title varchar(255),
Song_Title varchar(255),
FOREIGN KEY (Album_Title) REFERENCES Album(Title),
FOREIGN KEY (Song_Title) REFERENCES Song(ID)
)
;
CREATE TABLE Part_Of (
PersonID int,
SongID int,
Role varchar(255) NOT NULL,
FOREIGN KEY (PersonID) REFERENCES Person(ID),
FOREIGN KEY (SongID) REFERENCES Song(ID),
PRIMARY KEY (Role)
)
;
In the Song and Has tables you defined Song.ID and Has.Song_Title as a varchar(255). In the Part_Of table you defined SongID as an int. This is why the foreign key is failing. Use the same datatype in all tables (INT seems like a good option) to fix this.
CREATE TABLE Song (
ID varchar(255) NOT NULL, <------
Title varchar(255),
Run_Time varchar(255),
Lyrics varchar(255),
LeadID int,
FOREIGN KEY (LeadID) REFERENCES Person(ID),
PRIMARY KEY (ID)
)
;
CREATE TABLE Has (
Album_Title varchar(255),
Song_Title varchar(255), <-----
FOREIGN KEY (Album_Title) REFERENCES Album(Title),
FOREIGN KEY (Song_Title) REFERENCES Song(ID)
)
;
CREATE TABLE Part_Of (
PersonID int,
SongID int, <-----
Role varchar(255) NOT NULL,
FOREIGN KEY (PersonID) REFERENCES Person(ID),
FOREIGN KEY (SongID) REFERENCES Song(ID),
PRIMARY KEY (Role)
)
;

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)

Relationship 1 to n

create database shop1
use shop1
create table mathang
(
MatHangID INT primary key not null,
TenMatHang varchar(50),
SoLuong int not null,
Price int not null,
)
create table nhacungcap
(
MatHangID INT foreign key references mathang,
TenNhaCungCap varchar(50) ,
DiaChi varchar(100),
SoDienThoai int ,
CONSTRAINT pk_M_CC primary key (MatHangID)
)
create table khachhang
(
KhachHangID int not null primary key,
TenKhachHang varchar(50) not null,
[DiaChi] varchar(100) not null,
[SoDienThoai] varchar(50) not null,
)
create table donhang
(
DonHangID int references to khachhang(KhachHangID),
TenDonHang varchar(50),
SoLuong int,
CONSTRAINT pk_DHID primary key (DonHangID)
)
I can not find errors with this, it shows the foreign key error to add this relation ship. Any one please help.
Remove the to in
...
DonHangID int references to khachhang(KhachHangID)
...
to get
...
DonHangID int references khachhang(KhachHangID)
...
. The to doesn't belong there syntactically.

SQL Server 2012 creating tables and values

For some odd reason I can't create a table and because if that I can't insert any values into those tables. This is a new database and I am having a bit of a brain fart.. any help??? thanks
CREATE TABLE Customers
(
CustomerID INT PRIMARY KEY IDENTITY,
CustomerFName NVARCHAR(20),
CustomerLName NVARCHAR(25),
DateOfTravel DATETIME,
TravelLocation NVARCHAR(25),
AgencyID NVARCHAR(25) FOREIGN KEY (Agencies)
)
Example of a working code
create table Agencies
(
AgencyID nvarchar(20) primary key,
AgName nvarchar(40),
AgAddress nvarchar(40),
AgPhone int
)
CREATE TABLE Customers
(
CustomerID INT PRIMARY KEY IDENTITY,
CustomerFName NVARCHAR(20),
CustomerLName NVARCHAR(25),
DateOfTravel DATETIME,
TravelLocation NVARCHAR(25),
AgencyID NVARCHAR(20) FOREIGN KEY REFERENCES Agencies(AgencyID)
)
Hope it helps!
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY IDENTITY,
CustomerFName NVARCHAR(20),
CustomerLName NVARCHAR(25),
DateOfTravel DATETIME,
TravelLocation NVARCHAR(25),
AgencyID NVARCHAR(25) FOREIGN KEY REFERENCES Agencies(Your_Agencies_ID_COLUMN)
)
There two ways of creating foreign key constrain:
creating a foreign key constraint within the table definition
CREATE TABLE [dbo].[DataSource]
(
[SurveyInstanceID] BIGINT
,[ProtoQuestionID] INT
,[Pts] TINYINT
,[PtsOf] TINYINT
, CONSTRAINT [PK_DataSource] PRIMARY KEY ([SurveyInstanceID], [ProtoQuestionID])
);
CREATE TABLE [dbo].[DataSourceComments]
(
[SurveyInstanceID] BIGINT
,[ProtoQuestionID] INT
,[Comments] NVARCHAR(MAX)
,CONSTRAINT [FK_DataSourceComments_DataSource_SurveyInstanceID_ProtoQuestionID]
FOREIGN KEY ([SurveyInstanceID], [ProtoQuestionID])
REFERENCES [dbo].[DataSource] ([SurveyInstanceID], [ProtoQuestionID])
);
creating a foreign key constraint for existing table
CREATE TABLE [dbo].[DataSource]
(
[SurveyInstanceID] BIGINT
,[ProtoQuestionID] INT
,[Pts] TINYINT
,[PtsOf] TINYINT
,CONSTRAINT [PK_DataSource] PRIMARY KEY ([SurveyInstanceID], [ProtoQuestionID] )
);
CREATE TABLE [dbo].[DataSourceComments]
(
[SurveyInstanceID] BIGINT
,[ProtoQuestionID] INT
,[Comments] NVARCHAR(MAX)
);
ALTER TABLE [dbo].[DataSourceComments]
ADD CONSTRAINT [FK_DataSourceComments_DataSource_SurveyInstanceID_ProtoQuestionID]
FOREIGN KEY ([SurveyInstanceID], [ProtoQuestionID])
REFERENCES [dbo].[DataSource] ([SurveyInstanceID], [ProtoQuestionID]);
Note, that in both cases I am specifying the FK constraint name. It's is a recommended to have some naming convention which to apply to all new FK.
I believe your foreign key is incorrect... maybe try this?
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY IDENTITY,
CustomerFName NVARCHAR(20),
CustomerLName NVARCHAR(25),
DateOfTravel DATETIME,
TravelLocation NVARCHAR(25),
AgencyID NVARCHAR(25) FOREIGN KEY REFERENCES Agencies(AgencyID)
)
Supposing "AgencyID" is the ID in your Agencies Table.
Try the below code
CREATE TABLE Agency
(
AgencyID INT PRIMARY KEY IDENTITY,
Name NVARCHAR(20)
)
CREATE TABLE Customers
(
CustomerID INT PRIMARY KEY IDENTITY,
CustomerFName NVARCHAR(20),
CustomerLName NVARCHAR(25),
DateOfTravel DATETIME,
TravelLocation NVARCHAR(25),
AgencyID INT FOREIGN KEY REFERENCES Agency(AgencyID)
)
First you have to create the parent table ie, Agency table. Then create the child (Customers) and refer the parent.