Problem in creating new table ORA-00906: missing left parenthesis - sql

Here is my instruction :
it works on my friend computer !
CREATE TABLE typeRdv1(
id_type int PRIMARY KEY,
des VARCHAR(20)
);
CREATE TABLE rdv1(
id_rdv int PRIMARY KEY,
cin_pat VARCHAR(20),
date_rdv VARCHAR NOT NULL,
type_rdv VARCHAR(20),
state VARCHAR(20),
CONSTRAINT `FK_cin_rdv` FOREIGN KEY (cin_pat)
REFERENCES patients(cin_pat) ON DELETE NO ACTION,
CONSTRAINT `FK_id_type_rdv` FOREIGN KEY (type_rdv)
REFERENCES typeRdv1(id_type) ON DELETE NO ACTION
);
A ORA-00906: missing left parenthesis
Any help !

Please make sure all the "varchar" fields has a length defined. In your second table, it says: date_rdv VARCHAR NOT NULL, which I believe causing the issue, so try to change it into date_rdv VARCHAR(50) NOT NULL and try again.
...
CREATE TABLE rdv1(
id_rdv int PRIMARY KEY,
cin_pat VARCHAR(20),
date_rdv VARCHAR(50) NOT NULL, --length added to varchar
type_rdv VARCHAR(20),
state VARCHAR(20),
...
EDIT:
The reason you get "invalid character" error is that you cannot use "on delete no action" while creating a table with a constraint. Instead you should alter your table after to create your constraints with "on delete cascade" option. Probably like below ( you may need to drop your constraints first):
CREATE TABLE patients(
cin_pat int PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE typeRdv1(
id_type int PRIMARY KEY,
des VARCHAR(20)
);
CREATE TABLE rdv1(
id_rdv int PRIMARY KEY,
cin_pat VARCHAR(20),
date_rdv VARCHAR(50) NOT NULL,
type_rdv VARCHAR(20),
state VARCHAR(20)
);
alter table rdv1 add (
CONSTRAINT FK_cin_rdv FOREIGN KEY (cin_pat)
REFERENCES patients (cin_pat) ON DELETE cascade,
CONSTRAINT FK_id_type_rdv FOREIGN KEY (type_rdv)
REFERENCES typeRdv1 (id_type) ON DELETE cascade
);
Check this for more details: https://www.haidongji.com/2006/07/24/defining-no-action-foreign-key-constraints-in-oracle/comment-page-1/

Related

ERROR :ORA-02264: name already used by an existing constraint

create table ward
(
wnum int primary key,
wname varchar(30),
phoneno int,
wloc varchar(50),
chnursename varchar(20) constraint ward_fk references charge_nurse
);
create table charge_nurse
(
chnurse varchar(20) constraint charge_nurse_pk primary key,
stnum int constraint charge_nurse_fk references staff
);
create table staff
(
stname varchar(20),
stnum int constraint staff_pk primary key,
addr varchar(20),
phoneno int,
stposition varchar(30),
specality varchar(30) unique,
shift varchar(10),
noofhoursperweek int
);
create table generalsupplies
(
itnum int constraint generalsupplies_pk primary key,
itname varchar(20) unique,
quantityinstock int,
reorder varchar(10),
despt varchar(10),
costperunit int
);
create table pharmasupplies
(
dnum int constraint pharmasupplies_pk primary key,
dname varchar(30) unique,
despt varchar(20),
dosage_Mg int,
quantityinstock int,
reorder varchar(10),
costperunit int
);
While creating the below table I am facing problem:
ORA-02264: name already used by an existing constraint
create table centralsupplies
(
wardnum int constraint centralsupplies_fk references ward,
itemnum int constraint centralsupplies_fk references generalsupplies,
drugnum int constraint centralsupplies_fk references pharmasupplies,
quantity_required varchar(20),
staffname varchar(10) references staff(stname),
staffnum int constraint centralsupplies_fk references staff,
regnum int unique,
dateord date,
daterec date
);
How do I solve this problem?
You use 3 times the same constraint name centralsupplies_fk in your centralsupplies table.
3 constraints = 3 constraint names
Your create table statement has four foreign keys all called centralsupplies_fk. That is not allowed: constraint names must be unique within a schema. You must give each one a different name.
It is usual practice to include the referenced table in the key name. So
create table centralsupplies
(
wardnum int constraint centralsupplies_ward_fk references ward,
itemnum int constraint centralsupplies_generalsupplies_fk references generalsupplies,
drugnum int constraint centralsupplies_pharmasupplies_fk references pharmasupplies,
quantity_required varchar(20),
staffname varchar(10) references staff(stname),
staffnum int constraint centralsupplies_staff_fk references staff,
regnum int unique,
dateord date,
daterec date
)
Also you have another foreign key constraint on STAFFNAME which you have not named. You do not need to name constraints, the system will generate a unique one for you, but it's generally a good idea to name them, not least because it is easier to diagnose relational integrity error messages with meaningfully named constraints.
However, in this case the correct solution is to drop the STAFFNAME column. You already have a foreign on the STAFF table, and you should join to that table whenever you need to display a value for STAFFNAME. Besides you do not have a unique constraint on staff.stname column, so the foreign key statement will fail: foreign keys can only reference primary or unique keys.

Can't figure out how to make a subquery

CREATE TABLE TBLTeams
(
teamnm varchar(30) NOT NULL,
teamid varchar(10),
rondenm varchar(10)
primary key(teamnm)
)
CREATE TABLE TBLWedstrijd
(
thuisteamnm varchar(30) NOT NULL,
uitteamnm varchar(30) NOT NULL,
wedstrijdid varchar(10)
primary key(wedstrijdid)
)
alter table TBLWedstrijd add foreign key (thuisteamnm) references TBLTeams(teamnm)
alter table TBLWedstrijd add foreign key (uitteamnm) references TBLTeams(teamnm)
CREATE TABLE TBLUitslag
(
thuisteamnm varchar(30) NOT NULL,
uitteamnm varchar(30) NOT NULL,
wedstrijdid varchar(10) NOT NULL,
uitteampunt int,
thuisteampunt int
primary key(thuisteamnm)
)
alter table TBLUitslag add foreign key (wedstrijdid) references TBLWedstrijd(wedstrijdid)
If I update teamnm in TBLTeams how can I update reference in TBLWedstrijd uitteamnm. I know its a subquery but I really dont know how.
You're going about it a bit wrong. If you're going to change the teamname, you shouldn't use it as a key (Since that will break all references).
You'll want to reference their IDs from the TBLWedstrijd instead of using their name.
So your statements will become:
CREATE TABLE TBLWedstrijd
(
wedstrijdid varchar(10),
thuisteamid varchar(10) NOT NULL,
uitteamid varchar(10) NOT NULL,
primary key(wedstrijdid)
)
alter table TBLWedstrijd add foreign key (thuisteamid) references TBLTeams(teamid)
alter table TBLWedstrijd add foreign key (uitteamid) references TBLTeams(teamid)
The same is valid for your table TBLUitslag, you should only reference TBLWedstrijd, since that one already holds the info about which teams are playing. Not to mention, the score is pretty much part of a game anyway so it doesn't make a lot of sense to seperate those into 2 tables. So that becomes:
CREATE TABLE TBLWedstrijd
(
wedstrijdid varchar(10),
thuisteamid varchar(10) NOT NULL,
uitteamid varchar(10) NOT NULL,
uitteampunt int,
thuisteampunt int
primary key(wedstrijdid)
)
This is called "table normalisation" if you wanted to google some stuff for more info about it.

SQL ALTER TABLE foreign key

I have a problem with adding foreign keys with alter table command. I don't know how to make it so it works.
I need to add ISIK_ID and STAADION_ID to ISIK_STAADIONIL table as foreign key.
Here is my code:
CREATE TABLE ISIK(
ISIK_ID INT NOT NULL,
EESNIMI VARCHAR(25) NOT NULL,
PEREKONNANIMI VARCHAR(25) NOT NULL,
ISIKUKOOD VARCHAR(20),
KODAKONDSUS VARCHAR(30),
SUGU CHAR(1) NOT NULL,
HARIDUSTASE CHAR(1) NOT NULL,
TELEFONI_NR VARCHAR(20),
SYNNIPAEV DATE,
CONSTRAINT ISIK_ID_PK PRIMARY KEY (ISIK_ID)
);
CREATE TABLE ISIK_STAADIONIL(
ISIK_STAADIONIL_ID INT NOT NULL,
CONSTRAINT ISIK__STAADIONIL_ID_PK PRIMARY KEY (ISIK_STAADIONIL_ID),
ALATES TIMESTAMP,
KUNI TIMESTAMP
);
CREATE TABLE STAADION(
STAADION_ID INT NOT NULL,
NIMETUS VARCHAR(20),
KIRJELDUS VARCHAR(100),
ASUKOHT VARCHAR(50),
SUURUS VARCHAR(20),
MAHUTAVUS INT,
EHITATUD VARCHAR(20),
EHITAJA VARCHAR(20),
CONSTRAINT STAADION_ID_PK PRIMARY KEY (STAADION_ID)
);
ALTER TABLE ISIK_STAADIONIL
ADD CONSTRAINT ISIK_ID_FK
FOREIGN KEY(ISIK_ID)
REFERENCES ISIK(ID);
You need to add the columns first (to the table) before you can create FK constraints using them.
CREATE TABLE ISIK_STAADIONIL(
ISIK_STAADIONIL_ID INT NOT NULL,
CONSTRAINT ISIK__STAADIONIL_ID_PK PRIMARY KEY (ISIK_STAADIONIL_ID),
ALATES TIMESTAMP,
KUNI TIMESTAMP,
ISIK_ID INT,
STAADION_ID INT
);
And then your ALTER statement should work fine.
UPDATE
Changing the table-design slightly would make more sense, as per following. The logic is that ISIK_ID and STAADION_ID would together be sufficient to determine uniqueness in the ISIK_STAADIONIL table, so a separate ID is redundant:
CREATE TABLE ISIK_STAADIONIL(
ISIK_ID INT NOT NULL,
STAADION_ID INT NOT NULL,
CONSTRAINT ISIK__STAADIONIL_ID_PK PRIMARY KEY (ISIK_ID, STAADION_ID),
ALATES TIMESTAMP,
KUNI TIMESTAMP
);

Issues with already defined PK's: no matching unique or primary key for this column-list

I am aware this error is telling me I shouldn't have a FK of something that isn't a PK, but I think that's not my case and I haven't found a way around it, so here's my code:
I have this ENTITY table
CREATE TABLE ENTITY(
entity_id VARCHAR(20) PRIMARY KEY,
name VARCHAR(30)
);
No problem there, the problem comes when I try to create another table with entity_id as a FK, as such:
CREATE TABLE EXPORTER(
exporter_id VARCHAR(20) PRIMARY KEY,
name VARCHAR(30),
FOREIGN KEY (exporter_id) REFERENCES ENTITY(ENTITY_ID)
);
Any help would be appreciated.
try this:
CREATE TABLE ENTITY(
entity_id VARCHAR(20) PRIMARY KEY,
name VARCHAR(30)
);
CREATE TABLE EXPORTER(
exporter_id VARCHAR(20) PRIMARY KEY,
name VARCHAR(30),
CONSTRAINT FK_exporter_id
FOREIGN KEY (exporter_id)
REFERENCES ENTITY(entity_id)
);

ORA-00904: : invalid identifier two references in Oracle

I cant find a good anwser too my question, I want to create a table that have references too one table but i use it twice in the code..
Vårdnadshavare VARCHAR(11),
FOREIGN KEY (Vårdnadshavare) REFERENCES Person(Personnummer),
Barn VARCHAR(11),
FOREIGN KEY (Barn) REFERENCES Person(Personnummer)
But then i get the error:
ORA-02256: number of referencing columns must match referenced columns
I understand that my reference is wrong some how but can´t figure it out...
Sorry for the swedish words!!
Create table Ärende(
Ärendenr VARCHAR(50) NOT NULL,
Handläggare VARCHAR(50),
FOREIGN KEY (Handläggare) REFERENCES Handläggare(Anställningsnr),
Vårdnadshavare VARCHAR(11),
FOREIGN KEY (Vårdnadshavare) REFERENCES Person(Personnummer),
Barn VARCHAR(11),
FOREIGN KEY (Barn) REFERENCES Person(Personnummer),
In Datum VARCHAR(50),
Ömmande Skäl VARCHAR(5),
Förskola VARCHAR(50),
FOREIGN KEY (Förskola) REFERENCES Förskola(IDnr),
PRIMARY KEY (Ärendenr)
);
I have used Person(Personnumer) as a reference before just not twice in createing a table..
I created this script which does work. Probably there is an issue with your indexes:
create table handläggare
( anställningsnr varchar(50) primary key
)
create table person
( personnummer varchar(11) primary key
)
;
create table förskola
( idnr varchar(50) primary key
);
create table ärende
( ärendenr varchar(50) not null
, handläggare varchar(50)
, foreign key (handläggare) references handläggare(anställningsnr)
, vårdnadshavare varchar(11)
, foreign key (vårdnadshavare) references person(personnummer)
, barn varchar(11)
, foreign key (barn) references person(personnummer)
, indatum varchar(50)
, ömmandeskäl varchar(5)
, förskola varchar(50)
, foreign key (förskola) references förskola(idnr)
, primary key (ärendenr)
);
There are two other issues in your script above which I edited:
In Datum should be one name, not separated by a space;
Ömmande Skäl, same as above.
In you create statement you have provided 4 referenes:
Handläggare VARCHAR(50),
FOREIGN KEY (Handläggare) REFERENCES Handläggare(Anställningsnr),
Vårdnadshavare VARCHAR(11),
FOREIGN KEY (Vårdnadshavare) REFERENCES Person(Personnummer),
Barn VARCHAR(11),
FOREIGN KEY (Barn) REFERENCES Person(Personnummer),
Förskola VARCHAR(50),
FOREIGN KEY (Förskola) REFERENCES Förskola(IDnr),
Please check that all 3 distinct columns are of same data type.
That means:
Handläggare(Anställningsnr) should be VARCHAR(50)
Person(Personnummer) should be VARCHAR(11)
Förskola(IDnr) should be VARCHAR(50).
As per me i think you are making a mistake in
Förskola(IDnr) should be VARCHAR(50)
which should have been a Number