Missing Parenthesis in Apex - sql

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.

Related

I'm getting an error on Oracle Apex ORA-00907: missing right parenthesis

I'm trying to run this snipped of SQL in Oracle Apex and keep receiving errors about right parenthesis. I have tried to remove the constraints or alter the tables later, but I keep coming up with the same sort of errors. I cannot seem to figure out what it is that's wrong with the table structure, and I can't find anything online about it that makes sense. Any help would be much appreciated, thanks. Code below...
DROP TABLE Employee_T
CASCADE CONSTRAINTS;
DROP TABLE TaxDepartment_T
CASCADE CONSTRAINTS;
DROP TABLE Location_T
CASCADE CONSTRAINTS;
CREATE TABLE Employee_T
(
EmployeeID NUMBER(11) NOT NULL,
EmployeeName VARCHAR2(25) NOT NULL,
EmployeeAddress VARCHAR2(30) ,
EmployeeCity VARCHAR2(20) ,
EmployeeState CHAR(2) ,
EmployeePostalCode VARCHAR2(10) ,
CONSTRAINT Employee_PK PRIMARY KEY(EmployeeID),
CONSTRAINT Employee_FK1 FOREIGN KEY(DepartmentID) REFERENCES (TaxDepartment_T),
CONSTRAINT Employee_FK2 FOREIGN KEY(BranchID) REFERENCES (Location_T)
);
CREATE TABLE TaxDepartment_T
(
DepartmentID INTEGER(11) NOT NULL,
BranchID INTEGER(11) NOT NULL,
CPAID INTEGER(11) NOT NULL,
EmployeeID INTEGER(11) NOT NULL,
BranchName VARCHAR2(50) NOT NULL,
CONSTRAINT TaxDepartment_PK PRIMARY KEY(DepartmentID, BranchID, CPAID),
CONSTRAINT TaxDepartment_FK1 FOREIGN KEY(BranchID) REFERENCES (Location_T),
CONSTRAINT TaxDepartment_FK2 FOREIGN KEY(EmployeeID) REFERENCES (Employee_T)
);
CREATE TABLE Location_T
(
BranchID INTEGER(11) NOT NULL,
BranchName VARCHAR2(50) NOT NULL,
ManagerName VARCHAR2(50) NOT NULL,
EmployeeID INTEGER(11) NOT NULL,
CONSTRAINT Location_PK PRIMARY KEY(BranchID),
CONSTRAINT Location_FK1 FOREIGN KEY(EmployeeID) REFERENCES (Employee_T)
);
Your foreign key constraint syntax is off.
What it should look like:
REFERENCES SCHEMA.TABLE (COLUMN)
and you just have:
REFERENCES (COLUMN)
If you look at this code in SQL Developer, the parser catches your issue right away, and even gives you a simple click to get to the Docs with syntax diagram for defining FK constraints.
This is your FIRST problem.
The fun with bugs is killing one only exposes the next one. You can't create FK constraints for tables you haven't created yet. So either you need to create the base tables first, OR you need to remove the FK constraints from your CREATE TABLE calls, and add them back later as
alter table TABLE_NAME add constraint CONSTRAINT_NAME foreign key(COLUMN_NAME) references TABLE_NAME2(COLUMN_NAME)
Place all of these ALTER TABLE ADD CONSTRAINT calls at the end of your script, once all the tables have already been created.
Someone else has also noticed that you're using INTEGER.
Which I do, ALL THE TIME...because I'm too lazy to type 'NUMBER(38,0)'
That's fine. But what you can't do is say INTEGER(9). That makes no sense in Oracle.
You should use NUMBER instead of INTEGER in TaxDepartment_T and Location_T tables
There are a few syntax issues.
integer is OK but not integer(11). Use number(11). (Also, while char is a valid type, you should stick to the standard varchar2 for strings to avoid unexpected behaviour.)
Foreign key constraints are written constraint fk references tablename, or optionally you can specify the referenced column in brackets: constraint fk references tablename (columnname). (Also if you write them inline as part of the column definition, you can let the datatype inherit from the parent.)
Employee FK1 and FK2 refer to DepartmentID and BranchID columns that the table doesn't have.
You need to put the parent before the child if you want to run it as a script.
I would write it like this:
drop table employee_t cascade constraints;
drop table taxdepartment_t cascade constraints;
drop table location_t cascade constraints;
create table Location_T
( BranchID number(11) not null constraint Location_PK primary key
, BranchName varchar2(50) not null
, ManagerName varchar2(50) not null );
create table TaxDepartment_T
( DepartmentID number(11) not null
, BranchID constraint TaxDepartment_Location_FK references location_t not null
, CPAID number(11) not null
, BranchName varchar2(50) not null
, constraint TaxDepartment_PK primary key(DepartmentID, BranchID, CPAID) );
create table Employee_T
( EmployeeID number(11) not null constraint Employee_PK primary key
, EmployeeName varchar2(25) not null
, EmployeeAddress varchar2(30)
, EmployeeCity varchar2(20)
, EmployeeState varchar2(2)
, EmployeePostalCode varchar2(10)
, DepartmentID constraint Employee_Department_FK references location_t
, BranchID constraint Employee_Branch_FK references Location_T );
I don't think Location or Tax Department should have EmployeeId columns so I removed them - say if you think that's wrong.
Personally I wouldn't put _T on the end of my table names, and I would avoid camelCase naming because the data dictionary doesn't retain it, and so describing a table gives for example:
SQL> #desc location_t
Name Null? Type
----------------------------------------- -------- ----------------------------
BRANCHID NOT NULL NUMBER(11)
BRANCHNAME NOT NULL VARCHAR2(50)
MANAGERNAME NOT NULL VARCHAR2(50)

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.

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

Here is my main table:
create table employee
(
eno SERIAL primary key,
ename varchar(100) not null,
title varchar(100) not null
);
I want to referenced the title only because I've already referenced the eno in another table.
create table pay
(
title varchar(100),
sal money not null,
foreign key(title) references employee(title)
);
I get an error that there is no unique constraint matching given keys for referenced table "employee" 1
Please help me. I'm having a hard time solving this error. I'm still a beginner at SQL. thanks a lot
There is no limit to the number of tables that may reference a given table.
Foreign keys may only reference primary keys.
Use a foreign key to eno:
create table pay
(
eno varchar(100),
sal money not null,
foreign key(eno) references employee(eno)
);

SQL error- No matching or unique primary key for this column list

Hello I'm new to learning SQL as well as new to the site! I've been trying to mend these tables together with a foreign key on the Job attribute and I keep running into this error "SQL error- No matching or unique primary key for this column-list."
From what I do understand and have been able to figure through trial and error is that the problem is specifically with my foreign key constraint, but I can't seem to figure out how to fix it.
Any help would be appreciated.
Create Table Names
(
FirstName VARCHAR2(25),
LastName VARCHAR2(25),
Address VARCHAR2(25),
Job VARCHAR2(25),
Constraint Name_PK
Primary Key (FirstName, LastName)
);
Create Table Jobs
(
Job VARCHAR2(25),
Salary VARCHAR2(5),
Assistant VARCHAR2(5),
Constraint Jobs_PK
Primary Key (Job),
Constraint Jobs_FK
Foreign Key (Job)
References Names(Job)
);
There are few things you need to improve with your design.
A FOREIGN KEY in one table points to a PRIMARY KEY in another table.
http://www.w3schools.com/sql/sql_foreignkey.asp
Column Job is obviously not a primary key in table Names. This is the reason for error "SQL error- No matching or unique primary key for this column-list."
It looks like instead you intend to reference Jobs from Names table. Meaning that there is foreign key constraint in Names table referencing Jobs with Job column. You can have multiple Names that share same Job.
You will need to slightly change foreign key and move it to your definition of Names table:
Create Table Jobs
(
Job VARCHAR2(25),
Salary VARCHAR2(5),
Assistant VARCHAR2(5),
Constraint Jobs_PK
Primary Key (Job)
);
Create Table Names
(
FirstName VARCHAR2(25),
LastName VARCHAR2(25),
Address VARCHAR2(25),
Job VARCHAR2(25),
Constraint Name_PK
Primary Key (FirstName, LastName),
Constraint Jobs_FK
Foreign Key (Job)
References Jobs(Job)
);
If you really-really need to reference Names from Jobs, all you need to do is to list FirstName and LastName in your existing code like below:
References Names(FirstName, LastName)
As a side note, it is usually not a great idea to have two column primary key, but if you have one like that you will have to reference as two column primary key.
You have the foreign keys in the wrong order. You want:
Create Table Names (
FirstName VARCHAR2(25),
LastName VARCHAR2(25),
Address VARCHAR2(25),
Job VARCHAR2(25),
Constraint pk_Names Primary Key (FirstName, LastName)
Constraint fk_Names_Job Foreign Key (Job) References Jobs(Job)
);
And then Names needs to be defined after Jobs.
You Could Try This One
CREATE TABLE `NAMES` (
`FirstName` varchar(25) DEFAULT NULL,
`LastName` varchar(25) DEFAULT NULL,
`Address` varchar(25) DEFAULT NULL,
`Job` varchar(25) DEFAULT NULL,
KEY `Job` (`Job`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `Jobs` (
`Job` varchar(25) DEFAULT NULL,
`Salary` varchar(5) DEFAULT NULL,
`Assistant` varchar(5) DEFAULT NULL,
KEY `Job` (`Job`),
CONSTRAINT `Jobs_ibfk_1` FOREIGN KEY (`Job`) REFERENCES `NAMES` (`Job`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
you can make any column a foreign key only when the column is a primary key or part of primary key of another table. Reference click

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.