1
CREATE TABLE Prescription (
    pre_id NUMBER(10),
    pssn CHAR(11),
    phy_ssn CHAR(11),
    date CHAR(11),
    quantity INTEGER,
    trade name CHAR(20),
    pharm id CHAR(11), 
    drop_off_time CHAR(10), 
    pick_up_time CHAR(10), 
    status CHAR (15),
    PRIMARY KEY(pre_id),
    FOREIGN KEY (pssn) REFERENCES Pri_Phy_Patient,
    FOREIGN KEY (phy_ssn) REFERENCES Doctor,
    FOREIGN KEY (trade_name, pharm_id) REFERENCES Make_Drug); 

I get ORA-00904:invalid identifier error on using the above sql statement.

The other sql statements related to the above one are:

CREATE TABLE Doctor (
    phy_ssn CHAR(11),
    name CHAR(20), 
    speciality CHAR(30),
    exp_years CHAR(10),
    PRIMARY KEY (phy_ssn));

CREATE TABLE Pri_Phy_Patient (
    pssn CHAR(11),
    name CHAR(20),
    age INTEGER,
    address CHAR(20),
    phy ssn CHAR(11),
    PRIMARY KEY (pssn),
    FOREIGN KEY (phy_ssn) REFERENCES Doctor );

CREATE TABLE Make_Drug (
    trade_name CHAR(20),
    pharm_id CHAR(11),
    formula VARCHAR(100)
    PRIMARY KEY (trade_name, pharm_id),
    FOREIGN KEY (pharm_id) REFERENCES Pharm_co);

Could anyone suggest on what I can do about this?!!

1
  • As Doc123 says, DATE is a reserved word. I didn't spot that one, but I can give you an advice as well : even if you would be able to use DATE as a column description ... what does that say about the column value ? The name of a column should be meaningful. Your developers will thank you afterwards. Another column is "status", that one could also be a bit more indicative. What kind of status ? A limit on the lenght of column naming exist, but don't overdo in the other direction, making all columns only 6 or 8 characters, using general names.
    – tvCa
    Dec 31, 2014 at 14:47

4 Answers 4

1

If you execute the statement in SQL*Plus, you can easily find the error and fix it.

SQL> CREATE TABLE Prescription (
  2      pre_id NUMBER(10),
  3      pssn CHAR(11),
  4      phy_ssn CHAR(11),
  5      date CHAR(11),
  6      quantity INTEGER,
  7      trade name CHAR(20),
  8      pharm id CHAR(11),
  9      drop_off_time CHAR(10),
 10      pick_up_time CHAR(10),
 11      status CHAR (15),
 12      PRIMARY KEY(pre_id),
 13      FOREIGN KEY (pssn) REFERENCES Pri_Phy_Patient,
 14      FOREIGN KEY (phy_ssn) REFERENCES Doctor,
 15      FOREIGN KEY (trade_name, pharm_id) REFERENCES Make_Drug);
    date CHAR(11),
    *
ERROR at line 5:
ORA-00904: : invalid identifier
  1. You cannot use DATE as column name directly. Either keep it in "" double-quotation marks or better avoid using the keyword.

More issues with the create table script -

  1. The column name cannot have two words, trade name is an invalid column name. Change it to trade_name.
  2. Similarly, change pharm id to pharm_id.
  3. Why do you use CHAR data type? It will always have blank padding to the right and consume space. Better use VARCHAR2 data type.
0

Change DATE column name with any other because you cannot use DATE as column name or table name. You can get more detail of reserved word of Oracle from following link http://www.petefreitag.com/tools/sql_reserved_words_checker/

0

This script works:

CREATE TABLE Doctor (
    phy_ssn CHAR(11) not null,
    name CHAR(20), 
    speciality CHAR(30),
    exp_years CHAR(10),
    constraint pk_phy_ssn PRIMARY KEY (phy_ssn));

CREATE TABLE Pri_Phy_Patient (
    pssn CHAR(11) not null,
    name CHAR(20),
    age INTEGER,
    address CHAR(20),
    phy_ssn CHAR(11),
    constraint pk_pssn PRIMARY KEY (pssn),
    constraint fk_ppp_phy_ssn FOREIGN KEY (phy_ssn) REFERENCES Doctor(phy_ssn) );

CREATE TABLE Make_Drug (
    trade_name CHAR(20) not null,
    pharm_id CHAR(11) not null,
    formula VARCHAR(100),
    constraint pk_trname_phid PRIMARY KEY (trade_name, pharm_id));

CREATE TABLE Prescription (
    pre_id NUMBER(10) not null,
    pssn CHAR(11),
    phy_ssn CHAR(11),
    date_time CHAR(11),
    quantity INTEGER,
    trade_name CHAR(20),
    pharm_id CHAR(11), 
    drop_off_time CHAR(10), 
    pick_up_time CHAR(10), 
    status CHAR(15),
    constraint pk_pre_id PRIMARY KEY(pre_id),
    constraint fk_pre_pssn FOREIGN KEY (pssn) REFERENCES Pri_Phy_Patient(pssn),
    constraint fk_pre_phy_ssn FOREIGN KEY (phy_ssn) REFERENCES Doctor(phy_ssn),
    constraint fk_pre_tr_nm_ph_id FOREIGN KEY (trade_name, pharm_id) REFERENCES Make_Drug(trade_name, pharm_id));

UPDATE

The issue is mostly due to spaces in the middle of column names, missing commas and the invalid identifier was raised because you tried to use date as a column name and Oracle didn't like it.

You can play around with this Demo.

5
  • did you create the other tables successfully first?
    – benji
    Dec 31, 2014 at 3:15
  • @RachelPhilomena the invalid identifier is raised because of the date column in the prescription table. Look at the demo I shared in the answer.
    – benji
    Dec 31, 2014 at 3:46
  • Thank you for all the inputs. @benji yup it works when I place date in double quotation. Dec 31, 2014 at 5:52
  • @benji - You should really include the answer in your answer, instead of just a link to it. The code that you've shown here doesn't work, but the code in your SQL fiddle does. Jan 4, 2015 at 21:55
  • @DavidWallace you're absolutely right. I forgot to update that part.. just did it now. Thanks
    – benji
    Jan 4, 2015 at 23:10
-2

date CHAR(11) Date is a reserved word in SQL. It cannot be declared as a column in the way you did. Please change the column name and it will work.

1
  • It is not the only issue, there are also other issues as I have mentioned in my answer. Dec 31, 2014 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.