ORA-00911: invalid character - oracle

I am trying to write my first table create script with column definitions. I have tried several things to get this error to go away, but to no avail. Please take a look to see if their is anything obvious in my code below:
CREATE TABLE CD_TYPE (
CD_TYPE VARCHAR2(4) PRIMARY KEY,
CD_FORMAT VARCHAR2(10)
);
CREATE TABLE MANUFACTURER (
MANUFACTURER_NUM NUMBER(3) PRIMARY KEY,
MANUFACTURER_NAME VARCHAR2(30)
);
CREATE TABLE CD_TITLE (
CD_NUM NUMBER(4) PRIMARY KEY,
TITLE VARCHAR2(30),
MANUFACTURER_NUM VARCHAR2(30) FOREIGN KEY,
CD_TYPE VARCHAR2(4) FOREIGN KEY,
ACQUIRED_DATE DATE,
ORIGINAL CHAR(1)
);
CREATE TABLE CD_SN (
CD_NUM NUMBER(4) PRIMARY KEY FOREIGN KEY,
SERIAL_NUM VARCHAR2(30) PRIMARY KEY,
NUM_LIC_REMAIN NUMBER(2)
);

The problem is with the columns reading
MANUFACTURER_NUM VARCHAR2(30) FOREIGN KEY,
You forgot to specify what table these columns refer to.
You to that with a REFERENCES clause:
create table cd_title (
cd_num number(4) primary key,
title varchar2(30),
manufacturer_num REFERENCES MANUFACTURER,
cd_type REFERENCES CD_TYPE,
acquired_date date,
original char(1)
);
Alternatively, you can specify the name for the foreign key with a CONSTRAINT ... FOREIGN KEY (...) REFERENCES ... clause:
create table cd_title (
cd_num number(4) primary key,
title varchar2(30),
manufacturer_num,
cd_type,
acquired_date date,
original char(1),
--
CONSTRAINT cd_title_fk1 FOREIGN KEY (manufacturer_num) REFERENCES manufacturer,
CONSTRAINT cd_title_fk2 FOREIGN KEY (cd_type ) REFERENCES cd_type
)

Related

"ORA-00904 invalid identifier" when adding foreign key constraint

I'm trying to create a "diagnosis" table(last table), but something is wrong with the 2 lines of code that creates Foreign Key. I'm sure that's where the problem lies because when I comment them out, I can create the table. The first 4 tables can be created with no errors, so no problem with them. Thank you for reading my question.
I have tried to spot some syntax errors but to no avail.
CREATE TABLE nurse(
nurse_ID number(5) not null,
nurse_name varchar2(20),
nurse_number number(10),
nurse_address varchar2(50),
CONSTRAINTS nurse_pk PRIMARY KEY (nurse_ID)
);
CREATE TABLE medicine(
med_ID number(10) not null,
med_name varchar2(30),
med_type varchar2(20),
exp_date date,
dose_lim float,
med_components varchar2(50),
CONSTRAINTS med_pk PRIMARY KEY (med_ID)
);
CREATE TABLE in_patient (
in_pat_ID number(5) not null,
in_pat_name varchar2(20),
in_pat_add varchar2(50),
in_pat_dob date,
in_pat_history varchar2(100),
in_mode_payment varchar2(20),
in_start_date date,
in_end_date date,
CONSTRAINTS in_pat_pk PRIMARY KEY (in_pat_ID)
);
CREATE TABLE out_patient (
out_pat_ID number(5) not null,
out_pat_name varchar2(20),
out_pat_add varchar2(50),
out_pat_dob date,
out_pat_history varchar2(100),
out_mode_payment varchar(20),
out_date_of_visit date,
CONSTRAINTS out_pat_pk PRIMARY KEY (out_pat_ID)
);
CREATE TABLE diagnosis(
dia_ref number(12) not null,
dia_type varchar2(20),
dia_date date,
dia_result varchar2(8), --Either Negative or positive.
CONSTRAINTS dia_pk PRIMARY KEY (dia_ref),
--one of the following is going to be null when you insert rows into this table since one diagnosis can only be associated with a single patient
CONSTRAINTS dia_out_fk FOREIGN KEY (out_pat_ID) REFERENCES out_patient (out_pat_ID),
CONSTRAINTS dia_in_fk FOREIGN KEY (in_pat_ID) REFERENCES in_patient (in_pat_ID)
);
I expected that the table can be created with no error.
Your diagnosis table refers to out_pat_ID and in_pat_ID as columns which should serve as foreign keys, but these columns don't actually exist in the table definition. Try adding them as one possible fix here:
CREATE TABLE diagnosis (
dia_ref number(12) NOT NULL,
dia_type varchar2(20),
dia_date date,
dia_result varchar2(8),
out_pat_ID number(5),
in_pat_ID number(5)
CONSTRAINT dia_pk PRIMARY KEY (dia_ref),
CONSTRAINT dia_out_fk FOREIGN KEY (out_pat_ID) REFERENCES out_patient (out_pat_ID),
CONSTRAINT dia_in_fk FOREIGN KEY (in_pat_ID) REFERENCES in_patient (in_pat_ID)
);
diagnosis table need add out_pat_ID and in_pat_ID column
CREATE TABLE nurse(
nurse_ID number(5) not null,
nurse_name varchar2(20),
nurse_number number(10),
nurse_address varchar2(50),
CONSTRAINTS nurse_pk PRIMARY KEY (nurse_ID)
);
✓
CREATE TABLE medicine(
med_ID number(10) not null,
med_name varchar2(30),
med_type varchar2(20),
exp_date date,
dose_lim float,
med_components varchar2(50),
CONSTRAINTS med_pk PRIMARY KEY (med_ID)
);
✓
CREATE TABLE in_patient (
in_pat_ID number(5) not null,
in_pat_name varchar2(20),
in_pat_add varchar2(50),
in_pat_dob date,
in_pat_history varchar2(100),
in_mode_payment varchar2(20),
in_start_date date,
in_end_date date,
CONSTRAINTS in_pat_pk PRIMARY KEY (in_pat_ID)
);
✓
CREATE TABLE out_patient (
out_pat_ID number(5) not null,
out_pat_name varchar2(20),
out_pat_add varchar2(50),
out_pat_dob date,
out_pat_history varchar2(100),
out_mode_payment varchar(20),
out_date_of_visit date,
CONSTRAINTS out_pat_pk PRIMARY KEY (out_pat_ID)
);
✓
CREATE TABLE diagnosis(
dia_ref number(12) not null,
dia_type varchar2(20),
dia_date date,
dia_result varchar2(8), --Either Negative or positive.
out_pat_ID number(5) not null,
in_pat_ID number(5) not null,
CONSTRAINTS dia_pk PRIMARY KEY (dia_ref),
--one of the following is going to be null when you insert rows into this table since one diagnosis can only be associated with a single patient
CONSTRAINTS dia_out_fk FOREIGN KEY (out_pat_ID) REFERENCES out_patient (out_pat_ID),
CONSTRAINTS dia_in_fk FOREIGN KEY (in_pat_ID) REFERENCES in_patient (in_pat_ID)
);
✓
db<>fiddle here
If you are allowing only one patient per diagnosis, then you should have a check constraint as well. Your comment and data structure are out-of-sync:
CREATE TABLE diagnosis (
dia_ref number(12) not null,
dia_type varchar2(20),
dia_date date,
dia_result varchar2(8), --Either Negative or positive.
out_pat_ID number(5),
in_pat_ID number(5),
constraint chk_diagnosis_pat_id
check ( (out_pat_id is not null and in_pat_id is null) or
(out_pat_id is null and in_pat_id is not null)
),
constraint dia_pk primary key (dia_ref),
--one of the following is going to be null when you insert rows into this table since one diagnosis can only be associated with a single patient
constraint dia_out_fk foreign key (out_pat_ID) references out_patient (out_pat_ID),
constraints dia_in_fk foreign key (in_pat_ID) references in_patient (in_pat_ID)
);
That said, separate tables for in-patient and out-patient do not look correct. Or, at the very least, you need a persons table with information about a person over time. Then you can have separate "appointments" and "registrations" to determine if they make an out-patient appointment or register for in-patient care.
Personally I prefer to define single-column constraints inline as part of the column definition. Then you don't have to repeat the column name, and for FK constraints you can let the column inherit its parent's datatype so it is less error-prone.
create table in_patient
( in_pat_id number(5) constraint in_pat_pk primary key
, in_pat_name varchar2(20)
, in_pat_add varchar2(50)
, in_pat_dob date
, in_pat_history varchar2(100)
, in_mode_payment varchar2(20)
, in_start_date date
, in_end_date date
);
create table out_patient
( out_pat_id number(5) constraint out_pat_pk primary key
, out_pat_name varchar2(20)
, out_pat_add varchar2(50)
, out_pat_dob date
, out_pat_history varchar2(100)
, out_mode_payment varchar(20)
, out_date_of_visit date
);
create table diagnosis
( dia_ref number(12) constraint dia_pk primary key
, dia_type varchar2(20)
, dia_date date
, dia_result varchar2(8)
, dia_out_fk references out_patient (out_pat_id)
, dia_in_fk references in_patient (in_pat_id)
);
If dia_result can only be 'POSITIVE' or 'NEGATIVE' then I'd suggest adding a check constraint to enforce this.
Probably some more columns could be defined as not null. It's a good idea to define every possible constraint you can think of.

Create table field with foreign key constraint

I want to create a table department:
COLUMN NAME DATATYPE SIZE CONSTRAINT
dept_id number 4 Primary key
prod_id number 4 Foreign key
I tried this:
CREATE TABLE Department(
dept_id number(4) primary key,
prod_id number(4) foreign key);
It shows error. How can I add a foreign key constraint to this table?
A foreign key defines a relationship between your table DEPARTMENT and another table with a primary key. It means, you cannot create a row in DEPARTMENT with a PROD_ID of 1234 unless there is a pre-existing row in the designated parent table with a value of 1234 as its primary key.
So do you have such an existing parent table? if so you need to include its name in the foreign key definition. Otherwise you must create it.
Let's say the parent table is PRODUCT:
create table product (
prod_id number(4) primary key
, name varchar2(32) not null
);
Then you can create DEPARTMENT with a foreign key like this:
CREATE TABLE Department(
dept_id number(4) primary key,
prod_id references PRODUCT );
Yep, that's all the syntax you need: it automatically creates a column PROD_ID with the same datatype and precision as the primary key column of the referenced table. More verbose syntax is available. Read the Oracle SQL documentation to find out more.
I assume that the other table is named other_table_name and that it contains a primary key named prod_id.
CREATE Department (
dept_id number(4) primary key,
prod_id number(4) REFERENCES other_table_name (prod_id)
);
or a different syntax
CREATE Department (
dept_id number(4) primary key,
prod_id number(4)
...
CONSTRAINT fk_prod_id
FOREIGN KEY (prod_id)
REFERENCES other_table_name (prod_id)
);

I want to have only 3 columns (departure_City_Id, arrival_City_Id, Emp_number). However, when I come to insert value , it says that I have 4 columns

For the Epms table, I want to have only 3 columns (departure_City_Id, arrival_City_Id, Emp_number). However, when I come to insert value, it says that I have 4 columns. The extra one for City_Id. All I want is to avoid having City_Id column in this table. I declare it because I need it just as FK.
CREATE TABLE City (
City_Id char(3),
state varchar(30),
Primary key (City_Id)
);
create table Emps (
Emp_number varchar(30) primary key,
City_Id char(3),
departure_City_Id char(3),
arrival_City_Id char(3),
FOREIGN KEY (City_Id)
REFERENCES City(City_Id),
FOREIGN KEY (City_Id)
REFERENCES City(City_Id)
);
This is pretty straightforward, just specify e.g. , FOREIGN KEY (arrival_city_id) REFERENCES city(city_id) and you'll get an index.
CREATE TABLE emps (
emp_number varchar(30) NOT NULL,
city_id char(3) NOT NULL,
departure_city_id char(3) NOT NULL,
arrival_city_id char(3) NOT NULL,
PRIMARY KEY (emp_number),
KEY city_id (city_id),
KEY departure_city_id (departure_city_id),
KEY arrival_city_id (arrival_city_id),
CONSTRAINT emps_ibfk_1 FOREIGN KEY (city_id) REFERENCES city (city_id),
CONSTRAINT emps_ibfk_2 FOREIGN KEY (departure_city_id) REFERENCES city (city_id),
CONSTRAINT emps_ibfk_3 FOREIGN KEY (arrival_city_id) REFERENCES city (city_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
" I want is to avoid having City_Id column in this table. I declare it because I need it just as FK."
But you don't need it. The foreign key columns are the arrival and departure columns, and those are the ones you must reference in the constraint declarations:
create table Emps (
Emp_number varchar(30) primary key,
departure_City_Id char(3),
arrival_City_Id char(3),
FOREIGN KEY (departure_City_Id)
REFERENCES City(City_Id),
FOREIGN KEY (City_Id)
REFERENCES City(arrival_City_Id)
);
Naming constraints is optional but you may find it helpful when debugging foreign key failures, especially when a table has multiple constraints referencing the same parent key.

ORA-02253: constraint specification not allowed here

create table log_table(
log_id varchar2(1000) primary key,
voter_ID varchar2(1000),
date_logged date
CONSTRAINT abc FOREIGN KEY (voter_ID) REFERENCES voters(voter_ID)
)
The table works when i create it without date element. But when i add the date element to it says:
ORA-02253: constraint specification not allowed here
The table works when i create it without date element
create table log_table(
log_id varchar2(1000) primary key,
voter_ID varchar2(1000), -- comma
CONSTRAINT abc FOREIGN KEY (voter_ID) REFERENCES voters(voter_ID)
)
You have to add , before constraint:
create table log_table(
log_id varchar2(1000) primary key,
voter_ID varchar2(1000),
date_logged date, -- here
CONSTRAINT abc FOREIGN KEY (voter_ID) REFERENCES voters(voter_ID)
)
I would also reconsider datatype of log_id/voter_id as (NUMBER/INTEGER).
Try this:
create table log_table(
log_id varchar2(1000) primary key,
voter_ID varchar2(1000) CONSTRAINT abc REFERENCES voters(voter_ID)
)

Oracle foreign key relation

I have a composite primary key in my Candidate table
CREATE TABLE CANDIDATE(
CANDIDATE_ID VARCHAR(5),
NAME VARCHAR(30),
TELEPHONE NUMBER,
PRIMARY KEY(CANDIDATE_ID, NAME));
When I create a child table, I get an error saying the number of referencing columns must match referenced columns when I create a foreign key for the CANDIDATE_ID
CREATE TABLE JOB(
POSITION_ID VARCHAR(5) PRIMARY KEY,
CANDIDATE_ID VARCHAR(5),
DATE2 DATE,
FOREIGN KEY(CANDIDATE_ID) REFERENCES CANDIDATE);
A table can only have one primary key-- you have a composite primary key. If you have a composite primary key, you have to reference the entire key in your child table. That would mean that the child table would need to have a CANDIDATE_ID column and a NAME column.
CREATE TABLE job (
position_id VARCHAR2(5) PRIMARY KEY,
candidate_id VARCHAR2(5),
name VARCHAR2(30),
date2 DATE,
FOREIGN KEY( candidate_id, name ) REFERENCES candidate( candidate_id, name )
);
Of course, you probably don't want to store the name in both tables. You probably want the candidate_id to be the prmiary key of candidate and you may want to create a separate unique constraint on name.
CREATE TABLE CANDIDATE(
CANDIDATE_ID VARCHAR(5) primary key,
NAME VARCHAR(30) unique,
TELEPHONE NUMBER);
CREATE TABLE JOB(
POSITION_ID VARCHAR(5) PRIMARY KEY,
CANDIDATE_ID VARCHAR(5),
DATE2 DATE,
FOREIGN KEY(CANDIDATE_ID) REFERENCES CANDIDATE(candidate_id));
Assuming that the combination of CANDIDATE_ID and NAME is required for the key to be unique, then you will need to add a reference to the NAME column in your referencing table.
I suspect that CANDIDATE_ID is enough to uniquely identify the candidates in your primary table. If that is the case then it should be your primary key and your relationship will work. If you want to index the NAME separately then do so, but leave it out of the primary key.
Last line should be like this;
CONSTRAINT FK_CANDIDATE_ID FOREIGN KEY (CANDIDATE_ID)REFERENCES CANDIDATE(CANDIDATE_ID);
CREATE TABLE dept
( did char(3) not null,
dname varchar2(20) not null,
CONSTRAINT dept_pk PRIMARY KEY (did)
);
strong text
create table emp
(
eid char(3) unique,
ename varchar2(10) not null,
sal number check (sal between 20000 AND 50000),
city varchar2(10) default 'texus',
did char(3) not null,
constraint fk_did_dept
FOREIGN KEY (did) references
dept(did)
);

Resources