I keep getting this error in my psql database;
bikefacility=# ERROR: syntax error at or near "c"
bikefacility-# LINE 1: c
bikefacility-# ^
bikefacility-# bikefacility=# ALTER TABLE bicycle ADD CONSTRAINT fk_maintenance_contact_person FOREIGN KEY (maintenance_contact_person) REFERENCES maintenance(maintenance_contact_person);
ERROR: syntax error at or near "ERROR"
LINE 1: ERROR: syntax error at or near "c"
^
bikefacility=# ERROR: column "maintenance_contact_person" referenced in foreign key constraint does not exist
bikefacility-# bikefacility=# ALTER TABLE bicycle ADD CONSTRAINT fk_rental_period FOREIGN KEY (rental_period) REFERENCES rental(rental_period);
ERROR: syntax error at or near "ERROR"
LINE 1: ERROR: column "maintenance_contact_person" referenced in fo...
^
bikefacility=# ERROR: column "rental_period" referenced in foreign key constraint does not exist
bikefacility-# bikefacility=# ALTER TABLE bicycle ADD CONSTRAINT fk_terminal_id FOREIGN KEY (terminal_id) REFERENCES terminal(terminal_id);
ERROR: syntax error at or near "ERROR"
LINE 1: ERROR: column "rental_period" referenced in foreign key con...
^
bikefacility=# ERROR: column "terminal_id" referenced in foreign key constraint does not exist
This is my code. I've created the same style of foreign keys within the code as you can see here.
CREATE TABLE member (
member_id INTEGER PRIMARY KEY,
member_fname VARCHAR(15) NOT NULL,
member_lname VARCHAR(15) NOT NULL,
member_status VARCHAR(15) NOT NULL,
member_address VARCHAR(10) NOT NULL,
member_email VARCHAR(30) NOT NULL
);
CREATE TABLE bicycle (
bicycle_id INTEGER PRIMARY KEY,
bicycle_brand VARCHAR(25) NOT NULL,
bicycle_model VARCHAR(25) NOT NULL,
bicycle_colour VARCHAR(15) NOT NULL,
bicycle_type VARCHAR(20) NOT NULL,
bicycle_size VARCHAR(10) NOT NULL,
bicycle_availability VARCHAR(20) NOT NULL
);
ALTER TABLE bicycle ADD CONSTRAINT fk_bicycle_pickup_date FOREIGN KEY (bicycle_pickup_date) REFERENCES rental(bicycle_pickup_date) >MATCH FULL;
ALTER TABLE bicycle ADD CONSTRAINT fk_maintenance_contact_person FOREIGN KEY (maintenance_contact_person) REFERENCES maintenance(maintenance_contact_person);
ALTER TABLE bicycle ADD CONSTRAINT fk_terminal_id FOREIGN KEY (terminal_id) REFERENCES terminal(terminal_id);
ALTER TABLE bicycle ADD CONSTRAINT fk_rental_period FOREIGN KEY (rental_period) REFERENCES rental(rental_period);
CREATE TABLE sponsor (
sponsor_id INTEGER PRIMARY KEY,
sponsor_name VARCHAR(15) NOT NULL,
sponsor_contact VARCHAR(30) NOT NULL,
sponsor_period DATE NOT NULL,
sponsor_address VARCHAR(50) NOT NULL,
sponsor_fee DECIMAL (6, 2) NOT NULL
);
CREATE TABLE terminal (
terminal_id INTEGER PRIMARY KEY,
terminal_address VARCHAR(50) NOT NULL,
terminal_minstorage VARCHAR(50) NOT NULL,
terminal_maxstorage VARCHAR(50) NOT NULL
);
CREATE TABLE rental (
rental_no INTEGER PRIMARY KEY,
rental_period DATE NOT NULL,
bicycle_pickup_date DATE NOT NULL
);
It says that the columns don't exist but I know they do because they're right there! Can someone help me out, please? Thanks in advance!
The grammar of the the foreign keys are incorrect for postgresql. For instance, ALTER TABLE bicycle ADD CONSTRAINT fk_terminal_id FOREIGN KEY (terminal_id) REFERENCES terminal(terminal_id); requires there to be a field in bicycles, named terminal_id as the former terminal_id in your query refers to bicycle table, which should reference terminal_id in the terminal table.
Here is a short tutorial of foreign keys. https://www.postgresqltutorial.com/postgresql-foreign-key/
Best regards,
Bjarni
Your schema is quite messed up.
There's no table maintenance yet you try to make a reference to it.
The foreign table like rental must be created before you make a fk reference to it.
You must have the named fields in the table where you make the reference from. There are no fields bicycle_pickup_date, maintenance_contact_person, terminal_id or rental_period in table bicycle.
Your fk references in general sound odd. Do you really want that each bicycle could have only only one rental, ever? Maybe you meant that a rental references to a single bicycle?
Also, your naming convention is quite redundant. There's no need to repeat table name in each field name, that just adds unneeded clutter in your code.
Related
I'm getting this error even thought I think my table correctly references a primary key. The Constituent_Gift table references 5 foreign keys but only triggers this error for the Gift table. I can't see why the Category table would work but the Gift table wouldn't. The exact error message is:
Msg 547, Level 16, State 0, Line 12
The INSERT statement conflicted with the FOREIGN KEY constraint "fk_giftid". The conflict occurred in database "SusanCurtis", table "dbo.Gift", column 'Gift_ID'.
Code:
CREATE TABLE Category
(
Category_ID NCHAR(4) CONSTRAINT pk_categoryid PRIMARY KEY,
Category_Description NVARCHAR(50) CONSTRAINT nn_categorydescription NOT NULL
);
CREATE TABLE Gift
(
Gift_ID NCHAR(1) CONSTRAINT pk_giftid PRIMARY KEY,
Gift_Description NVARCHAR(20) CONSTRAINT nn_giftdescription NOT NULL,
);
CREATE TABLE Constituent_Gift
(
Constituent_Gift_ID NCHAR(6) CONSTRAINT pk_constituentgift PRIMARY KEY,
Constituent_ID NCHAR(6) CONSTRAINT fk_constituentid FOREIGN KEY
REFERENCES Constituent(Constituent_ID),
Gift_ID NCHAR(1) CONSTRAINT fk_giftid FOREIGN KEY
REFERENCES Gift(Gift_ID),
Fund_ID NCHAR(4) CONSTRAINT fk_fundid FOREIGN KEY
REFERENCES Fund(Fund_ID),
Event_ID NCHAR(2) CONSTRAINT fk_eventid FOREIGN KEY
REFERENCES [Event](Event_ID),
Category_ID NCHAR(4) CONSTRAINT fk_categoryid FOREIGN KEY
REFERENCES Category(Category_ID),
Note NVARCHAR(2000),
Gift_Amount MONEY CONSTRAINT nn_giftamount NOT NULL,
Deductible_Amount MONEY,
Gift_Date Date,
Payment_Type NVARCHAR(30),
Anonymous_Gift BIT
);
Does anyone know why this is happening?
Well, the error message really says it all: obviously, that INSERT (which unfortunately you haven't shown) is trying to insert a value that violates the foreign key constraint fk_giftid - that's the foreign key between Constituent_Gift.Gift_ID and Gift.Gift_ID - which means you're trying to insert a row into Constituent_Gift with a value for Gift_ID that doesn't exist in the referenced Gift table.
And that's the whole point of a foreign key constraint - ensure you only ever insert valid data - that is present in the referenced table (here Gift).
Fix that value to be something that is present in the Gift table - and you should be fine.
what data are you going to insert?
I also miss the schema for the table 'Constituent'
Constituent_ID NCHAR(6) CONSTRAINT fk_constituentid FOREIGN KEY
REFERENCES Constituent(Constituent_ID),
I'm not sure what I'm doing wrong, but I'm getting a foreign key references invalid table message telling me that dbo.ValueStream is an invalid table.
Edit: I originally posted my code in the incorrect order. Fixed in the edit to reflect the order it was done in SSMS
CREATE TABLE dbo.ValueStream
(
ValueStreamKey int IDENTITY(1,1) NOT NULL
CONSTRAINT PK_ValueStream_ValueStreamKey PRIMARY KEY,
ValueStream nvarchar(20),
)
CREATE TABLE dbo.NonConformance
(
NonConformanceKey int IDENTITY(1,1) NOT NULL
CONSTRAINT PK_NonConformance_NonConformanceKey PRIMARY KEY,
CustomerVendorKey int NOT NULL
CONSTRAINT FK_NonConformance_CustomerVendorKey_CustomerVendor_CustomerVendorKey
FOREIGN KEY REFERENCES dbo.CustomerVendor(CustomerVendorKey),
ValueStreamKey int NOT NULL
CONSTRAINT FK_NonConformance_ValueStream_Key_ValueStream_ValueStreamKey
FOREIGN KEY REFERENCES dbo.ValueStream(ValueStreamKey),
RepairOrder nvarchar(50) NOT NULL,
WorkOrder nvarchar(8) NOT NULL,
PartNumber nvarchar(50) NOT NULL,
SerialNumber nvarchar(50) NOT NULL,
PartDescription nvarchar(50) NOT NULL,
InductionDate datetime NOT NULL,
TriageStartDate datetime NOT NULL,
TriageCompletionDate datetime NOT NULL,
RequiresRRCA Bit NOT NULL,
NonConformanceSummary nvarchar(max) NOT NULL
);
taking out the CONSTRAINT FK_NonConformance_CustomerVendorKey_CustomerVendor_CustomerVendorKey
FOREIGN KEY REFERENCES dbo.CustomerVendor(CustomerVendorKey)
(we don't have that one)
and running the scripts seperately - first the first table and then the creation of the referencing table - this works for me.
No code errors.
You are creating dbo.NonConformance first in the script that you have posted. You need to move the CREATE TABLE dbo.ValueStream statement so that it is created first, ensuring there is a GO statement still between the two CREATE TABLE statements.
As it stands you are trying to create a foreign key to a table that does not exist.
Currently, I have the following code in my SQLQuery:
CREATE TABLE OrderTable (
OrderNumber varchar(3) NOT NULL,
Date_Time datetime NOT NULL,
WaitID varchar(7) NOT NULL,
CashID varchar(7),
TableNumber varchar(3) NOT NULL,
CONSTRAINT PK_Order PRIMARY KEY (OrderNumber, Date_Time),
CONSTRAINT FK_WaitID FOREIGN KEY (WaitID) REFERENCES WaiterWaitress(ID),
CONSTRAINT FK_CashID FOREIGN KEY (CashID) REFERENCES Cashier(ID),
CONSTRAINT FK_TableNumber FOREIGN KEY (TableNumber) REFERENCES RestTable(TableNumber)
);
I had to retype the CONSTRAINT ... FOREIGN KEY ... REFERENCES ... three times. Is there a shortcut to this while still maintaining the ability to name the foreign key (i.e. FK_WaitID)?
You could do some sort of complicated script after you run the CREATE TABLE statement to add the constraints without repeating the actual keyword "CONSTRAINT", but that code will not "look a bit better".
Writing it this way keeps it clear for others in the future who may need to review the structure. The constraints and keys are clearly laid out, and that's really the way it should be.
I have a question about this relation betweed SpelerTeam and Toernooi, i get a error, can someone help me with it all the other relations do work:
Msg 1776, Level 16, State 0, Line 2 There are no primary or candidate
keys in the referenced table 'SpelerTeam' that match the referencing
column list in the foreign key 'FK__Toernooi__team__1A14E395'. Msg
1750, Level 16, State 0, Line 2 Could not create constraint. See
previous errors.
CREATE TABLE dbo.LoginGegevens
(
Username varchar(25) NOT NULL,
Wachtwoord varchar(25) NOT NULL,
LoginDatum date NOT NULL,
)
CREATE TABLE dbo.NawGegevensKind
(
KindSpeler varchar(25) NOT NULL,
NawKind varchar(50) NOT NULL,
)
CREATE TABLE dbo.NawGegevensProfspeler
(
Profspeler varchar(25) NOT NULL,
NawProfspeler varchar(50) NOT NULL,
)
CREATE TABLE dbo.SpelerTeam
(
Team int NOT NULL,
Kindspeler varchar(25) NOT NULL,
Profspeler varchar(25) NOT NULL,
)
CREATE TABLE dbo.Toernooi
(
team int NOT NULL,
score int NOT NULL,
games int NOT NULL,
rondes int NOT NULL,
)
-- primary keys
ALTER TABLE LoginGegevens
ADD primary KEY (Username)
ALTER TABLE NawGegevensKind
ADD primary KEY (KindSpeler)
ALTER TABLE NawGegevensProfspeler
ADD primary KEY (ProfSpeler)
ALTER TABLE SpelerTeam
ADD primary KEY (Team,KindSpeler,ProfSpeler)
ALTER TABLE Toernooi
ADD primary KEY (Team,Rondes,Games)
-- relation
ALTER TABLE SpelerTeam
ADD FOREIGN KEY (Kindspeler)
REFERENCES NawGegevensKind(Kindspeler);
ALTER TABLE SpelerTeam
ADD FOREIGN KEY (Profspeler)
REFERENCES NawGegevensProfspeler(Profspeler);
ALTER TABLE Toernooi
ADD FOREIGN KEY (Team)
REFERENCES SpelerTeam(Team);
The Primary Key of SpelerTeam is Team,KindSpeler,ProfSpeler.
That means that any foreign key that references that table has to reference all three of those columns. You can't create a foreign key that uses only team.
You need an index on a colum so that it can be referenced as a foreign key efficiently. Add an index on SpelerTeam.Team and on all other columns with the same problem. Try CREATE INDEX IX_SpelerTeam_Team_Team ON SpelerTeam (Team);
Add below after all tables have been created:
CREATE UNIQUE NONCLUSTERED INDEX IX_SpelerTeam_Team
ON dbo.SpelerTeam (Team);
GO
As it was pointed in another answer you cannot reference to non unique column, to make column unique you can make this column primary key or create index for this column.
It is not enough that your "Team" column is part of composite key.
Does anyone know why my two tables in SQL developer produce the error of:
Error report - SQL Error: ORA-00904: "CLIENT_ID": invalid identifier 00904. 00000 - "%s: invalid identifier".
I know it is to do with the constraints for the foreign keys but don't know why:
CREATE TABLE person
( person_id NUMBER(4) not null,
person_name VARCHAR(50),
person_surname VARCHAR(50),
person_contact_number NUMBER(11),
person_address VARCHAR2(50),
person_postcode VARCHAR2(50),
fk_boss_id NUMBER(4),
CONSTRAINT person_id_pk PRIMARY KEY (person_id),
CONSTRAINT boss_id FOREIGN KEY (boss_id) REFERENCES boss (boss_id)
);
CREATE TABLE boss
( boss_id NUMBER(4) not null,
boss_name VARCHAR(50),
fk_person_id NUMBER(4),
CONSTRAINT boss_pk PRIMARY KEY (boss_id),
CONSTRAINT person_id FOREIGN KEY (person_id) REFERENCES person (person_id)
);
Your foreign key constraints say that the columns person.boss_id and boss.person_id are foreign keys, but neither table has a column by that name. It looks as though you've swapped the names of the FK columns with those of the FK constraints.
The FOREIGN KEY clause of the CONSTRAINT declaration must reference a column that exists in the declaring table. For instance, in the table boss you would have
...
person_id NUMBER(4),
...
CONSTRAINT fk_person_id FOREIGN KEY (person_id) REFERENCES person (person_id)
...
(Also, if your error message begins ORA then you're using Oracle, not MySQL, and if it refers to a column called CLIENT_ID then it's complaining about a third table which is neither of the ones you posted.)