Error: Could not create constraint SQL Server 2008 - sql

I am creating 6 tables. When creating the first 3 tables, everything works well. But then when I have to use a Foreign Key that I have used previously when creating another table, the system presents an error.
These are the 3 first tables:
CREATE TABLE Employee (
EmployeeID INTEGER PRIMARY KEY NOT NULL,
Name TEXT NOT NULL,
Position TEXT NOT NULL,
Salary REAL NOT NULL,
Remarks TEXT
);
CREATE TABLE Planet (
PlanetID INTEGER PRIMARY KEY NOT NULL,
Name TEXT NOT NULL,
Coordinates REAL NOT NULL
);
CREATE TABLE Has_Clearance (
Employee INTEGER NOT NULL
CONSTRAINT fk_Employee_EmployeeID REFERENCES Employee(EmployeeID),
Planet INTEGER NOT NULL
CONSTRAINT fk_Planet_PlanetID REFERENCES Planet(PlanetID),
Level INTEGER NOT NULL,
PRIMARY KEY(Employee, Planet)
);
Then I create the 4th table:
CREATE TABLE Shipment (
ShipmentID INTEGER PRIMARY KEY NOT NULL,
Date TEXT,
Manager INTEGER NOT NULL
CONSTRAINT fk_Employee_EmployeeID REFERENCES Employee(EmployeeID),
Planet INTEGER NOT NULL
CONSTRAINT fk_Planet_PlanetID REFERENCES Planet(PlanetID)
);
And I get the following error:
"There is already an object named 'fk_Employee_EmployeeID' in the database. Could not create constraint."
Please let me know how to create the FK in this 4th table.

The constraint "fk_Employee_EmployeeID" is first created on table "Has_Clearance" and then you try to create another constraint with the same name - which is not allowed.
Just rename the constraint in the 4-th table like this:
CREATE TABLE Shipment (
ShipmentID INTEGER PRIMARY KEY NOT NULL,
Date TEXT,
Manager INTEGER NOT NULL CONSTRAINT fk_Maneger_EmployeeID REFERENCES Employee(EmployeeID),
Planet INTEGER NOT NULL CONSTRAINT fk_Planet_PlanetID REFERENCES Planet(PlanetID)
);

Related

No primary or candidate keys in the referenced table?

I keep getting an error:
There are no primary or candidate keys in the referenced table 'TProducts' that match the referencing column list in the foreign key 'TProducts_TCategories_FK'."
When trying to establish a relationship between my products table and categories table. I have checked the spelling multiple times but can't seem to figure out why I keep getting this error. The rest of my tables are fine, and I used the same method.
CREATE TABLE TCategories
(
intCategoryID INTEGER NOT NULL,
strCategoryName VARCHAR(255) NOT NULL,
CONSTRAINT TCategories_PK PRIMARY KEY (intCategoryID)
)
CREATE TABLE TProducts
(
intProductID INTEGER NOT NULL,
intVendorID INTEGER NOT NULL,
intCategoryID INTEGER NOT NULL,
strProductName VARCHAR(255) NOT NULL,
monCostofProduct MONEY NOT NULL,
monRetailCost MONEY NOT NULL,
intInventory INTEGER NOT NULL,
CONSTRAINT TProducts_PK PRIMARY KEY (intProductID)
)
ALTER TABLE TProducts
ADD CONSTRAINT TProducts_TCategories_FK
FOREIGN KEY (intCategoryID) REFERENCES TProducts (intCategoryID)
You are referencing to TProducts table and not TCategories table:
ALTER TABLE TProducts ADD CONSTRAINT TProducts_TCategories_FK
FOREIGN KEY ( intCategoryID ) REFERENCES TCategories( intCategoryID )

Oracle (ORA-02270) : no matching unique or primary key for that column list

I have two tables:
CREATE TABLE Trasy_srodki_transportu(
ID_Trasy Integer NOT NULL,
ID_pojazdu Integer NOT NULL
)
/
CREATE TABLE Trasy(
ID_Trasy Integer NOT NULL,
Linia Varchar2(4 ) NOT NULL,
Data_rozpoczecia_kursowania Date NOT NULL,
Data_zakonczenia_kursowania Date,
ID_Pracownika Integer NOT NULL
)
Now i want to add foreign key to Trasy_srodki_transportu referencing to Trasy table:
ALTER TABLE Trasy_srodki_transportu ADD CONSTRAINT Trasa_jest_wykorzystywana FOREIGN KEY (ID_Trasy) REFERENCES Trasy (ID_Trasy)
/
and this throws Oracle (ORA-02270) : no matching unique or primary key for this column-list error. Any suggestions how to fix this?Data modeler view
A foreign key needs to reference a key on the related table, but it's not the case in your example. Change the definition of the second table by adding a PRIMARY KEY constraint in it, as in:
CREATE TABLE Trasy (
ID_Trasy Integer PRIMARY KEY NOT NULL,
Linia Varchar2(4 ) NOT NULL,
Data_rozpoczecia_kursowania Date NOT NULL,
Data_zakonczenia_kursowania Date,
ID_Pracownika Integer NOT NULL
)
Alternatively, you can create a unique constraint on it, that can also serve as a key. For example:
CREATE TABLE Trasy (
ID_Trasy Integer NOT NULL,
Linia Varchar2(4 ) NOT NULL,
Data_rozpoczecia_kursowania Date NOT NULL,
Data_zakonczenia_kursowania Date,
ID_Pracownika Integer NOT NULL,
CONSTRAINT uq_idtrasy UNIQUE (ID_Trasy)
)

Is it possible to find the row using something other than the primaryKey on SQLite?

I want to be able to find the value using either network_id or username.
Yet the following sintax gives the error of more than one primary key (as expected).
CREATE TABLE Player(
network_id TEXT not null,
username varchar2(50) not null,
value INTEGER,
CONSTRAINT player_pk1 PRIMARY KEY (username),
CONSTRAINT player_pk2 PRIMARY KEY (network_id)
);
Is there a way that I could do this in Sqlite?
A primary key has three components to its definition:
NOT NULL
UNIQUE
Only one per table
That is why you cannot have more than one. But you can have any number of NOT NULL UNIQUE columns:
CREATE TABLE Player(
network_id TEXT not null,
username varchar2(50) not null,
value INTEGER,
CONSTRAINT player_pk1 PRIMARY KEY (username),
CONSTRAINT unq_player_network_id UNIQUE (network_id)
);

Column exist but program don t see? [duplicate]

This question already has answers here:
SQLite Foreign Key
(5 answers)
Closed 1 year ago.
I created a 3 tables but in the last one shows errors
CREATE TABLE Student
(
St_Id char(7) PRIMARY KEY,
St_Fname varchar(15) NOT NULL,
St_Lname varchar(20) NOT NULL,
St_DOB date
)
CREATE TABLE Course
(
Course_code char(5) PRIMARY KEY,
Course_title varchar(30) NOT NULL,
Course_credit INTEGER NOT NULL
)
CREATE TABLE Registration
(
Reg_no INTEGER PRIMARY KEY AUTOINCREMENT ,
FOREIGN KEY (St_Id) REFERENCES Student(St_Id),
FOREIGN KEY (Course_code) REFERENCES Course(Course_code),
Mark_obtaines INTEGER
)
and error is
Execution finished with errors. Result: unknown column "St_Id" in
foreign key definition At line 1: CREATE TABLE Registration ( Reg_no
INTEGER PRIMARY KEY AUTOINCREMENT , FOREIGN KEY (St_Id) REFERENCES
Student(St_Id),
In order to define a foreign key, you need to define the column first -- and the types need to match the type in the referenced table:
CREATE TABLE Registration (
Reg_no INTEGER PRIMARY KEY AUTOINCREMENT,
St_Id CHAR(7),
Course_Code CHAR(5),
FOREIGN KEY (St_Id) REFERENCES Student(St_Id),
FOREIGN KEY (Course_code) REFERENCES Course(Course_code),
Mark_obtaines INTEGER
);
Then the FOREIGN KEY declaration provides more information about the column.

ERROR: violates foreign key constraint, key is not present in parent table (but it is??)

I know this question has been asked many times, but none of the answers have solved my issue.
I am creating a database for a uni assignment, using PostgreSQL through pgadmin 4, and I have a table named "staff" populated with staff members with a primary key of "staffid". I then have another table named "client_international", which includes a foreign key of "staffid" which relates to the staff tables primary key.
When trying to insert into the client table, I am getting the following error:
ERROR: insert or update on table "client_international" violates foreign key constraint "intclient_staff_fkey"
DETAIL: Key (staffid)=(100000024) is not present in table "staff".
SQL state: 23503
I am certain that that '100000024' key is in the staff table.. yet I still get the error. Any suggestions? Below I will paste the code I used to create the staff and client tables, in case anyone notices an error in them.
Staff table:
CREATE SEQUENCE staff_seq
start 100000000
increment 1;
CREATE TABLE staff
(
staffid integer default nextval('staff_seq'),
firstname varchar(20) NOT NULL,
lastname varchar(20) NOT NULL,
"position" varchar(20) NOT NULL,
mobile varchar(20) NOT NULL,
email varchar(100) NOT NULL,
"location" integer NOT NULL,
CONSTRAINT staff_pkey PRIMARY KEY (staffid)
);
Client table:
CREATE SEQUENCE client_seq
start 200000000
increment 1;
CREATE TABLE client
(
clientid integer default nextval('client_seq'),
company varchar(100) NOT NULL,
sector varchar(100) NOT NULL,
pointofcontact varchar(20) NOT NULL,
mobile varchar(20) NOT NULL,
email varchar(100) NOT NULL,
approvalstatus boolean default (false),
"location" integer NOT NULL,
staffid integer NOT NULL,
CONSTRAINT client_pkey PRIMARY KEY (clientid)
);
CREATE TABLE client_international
(
CONSTRAINT client_international_pkey PRIMARY KEY (clientid)
) INHERITS ("client");
ALTER TABLE client
ADD CONSTRAINT client_location_fkey FOREIGN KEY ("location") REFERENCES "location" (locationid),
ADD CONSTRAINT client_staff_fkey FOREIGN KEY (staffid) REFERENCES staff (staffid);
ALTER TABLE client_international
ADD CONSTRAINT intclient_location_fkey FOREIGN KEY ("location") REFERENCES "location" (locationid),
ADD CONSTRAINT intclient_staff_fkey FOREIGN KEY (staffid) REFERENCES staff (staffid);
I get the error when running the following statements:
INSERT INTO client_international(company, sector, pointofcontact, mobile, email, approvalstatus, "location", staffid)
VALUES ('Moores Dogs', 'Border Patrol', 'Carol Moore', '07911 653453', 'jenkinsj#k9solutions.co.uk', 'false', '500000001', '100000024');
Here's a screenshot of the entry in the staff table, showing that it's definitely in there:
Foreign keys aren't "inherited".
Quote from the manual
A serious limitation of the inheritance feature is that [...] foreign key constraints only apply to single tables, not to their inheritance children. This is true on both the referencing and referenced sides of a foreign key constraint.
(emphasis mine)
So what you are trying to do, simply isn't supported.