missing right parenthesis problems - oracle

i didn't know what the problem is. First this one works fine in sql
create table Department
(Department_Id number(8) PRIMARY KEY ,
Dept_Name varchar(20),
Location varchar(20));
but the second one says missing right parenthesis ora-00907
create table Instructor(Instructor_ID number(8) PRIMARY KEY ,
Department_Id number(8) FOREIGN KEY REFERENCES Department(Department_Id) ,
Ins_name varchar2(20) ,
Position varchar(20) ,
email_Id varchar (40),
Contact_No number(10),
Date_Of_Joining date);

Leave out the FOREIGN KEY part:
Department_Id number(8) REFERENCES Department(Department_Id) ,
See Oracle FAQs for an example. The FOREIGN KEY keywords are for out-of-line constraints, which go after the column definitions.

Related

ORA-02267: incompatible with the data type or collation of the referenced column

create table doctor
(
name varchar2(20)
, d_id varchar2(20)
, address varchar2(50)
, phone_number number(10)
, qualification varchar2(20)
, gender varchar2(20)
, constraint pk_doctor primary key(d_id)
)
;
create table room
(
room_id varchar2(5)
, room_type varchar2(20)
, constraint pk_room primary key(room_id)
)
;
create table patient
(
p_id varchar2(10)
, p_name varchar2(20)
, p_age number(3)
, p_gender varchar2(10)
, address varchar2(50)
, date_admission date
, phone_number number(10)
, room_id varchar2(5)
, constraint pk_patient primary key(p_id)
, constraint fk_p1 foreign key(room_id) references room
)
;
create table bill
(
bill_no varchar2(10)
, bill_date date
, p_id varchar2(10)
, p_name varchar2(20)
, p_age number(3)
, p_gender varchar2(10)
, date_admission date
, date_discharge date
, room_charges number(10)
, pathology_fees number(10)
, d_fees number(10)
, miscellaneous number(10)
, total_amount number(10)
, constraint pk_bill primary key(bill_no)
, constraint fk_b1 foreign key(p_id) references patient
, constraint fk_b2 foreign key(p_name) references patient
, constraint fk_b3 foreign key(p_age) references patient
, constraint fk_b4 foreign key(p_gender) references patient
, constraint fk_b5 foreign key(date_admission) references patient
)
;
Error starting at line : 15 in command -
create table bill
(
bill_no varchar2(10)
, bill_date date
, p_id varchar2(10)
, p_name varchar2(20)
, p_age number(3)
, p_gender varchar2(10)
, date_admission date
, date_discharge date
, room_charges number(10)
, pathology_fees number(10)
, d_fees number(10)
, miscellaneous number(10)
, total_amount number(10)
, constraint pk_bill primary key(bill_no)
, constraint fk_b1 foreign key(p_id) references patient
, constraint fk_b2 foreign key(p_name) references patient
, constraint fk_b3 foreign key(p_age) references patient
, constraint fk_b4 foreign key(p_gender) references patient
, constraint fk_b5 foreign key(date_admission) references patient
)
Error report -
ORA-02267: column type incompatible with referenced column type
02267. 00000 - "column type incompatible with referenced column type"
*Cause: The data type or collation of the referencing column was
incompatible with the data type or collation of the referenced
column.
*Action: Select a compatible data type for the referencing column.
Also, the collation of a character column in a foreign key must
match the collation of the corresponding column in the primary
key.
I understand what the error is trying to tell but my datatypes are same in both tables but still, it is showing the error??
Please tell me where I made a mistake. I would be very grateful.
From the documentation:
If you identify only the parent table or view and omit the column name, then the foreign key automatically references the primary key of the parent table or view.
So you're effectively actually doing:
, constraint fk_b1 foreign key(p_id) references patient
, constraint fk_b2 foreign key(p_name) references patient(p_id)
, constraint fk_b3 foreign key(p_age) references patient(p_id)
, constraint fk_b4 foreign key(p_gender) references patient(p_id)
, constraint fk_b5 foreign key(date_admission) references patient(p_id)
which obviously isn't what you want, and explains the error you get.
You need to specify the matching non-PK columns:
, constraint fk_b1 foreign key(p_id) references patient
, constraint fk_b2 foreign key(p_name) references patient(p_name)
, constraint fk_b3 foreign key(p_age) references patient(p_age)
, constraint fk_b4 foreign key(p_gender) references patient(p_gender)
, constraint fk_b5 foreign key(date_admission) references patient(date_admission)
However, this will not get
ORA-02270: no matching unique or primary key for this column-list
because those four columns aren't suitable targets; none can really be unique, at least safely. And it wouldn't allow data to change - name and gender could, but age will, for example. These may be recording the patient's status on admission though, so then those wouldn't change, other than to correct mistakes.
It doesn't really make sense to duplicate/denormalise the data. You can just have the PK reference and join to the main table as needed to get the other information.
db<>fiddle
Splitting patient into a table to identify an individual (with date of birth rather than age) and a separate table that records each stay for that patient - with room and admission/discharge dates, for example - might make more sense. Only recording the discharge date on the bill seems odd.
The problem you're facing comes from bad data model which is needed for some rework.
The "bill" table contains p_name, p_age, p_gender, and date_admission columns. Same columns exist in the "patient" table, so you don't need them in the "bill". Just remove the columns from the "bill" definition and it'll be fine.
Whenever you need to get patient's name for the bill, you can always get it using p_id:
select p.p_name, p.p_age
from patient p
join bill b
on b.p_id, p.p_id
where b.bill_no = 'some_bill_no'
And keeping patient's age will lead to an engineering overhead because you'll have to update this field constantly once per year. I'd suggest you to store patient's birth_date and calculate his/her age when needed.
Of course when it is not a homework with gives table structure

oracle identifier too long

New to oracle
Im trying to assign composite key
CREATE TABLE rep_contracts
( Store_id NUMBER(8),
Name NUMBER(5)
Quarter CHAR(3),
Rep_id NUMBER(5),
CONSTRAINT rep_contracts_rep_idstore_id_pk PRIMARY KEY (rep_id, store_id),
CONSTRAINT rep_contracts_rep_id_fk FOREIGN KEY (rep_id),
CONSTRAINT rep_contracts_store_id_fk FOREIGN KEY (store_id) );
and im receiving
ORA-00972: identifier is too long
Im aware that composit key identifier longer than 30 char
but if I cut identifier few character shorter ex:
rep_contrac_rep_idstore_id_pk
then I receive
ORA-00905: missing keyword
I couldn't find any workaround
I cleaned the script and incorporated the comments:
added a comma after number(5)
changed all names to uppercase to save pain later
changed NAME which is an Oracle reserved key work to S_NAME
changed the data type from CHAR to VARCHAR2
added the references keyword to point to the table where the foreign key is
renamed the constraint so it is not more than 30 characters
like so:
CREATE TABLE rep_contracts
( STORE_ID NUMBER(8),
S_NAME NUMBER(5),
QUARTER VARCHAR2(3),
REP_ID NUMBER(5),
CONSTRAINT rep_cont_rep_idstore_id_pk PRIMARY KEY (REP_ID, STORE_ID),
CONSTRAINT rep_contracts_rep_id_fk FOREIGN KEY REFERENCES your_table(REP_ID),
CONSTRAINT rep_contracts_store_id_fk FOREIGN KEY REFERENCES your_table(STORE_ID) );

ORA-00911: invalid character

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
)

missing right parenthesis SQL

I am new to SQL.
I am trying to create a table:
CREATE TABLE account
(AccountNo NUMBER(2) PRIMARY KEY,
AccountType VARCHAR(1) FOREIGN KEY REFERENCES account_type(TypeCode),
CustomerRef NUMBER(2) FOREIGN KEY REFERENCES bank_customer(CustomerRef),
DateOpened DATE,
CurrentBalence NUMBER(6,2),
OverdraftLimit NUMBER(5,2));
However it comes up with: ORA-00907: missing right parenthesis
I know for a fact that you can have to foreign keys, so that's not the problem. Could someone give me a had with the solution of creating the table?
A foreign key actually has to refer to something. In your case you have to tell Oracle what AccountType and CustomerRef refer to. Usually it looks as follows:
AccountType VARCHAR(1) FOREIGN KEY REFERENCES TABLE_NAME(COLUMN_NAME)
Of course, you have to replace TABLE_NAME and COLUMN_NAME with the name of the table and the name of the column you're referring to.
There are two notations you can use when declaring constraints:
1) inline (a constraint):
CREATE TABLE account
(AccountNo NUMBER(2) PRIMARY KEY,
AccountType VARCHAR(1) REFERENCES account_type(TypeCode),
CustomerRef NUMBER(2) REFERENCES bank_customer(CustomerRef),
DateOpened DATE,
CurrentBalence NUMBER(6,2),
OverdraftLimit NUMBER(5,2));
2) out-of-line
CREATE TABLE account
(
AccountNo NUMBER(2) PRIMARY KEY,
AccountType VARCHAR(1),
CustomerRef NUMBER(2),
DateOpened DATE,
CurrentBalence NUMBER(6,2),
OverdraftLimit NUMBER(5,2),
FOREIGN KEY(AccountType) REFERENCES account_type(TypeCode),
FOREIGN KEY(CustomerRef) REFERENCES account_type(TypeCode)
);
In both cases you can prepend the constraint declaration with CONSTRAINT <name> to give your name to a constraint, otherwise Oracle assigns its own name.
inline notation is applied to a column where the constraint is declared, out-of-line is applied to the table. There slight differences in syntax + some restrictions, for example you cannot declare out-of-line NULL constraint.
You can use inline and out-of-line syntax in CREATE TABLE and ALTER TABLE. Refer to Oracle documentation for more information
You need to define the foreign keys with REFERENCES. In the below snippet, replace AccountTypeTable and CustomerRefTable with the correct table names and replace typeColumn and refColumn with the correct column names in those tables these match to.
UPDATE
I added in the correct values from your comment on another answer
CREATE TABLE account (
AccountNo NUMBER(2) PRIMARY KEY,
AccountType VARCHAR(1),
CustomerRef NUMBER(2),
DateOpened DATE,
CurrentBalence NUMBER(6,2),
OverdraftLimit NUMBER(5,2),
CONSTRAINT account_fk1 FOREIGN KEY (AccountType) REFERENCES Account_Type(TypeCode),
CONSTRAINT account_fk2 FOREIGN KEY (CustomerRef) REFERENCES Bank_Customer(CustomerRef));
you are missing foreign key syntax...
CREATE TABLE account
(AccountNo NUMBER(2) PRIMARY KEY (P_ID),
AccountType VARCHAR(1) FOREIGN KEY (F1_ID) REFERENCES <table_name>(field_name>),
CustomerRef NUMBER(2) FOREIGN KEY (F2_ID) REFERENCES <table_name>(field_name>),
DateOpened DATE,
CurrentBalence NUMBER(6,2),
OverdraftLimit NUMBER(5,2));

ORA-00907: missing right parenthesis Error while creating a table?

I am new to oracle,
I have created two tables using following queries,
CREATE TABLE employee
(
emp_name VARCHAR(20) NOT NULL,
street VARCHAR(50) NOT NULL,
city VARCHAR(20) NOT NULL,
PRIMARY KEY(emp_name)
)
and
CREATE TABLE company
(
comp_name VARCHAR(20) NOT NULL,
city VARCHAR(20) NOT NULL,
PRIMARY KEY(comp_name)
)
Now I am trying to create another table using some foreign keys,
CREATE TABLE works
(
emp_name varchar(20) NOT NULL,
comp_name varchar(20) NOT NULL,
salary int(10) NOT NULL,
FOREIGN KEY(emp_name) REFERENCES employee(emp_name),
FOREIGN KEY(comp_name) REFERENCES company(comp_name)
)
Getting ERROR : ORA-00907: missing right parenthesis
I have also tried with
CREATE TABLE works
(
emp_name varchar(20) NOT NULL,
comp_name varchar(20) NOT NULL,
salary int(10) NOT NULL,
constraint wemployee FOREIGN KEY(emp_name) REFERENCES employee(emp_name),
constraint wcompany FOREIGN KEY(comp_name) REFERENCES company(comp_name)
)
But getting same error.
Can any one tell me that where I am doing mistake?
I'm no expert in oracle, but are you allowed to specify the (10) in salary int(10) NOT NULL?
1: you should have a table called "test" with two columns, id and testdata. (This is just a dumb quick example, so I won't bother to specify any constraints on id.)
create table test (id number, testdata varchar2(255));
2: Next we'll create a sequence to use for the id numbers in our test table.
create sequence test_seq
start with 1
increment by 1
nomaxvalue;
You could change "start with 1" to any number you want to begin with (e.g. if you already have 213 entries in a table and you want to begin using this for your 214th entry, replace with "start with 214"). The "increment by 1" clause is the default, so you could omit it. You could also replace it with "increment by n" if you want it to skip n-1 numbers between id numbers. The "nomaxvalue" tells it to keep incrementing forever as opposed to resetting at some point.i (I'm sure Oracle has some limitation on how big it can get, but I don't know what that limit is).
3: Now we're ready to create the trigger that will automatically insert the next number from the sequence into the id column.
create trigger test_trigger
before insert on test
for each row beginselect test_seq.nextval into :new.id from dual;
end;
/
There are two different ways to create a table with constraints:
1)
create table department(
deptno number(5) primary key,
deptname varchar2(30),
empno number(5) references emp(empno));
2)
create table department(
deptno number(5),
deptname varchar2(30),
empno number(5),
constraint pkey_deptno primary key(deptno),
constraint fkey_empno foreign key(empno) references Emp(empno));
When creating the index inline with the rest of the table creation statement try dropping the FOREIGN KEY part:
CREATE TABLE works
(
emp_name varchar(20) NOT NULL,
comp_name varchar(20) NOT NULL,
salary int(10) NOT NULL,
emp_name REFERENCES employee(emp_name),
comp_name REFERENCES company(comp_name)
)
See this question for more details:
ORA-00907: missing right parenthesis

Resources