ORA-00905: missing keyword (constraint foreign key) - sql

hi everyone I'm completely new to SQL and I'm trying to answer this question in my book:
7.5 Write a CREATE TABLE statement for the EMPLOYEE table. Email is required and is an alternate key, and the default value of Department is Human Resources. Cascade updates but not deletions from DEPARTMENT to EMPLOYEE.
I'm running the query in Oracle iSQL*Plus, I successfully created the department table, but when i tried creating the employee table while meeting these requirements I get the missing keyword error at line 12
constraint DepartmentFK FOREIGN KEY(DepartmentName),
this is the whole query (I dropped the department table before attempting the whole thing, and it still gives the same error)
CREATE TABLE DEPARTMENT (
DepartmentName char(35) NOT NULL,
BudgetCode char(30) NOT NULL,
OfficeNumber char(15) NOT NULL,
Phone char(12) NOT NULL,
Constraint DepartmentPK PRIMARY KEY(DepartmentName)
);
CREATE TABLE EMPLOYEE (
ProjectID int NOT NULL,
Name char(30) NOT NULL,
Department char(15) NOT NULL,
MaxHours int NOT NULL,
StartDate char(8) NULL,
EndDate char(8) NULL,
Email char(30) DEFAULT 'Human Resources' NOT NULL,
Constraint EmployeePK PRIMARY KEY(ProjectID),
Constraint EmployeeAK1 UNIQUE(Email),
constraint DepartmentFK FOREIGN KEY(DepartmentName),
references DEPARTMENT(DepartmentName)
ON UPDATE CASCADE
ON DELETE no ACTION
);
I tried following the most similar example in the book and looking up foreign key constraint and references but i can't understand why I'm getting this error...
EDIT:
I took out the comma but i still got these two errors:
CREATE TABLE DEPARTMENT (
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
ON UPDATE CASCADE
*
ERROR at line 14:
ORA-00905: missing keyword

You have to remove the comma you have used after
constraint DepartmentFK FOREIGN KEY(DepartmentName),
This is one unit
constraint DepartmentFK FOREIGN KEY(DepartmentName) references DEPARTMENT(DepartmentName)
EDIT:
Since, you have edited your question -
ON UDDATE CASCADE option is not available in Oracle Database, and that's why you're getting an error.
You're getting an error for Department table, since the table already exists, probably from the last run!

Related

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.

Missing Parenthesis in Apex

I'm trying to write a script in Apex to create multiple tables. The first table is created with no issues but every table after that one gives me a missing parenthesis issue. Sometimes it's the left, sometimes it's the right. I've tried everything with no avail.
I have debugged it numerous times, spoken with the others and have not found the solution.
Create Table Employee -- Creates Employee table and references it to ProjDept table
(
EmployeeID Number(4) Not Null,
FirstName VarChar2(15) Not Null,
LastName VarChar2(20) Not Null,
ProjDeptID Number(4) Not Null,
PhoneNumber Number(10),
Constraint Employee_pk Primary Key (EmployeeID), -- sets primary key for table
Constraint Employee_FK Foreign Key References ProjDept(ProjDeptID)-- identifies foreign key
);
This is the second table in the script that won't work, the next 2 tables generate similar errors.
You forgot to include the name of the column which is referencing another in the foreign key:
Create Table Employee (
EmployeeID Number(4) Not Null,
FirstName VarChar2(15) Not Null,
LastName VarChar2(20) Not Null,
ProjDeptID Number(4) Not Null,
PhoneNumber Number(10),
Constraint Employee_pk Primary Key (EmployeeID), -- sets primary key for table
Constraint Employee_FK Foreign Key (ProjDeptId) References ProjDept(ProjDeptID)-- identifies foreign key
);
db<>fiddle
Where exactly in Apex are you executing those commands?
If SQL Workshop's SQL Commands, then you can't have more than a single command in there, i.e. you should create tables one by one:
create the first table
delete that create table command and write another one, for the second table; then create it
the same goes for other tables as well
Alternatively, go to SQL Workshop's SQL Scripts and put all your commands into a script, e.g.
create table a (id number, name varchar2(20), ...);
create table b (cdate date, ...);
save & run the script.

ORA-01748: only simple column names allowed here in Oracle

What I am trying to do ?
I am trying to create two tables and at the same time i am trying to link them together using foreign and primary keys. However I successfully create my parent table ( Student with primary key ) but failed to create child table ( Attendence with foreign key ).
What is the problem ?
I get the following error while creating Attendence table:
ERROR at line 5: ORA-01748: only simple column names allowed here
My code:
Student table:
create table Student (
ST_ROLLNO NUMBER(6) constraint s_pk primary key,
ST_NAME VARCHAR(30) not null,
ST_ADDRESS varchar(35) not null
);
Attendence table:
create table Attendence (
ST_ROLLNO NUMBER(6),
ST_DATE VARCHAR(30) not null,
ST_PRESENT_ABSENT varchar(1) not null,
constraint f_pk Attendence.ST_ROLLNO foreign key references Student(ST_ROLLNO)
);
Your foreign key constraint syntax is wrong; it should be:
constraint f_pk foreign key (ST_ROLLNO) references Student(ST_ROLLNO)
You are preceding the FK column name with the table name, which is wrong in itself, but also have it in the wrong place.
create table Student (
ST_ROLLNO NUMBER(6) constraint s_pk primary key,
ST_NAME VARCHAR(30) not null,
ST_ADDRESS varchar(35) not null
);
Table STUDENT created.
create table Attendence (
ST_ROLLNO NUMBER(6),
ST_DATE VARCHAR(30) not null,
ST_PRESENT_ABSENT varchar(1) not null,
constraint f_pk foreign key (ST_ROLLNO) references Student(ST_ROLLNO)
);
Table ATTENDENCE created.
According to oracle documentation,
ORA ERR
ORA-01748 only simple column names allowed here
The following is the cause of this error:
This SQL statement does not allow a qualified column name, such as
username.table.column or table.column.
Action you can take to resolve this issue: Remove the qualifications
from the column and retry the operation.
In your case, you are trying to refer to the table name while defining a constraint -
Attendence.ST_ROLLNO - WRONG.
It must contain a simple name without the table name or schema name.

ORA missing right parenthesis and unimplemented feature

I'm trying to execute the following script on Oracle APEX:
CREATE TABLE employer (
ename VARCHAR2(30) NOT NULL,
essn CHAR(9),
bdate DATE,
dno INTEGER DEFAULT 1
CHECK (dno > 0 AND dno < 21),
superssn CHAR(9),
CONSTRAINT employer_pk
PRIMARY KEY (essn),
CONSTRAINT employer_fk
FOREIGN KEY (superssn) REFERENCES employer (essn)
ON DELETE SET NULL
ON UPDATE CASCADE
);
CREATE TABLE department (
dname VARCHAR2(10) NOT NULL,
dnumber INTEGER NOT NULL,
mgrssn CHAR(9) NOT NULL,
mgrstartdate CHAR(9) NOT NULL,
PRIMARY KEY (dnumber),
UNIQUE (dname),
FOREIGN KEY (mgrssn) REFERENCES employer (essn)
ON DELETE SET DEFAULT
ON UPDATE CASCADE
);
However, it gives 2 errors. ORA-00907: missing right parenthesis on the first table, and ORA-03001: unimplemented feature on the second.
When I tried to run the script without the ON DELETE/ON UPDATE statements, it didn't show any errors and the tables were created successfully. Do I need a parenthesis somewhere when I'm adding ON DELETE/ON UPDATE?
Oracle doesn't support the following features:
ON UPDATE CASCADE
ON DELETE SET DEFAULT
Presumably, your script will run fine if you remove them. Personally, I've never needed either of them (since I always use surrogate primary keys that are never changed).

Are these FKs necessary -- and are they stopping me?

I'm having some difficulties with a database I'm creating for a summer camp, specifically with the PK and FK constraints. When I declare the FK constraint (e.g. FOREIGN KEY(PID) references Campers(CamperID)) I get an error running my code through pgAdmin (I'm using PostgreSQL). I understand that, for example, the Campers table is not yet created, and this is most likely part/all of the roadblock, however I feel like my FKs are still wrong somehow. To my understanding, a FK is a PK in another table -- but I feel like there is some redundancy or disconnect between my tables.
I've put the syntax for some of my CREATE statements below. I'm not sure if I'll get reprimanded for the quality of my (somewhat vague) question, but I feel a bit lost and would appreciate any help or advice. Thank you in advance!
DROP TABLE IF EXISTS People;
CREATE TABLE People (
PID VARCHAR(10) NOT NULL UNIQUE,
FName TEXT NOT NULL,
LName TEXT NOT NULL,
DOB DATE NOT NULL,
ArrivalDate DATE NOT NULL DEFAULT CURRENT_DATE,
DepartureDate DATE,
US_PhoneNum VARCHAR(11) NOT NULL,
StreetAddress VARCHAR(200) NOT NULL,
Sex GENDER NOT NULL,
ZIP VARCHAR(10) NOT NULL,
PRIMARY KEY(PID),
FOREIGN KEY(PID) REFERENCES Campers(CamperID),
FOREIGN KEY(PID) REFERENCES Counselors(CounselorID),
FOREIGN KEY(ZIP) REFERENCES Zip(ZIP)
);
DROP TABLE IF EXISTS Zip;
CREATE TABLE Zip (
ZIP VARCHAR(10) NOT NULL,
City TEXT NOT NULL,
State VARCHAR(2) NOT NULL,
PRIMARY KEY(ZIP)
);
DROP TABLE IF EXISTS Campers;
CREATE TABLE Campers (
CamperID VARCHAR(10) NOT NULL REFERENCES People(PID),
AgeGroup AGES NOT NULL,
CabinID VARCHAR(2) NOT NULL,
Bed BEDTYPES NOT NULL,
GroupID VARCHAR(3) NOT NULL,
PRIMARY KEY(CamperID),
FOREIGN KEY(CamperID) REFERENCES People(PID),
FOREIGN KEY(CabinID) REFERENCES Cabins(CabinID),
FOREIGN KEY(Bed) REFERENCES Beds(Bed),
FOREIGN KEY(GroupID) REFERENCES Groups(GroupID)
);
DROP TABLE IF EXISTS Counselors;
CREATE TABLE Counselors (
CounselorID VARCHAR(10) NOT NULL REFERENCES People(PID),
GroupID VARCHAR(3) NOT NULL,
CabinID VARCHAR(2) NOT NULL,
PRIMARY KEY(CounselorID),
FOREIGN KEY(GroupID) REFERENCES Groups(GroupID),
FOREIGN KEY(CabinID) REFERENCES Cabins(CabinID)
);
ERROR message for further clarification:
ERROR: relation "campers" does not exist
********** Error **********
ERROR: relation "campers" does not exist
SQL state: 42P01
There are more tables (obviously) which I can provide the create statements for, if needed.
You should really start here: Foreign key.
In the context of relational databases, a foreign key is a field (or
collection of fields) in one table that uniquely identifies a row of
another table.
What you are trying to do in your script is to create a circular link between People, Campers and Counselors. Having a Primary Key field also a Foreign Key mandates that IDs across all referenced tables are identical.
... and to create a Foreign Key the referenced table must already exist in the database. So you should start with the table that does not have any Foreign Keys and create tables that reference only those tables created previously. Alternatively you can create all tables without Foreign Keys and add them later, when all the tables are present.
... and to answer the question, Foreign Keys are never necessary, but they might help.