Creating a table with a foreign key - sql

Hey all I'm trying to create a table that contains a Foreign Key, and for some reason I am getting an error. The error says 00907. 00000 - "missing right parenthesis" which is odd because I don't have a random left parenthesis. I looked up how to create a table with a Foreign Key and that led to the following code:
Create Table EMPHIREINFO
(
empname VARCHAR2(10) NOT NULL FOREIGN KEY REFERENCES EMPADDRESS(empname),
empno NUMBER(4,0) NOT NULL PRIMARY KEY,
startdt DATE,
enddt DATE,
cntrlgth NUMBER(5,0)
)
I tried it with and without the REFERENCES EMPADDRESS(empname) and I still get the same error. Any help is appreciated, thanks.

You need to specify column after FOREIGN KEY. However, I'd prefer to use naming constraints, for instance
Create Table EMPHIREINFO
(
empname VARCHAR2(10) NOT NULL ,
empno NUMBER(4,0) NOT NULL ,
startdt DATE,
enddt DATE,
cntrlgth NUMBER(5,0),
CONSTRAINT PK_EMPHIREINFO PRIMARY KEY(empno) USING INDEX
(CREATE UNIQUE INDEX IDXU_EMPHIREINFO_empno ON EMPHIREINFO(empno) ),
CONSTRAINT FK_EMPHIREINFO_EMPNAME FOREIGN KEY(empname)
REFERENCES EMPADDRESS(empname)
)

Related

Oracle SQL "invalid identifier error" when trying to create a table

I have successfully built these tables:
CREATE TABLE Tables
(
tnum NUMBER(4) PRIMARY KEY,
floor NUMBER(1), CHECK (floor >= 0 AND floor <= 4),
capacity NUMBER(2), CHECK(capacity >= 0 AND capacity <= 25),
location VARCHAR(50)
);
CREATE TABLE Customer
(
name VARCHAR(25),
phone VARCHAR(16),
city VARCHAR(25),
PRIMARY KEY(name, phone)
);
CREATE TABLE Menu
(
pid NUMBER(4) PRIMARY KEY,
pname VARCHAR(25),
price NUMBER(4, 1)
);
BUT when I try build this one, I got an error:
Error report -
ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
CREATE TABLE Orders
(
orderid NUMBER(4) PRIMARY KEY,
custname VARCHAR(25),
CONSTRAINT fk_customer
FOREIGN KEY (custname)
REFERENCES Customer(customer.name),
phone VARCHAR(16),
CONSTRAINT fk_customer
FOREIGN KEY(phone)
REFERENCES Customer(customer.phone),
date DATE,
tnum NUMBER(4),
CONSTRAINT fk_tables
FOREIGN KEY(tnum)
REFERENCES Tables(tables.tnum),
numofdiners NUMBER(2)
);
I tried to understand what can cause the problem, even though I checked this link and some more other links, I didn't manage to solve the problem.
You can specify a column level constraint directly after the column, but then you have to get rid of the comma and the foreign key part as it's clear which column is meant.
But you need to include all columns of the primary key in the foreign key definition. And because the primary key of the customer table consist of two columns, you can't use an inline foreign key in the table orders.
The foreign key "references" part needs to specify the column name in the target table without a prefix for the table (the table is already clear because of the references table_xxx part, so there is no need to prefix the target column with the table name).
Additionally DATE is a reserved keyword.
CREATE TABLE Orders
(
orderid NUMBER(4) PRIMARY KEY,
custname VARCHAR(25) not null
phone VARCHAR(16) not null,
reservation_date DATE,
tnum NUMBER(4) --<< no comma here!
-- no FOREIGN KEY keyword here
CONSTRAINT fk_tables
REFERENCES Tables(tnum), --<< only the column name between the (...)
numofdiners NUMBER(2), --<< you need a comma before the table level constraints
CONSTRAINT fk_customer
FOREIGN KEY(name, phone) --<< list both columns here
REFERENCES Customer(name, phone) --<< and here
);
Note that the columns in a multi-column foreign key are matched by position, not by name.
Therefore, the following would be wrong
CONSTRAINT fk_customer
FOREIGN KEY(phone, name)
REFERENCES Customer(name, phone)

"no matching unique or primary key for this column-list". The primary key does exist though

So i'm practicing some sql coding for a test and I can't get a foreign key to reference a primary key.
Here's the table that doesn't work:
CREATE TABLE ASSIGNMENT(
ASSIGN_ID NUMBER(2) NOT NULL,
START_DATE DATE,
END_DATE DATE,
BUDGET NUMBER (10,2),
MANAGER_ID NUMBER(2),
PRIMARY KEY (ASSIGN_ID,MANAGER_ID),
FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEE(EMP_ID)
);
Here's the table it is referencing:
CREATE TABLE EMPLOYEE(
EMP_ID NUMBER(2) NOT NULL,
NAME VARCHAR(40),
OFFICE VARCHAR(20),
EXPERT_ID NUMBER(2),
PRIMARY KEY (EMP_ID,EXPERT_ID),
FOREIGN KEY (EXPERT_ID) REFERENCES EXPERTISE(EXPERT_ID)
);
Whenever I try to run the script it always comes back with:
Error report -
SQL Error: ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
I've looked around but can't seem to find the problem. Any help would be appreciated.
Here's the full code (haven't tested the last table yet):
CREATE TABLE EXPERTISE(
EXPERT_ID NUMBER(2) NOT NULL,
DESCRIPTION VARCHAR(50),
HOURLY_RATE NUMBER(3,2),
CHARGE_RATE NUMBER(3,2),
PRIMARY KEY(EXPERT_ID)
);
CREATE TABLE EMPLOYEE(
EMP_ID NUMBER(2) NOT NULL,
NAME VARCHAR(40),
OFFICE VARCHAR(20),
EXPERT_ID NUMBER(2),
PRIMARY KEY (EMP_ID,EXPERT_ID),
FOREIGN KEY (EXPERT_ID) REFERENCES EXPERTISE(EXPERT_ID)
);
CREATE TABLE ASSIGNMENT(
ASSIGN_ID NUMBER(2) NOT NULL,
START_DATE DATE,
END_DATE DATE,
BUDGET NUMBER (10,2),
MANAGER_ID NUMBER(2),
PRIMARY KEY (ASSIGN_ID,MANAGER_ID),
FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEE(EMP_ID)
);
CREATE TABLE ALLOCATION(
EMP_ID NUMBER(3) NOT NULL,
ASSIGN_ID NUMBER(3) NOT NULL,
DAYS_WORKED_ON DATE,
HOURS_WORKED_ON DATE,
PRIMARY KEY(EMP_ID,ASSIGN_ID),
FOREIGN KEY(EMP_ID) REFERENCES EMPLOYEE(EMP_ID),
FOREIGN KEY(ASSIGN_ID) REFERENCES ASSIGNMENT(ASSIGN_ID)
);
I'm using Oracle SQL Developer to make it
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement gives a column-list for which there is no matching unique or primary key
constraint in the referenced table.
The problem is that EMP_ID (by itself) isn't a primary or unique key of table Employees, instead, you have a compound primary key (EMP_ID, EXPERT_ID).
To fix the issue either make EMP_ID the primary key of the Employees table (which seems intuitive as each employee ought to have a unique id) or add a separate unique constraint on EMP_ID.
As pointed out in the comments, if you make EMP_ID the primary key, then (EMP_ID, EXPERT_ID) will also be unique by extension.
As the error suggest, the column you've referenced a foreign key doesn't match a unique constraint/pk on the parent table. Specifically for the primary key EMP_ID, EXPERT_ID you reference only EMP_ID.

Error with oracle ORA-00907: missing right parenthesis

I'm using Oracle10g Express edition and I tried to create this tables but an Error appeared and I need some help fixing the "ORA-00907: missing right parenthesis" problem. I searched for a solution to this error and it looks like the main reason is not the "missing right parenthesis" but I still can't fix the code.
CREATE TABLE Pays
(
codePays NUMBER(4) CONSTRAINT pk_Pays PRIMARY KEY,
nomPays VARCHAR(20)
);
CREATE TABLE Equipe
(
codeEquipe NUMBER(4) CONSTRAINT pk_Equipe PRIMARY KEY,
nomEquipe VARCHAR(4),
);
CREATE TABLE Etape
(
numEtape NUMBER(4) CONSTRAINT pk_Etape PRIMARY KEY,
);
CREATE TABLE Coureur
(
numCoureur NUMBER(4) CONSTRAINT pk_Coureur PRIMARY KEY,
codeEquipe NUMBER(4),
codePays NUMBER(4),
CONSTRAINT FK_Equipe_Coureur FOREIGN KEY(codeEquipe) REFERENCES Equipe(codeEquipe);
CONSTRAINT FK_Pays_Coureur FOREIGN KEY(codePays) REFERENCES Pays(codePays);
);
You have two semicolons in the code for creating the Coureur table. You also have a dangling comma in the code for creating the Equipe table. Replace your code with this:
CREATE TABLE Pays
(
codePays NUMBER(4) CONSTRAINT pk_Pays PRIMARY KEY,
nomPays VARCHAR(20)
);
CREATE TABLE Equipe
(
codeEquipe NUMBER(4) CONSTRAINT pk_Equipe PRIMARY KEY,
nomEquipe VARCHAR(4)
);
CREATE TABLE Etape
(
numEtape NUMBER(4) CONSTRAINT pk_Etape PRIMARY KEY
);
CREATE TABLE Coureur
(
numCoureur NUMBER(4) CONSTRAINT pk_Coureur PRIMARY KEY,
codeEquipe NUMBER(4),
codePays NUMBER(4),
CONSTRAINT FK_Equipe_Coureur FOREIGN KEY(codeEquipe) REFERENCES Equipe(codeEquipe),
CONSTRAINT FK_Pays_Coureur FOREIGN KEY(codePays) REFERENCES Pays(codePays)
);
Here is what I believe is happening leading to the exact error you are seeing. Your Oracle workbench is parsing the Coureur table definition, and it hits a semicolon on the line for the FK_Equipe_Courer constraint. It interprets this as the end of the table definition, but it doesn't see a closing right parenthesis before this semicolon, so it gives you the error you see.

sql - Oracle table create error ORA-00904 invalid identifier

My code is giving an "ORA-00904 invalid identifier" error. What might be the cause?
CREATE TABLE student
(
s_ID int,
S_NAMe varchar2(10),
S_major varchar2(20),
CONSTRAINT PK_s_ID PRIMARY KEY (s_ID),
CONSTRAINT FK_D_ID FOREIGN KEY (D_ID) REFERENCES dep (D_ID)
);
Your last constraint, D_ID references a column that doesn't exist. You need to add it and it's definition to your column list. Also, int is not a valid data type for Oracle.
thank you guys, i appreciate your help , i forget to write the foreign key as a column on student table and i think int is valid on my oracle 11g the table already created , here is code it worked with me
create table student (
s_ID int ,
S_NAMe varchar2 (10),
S_major varchar2(20),
D_ID number (10) ,
Constraint PK_s_ID primary key (s_ID),
constraint FK_D_ID foreign key (D_ID) references dep (D_ID) );

Composite Foreign Key - Possible In Oracle?

I am trying to create a relation/table in Oracle that is between two many to many tables and therefore the Primary key of this table is a composite key but both keys are foreign.
CREATE TABLE employee_licence_certificate(
emp_id NUMBER(4) REFERENCES employee(emp_id)
, licence_cert_code VARCHAR2(6) REFERENCES licence_certificate(licence_cert_code)
, date_earned DATE NOT NULL
)
PRIMARY KEY (emp_id, licence_cert_code))
I have tried using the method for composition keys but I seem to get the following error, which is starting to make me wonder is this even possible?
Error starting at line 1 in command:
CREATE TABLE employee_licence_certificate(emp_id NUMBER(4) REFERENCES employee(emp_id)
, licence_cert_code VARCHAR2(6) REFERENCES licence_certificate(licence_cert_code)
, date_earned DATE NOT NULL) PRIMARY KEY (emp_id, licence_cert_code))
Error at Command Line:3 Column:29
Error report:
SQL Error: ORA-00922: missing or invalid option
00922. 00000 - "missing or invalid option"
*Cause:
*Action:
Try this:
CREATE TABLE employee_licence_certificate(
emp_id NUMBER(4) REFERENCES employee(emp_id)
, licence_cert_code VARCHAR2(6) REFERENCES licence_certificate(licence_cert_code)
, date_earned DATE NOT NULL
,
PRIMARY KEY (emp_id, licence_cert_code))
I use a different syntax. I prefer to explicitly name my foreign key constraints so that the error message if/when its violated is more meaningful/tracable. So I would do it something like this:
CREATE TABLE employee_licence_certificate
( emp_id NUMBER(4) NOT NULL
, licence_cert_code VARCHAR2(6) NOT NULL
, date_earned DATE NOT NULL
, CONSTRAINT elc_pk PRIMARY KEY (emp_id, licence_cert_code)
, CONSTRAINT elc_emp_fk FOREIGN KEY (emp_id)
REFERENCES employee(emp_id)
, CONSTRAINT elc_lct_fk FOREIGN KEY (licence_cert_code )
REFERENCES licence_certificate(licence_cert_code)
)
It sure is possible. You simply need to fix your statement:
CREATE TABLE employee_licence_certificate(emp_id NUMBER(4) REFERENCES employee(emp_id)
, licence_cert_code VARCHAR2(6) REFERENCES licence_certificate(licence_cert_code)
, date_earned DATE NOT NULL, PRIMARY KEY (emp_id, licence_cert_code))
By the way, it is MUCH easier to spot such errors when you format your statements properly:
create table employee_licence_certificate
(
emp_id number(15) references employee(emp_id),
licence_cert_code VARCHAR2(6) REFERENCES licence_certificate(licence_cert_code),
date_earned date not null,
primary key (emp_id, licence_cert_code)
)