"ORA-00907: missing right parenthesis" in foreign key declaration - sql

I am a university student who are new to Oracle Live SQL. My professor give us one page of code and let us debugging.
Only 1 error in the last paragraph(Enrollment TABLE) left. All tables except Enrollment table have been successfully created. I spent 6 hours on last paragraph but still can not find the wrong place. I have checked all the information on the web and did not find solution.
Please help, thanks!
CREATE TABLE Faculty (
FacNo CHAR(11) NOT NULL,
FacFirstName VARCHAR(30) NOT NULL,
FacLastName VARCHAR(30) NOT NULL,
FacCity VARCHAR(30) NOT NULL,
FacState CHAR(2) NOT NULL,
FacDept CHAR(6) NULL,
FacRank CHAR(4) NULL,
FacSalary DECIMAL(10,2) NULL,
FacSupervisor CHAR(11) NULL,
FacHireDate DATE NULL,
FacZipCode CHAR(10) NOT NULL,
CONSTRAINT FacultyPK PRIMARY KEY (FacNo),
CONSTRAINT SupervisorFK FOREIGN KEY (FacSupervisor) REFERENCES Faculty);
CREATE TABLE Course (
CourseNo CHAR(6) NOT NULL,
CrsDesc VARCHAR(50) NOT NULL,
CrsUnits INTEGER NULL,
CONSTRAINT CoursePK PRIMARY KEY (CourseNo) );
CREATE TABLE Offering (
OfferNo INTEGER NOT NULL,
CourseNo CHAR(6) NOT NULL,
OffTerm CHAR(6) NOT NULL,
OffYear INTEGER NOT NULL,
OffLocation VARCHAR(30) NULL,
OffTime VARCHAR(10) NULL,
FacNo CHAR(11) NULL,
OffDays CHAR(4) NULL,
CONSTRAINT OfferingPK PRIMARY KEY (OfferNo),
CONSTRAINT CourseFK FOREIGN KEY (CourseNo) REFERENCES Course,
CONSTRAINT FacultyFK FOREIGN KEY (FacNo) REFERENCES Faculty);
CREATE TABLE Student (
StdNo CHAR(11) NOT NULL,
StdFirstName VARCHAR(30) NOT NULL,
StdLastName VARCHAR(30) NOT NULL,
StdCity VARCHAR(30) NOT NULL,
StdState CHAR(2) NOT NULL,
StdZip CHAR(10) NOT NULL,
StdMajor CHAR(6) NULL,
StdClass CHAR(2) NULL,
StdGPA DECIMAL(3,2) NULL,
CONSTRAINT StudentPk PRIMARY KEY (StdNo) )
CREATE TABLE Enrollment(
OfferNo INTEGER NOT NULL,
StdNo CHAR(11) NOT NULL,
EnrGrade DECIMAL(3,2) NULL,
CONSTRAINT EnrollmentPK PRIMARY KEY (OfferNo,StdNo),
CONSTRAINT OfferingFK FOREIGN KEY (OfferNo) REFERENCES Offering
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT StudentFK FOREIGN KEY (StdNo) REFERENCES Student
ON DELETE CASCADE
ON UPDATE CASCADE);

First of all, there is missing semicolon at the end of the CREATE TABLE Student statement.
But I believe that what your teacher wants to emphasize is that ON UPDATE is not supported by Oracle foreign keys. As far as concerns, Oracle's point of view is that primary keys are meant to be immutable.
If you comment this part of the statement, the code runs fine :
CREATE TABLE Enrollment(
OfferNo INTEGER NOT NULL,
StdNo CHAR(11) NOT NULL,
EnrGrade DECIMAL(3,2) NULL,
CONSTRAINT EnrollmentPK PRIMARY KEY (OfferNo,StdNo),
CONSTRAINT OfferingFK FOREIGN KEY (OfferNo) REFERENCES Offering
ON DELETE CASCADE,
-- ON UPDATE CASCADE,
CONSTRAINT StudentFK FOREIGN KEY (StdNo) REFERENCES Student
ON DELETE CASCADE
-- ON UPDATE CASCADE
);
Demo on DB Fiddle.

Related

Table with foreign key won't compile (no matching unique or primary key for this column-list)

I've create two tables with some columns and all that stuff. And I get the error "no matching unique or primary key for this column-list", but I've no clue what I am missing...
It's a basic primary and foreign keys tables and it won't compile. I have check on internet for hours and found out nothing.
create table TP2_ITEM_FAVORI (
NOM_UTILISATEUR varchar2(30) not null,
NO_ITEM number(6) not null,
constraint PK_ITEM_FAV primary key(NOM_UTILISATEUR, NO_ITEM));
create table TP2_UTILISATEUR (
NOM_UTILISATEUR varchar2(30) not null,
NO_ENCAN number(6) not null,
MOT_DE_PASSE_UTI varchar(30) not null,
NOM_UTI varchar2(20) not null,
PRENOM_UTI varchar2(20) not null,
TEL_UTI char(13) not null,
COURRIEL_UTI varchar2(25) not null,
TYPE_UTI varchar(20) not null,
NOM_UTILISATEUR_PARENT varchar2(30) not null,
constraint PK_UTILISATEUR primary key(NOM_UTILISATEUR),
constraint AK_NOM_PRENOM_TEL_UTI unique(NOM_UTI, PRENOM_UTI, TEL_UTI),
constraint AK_COURR_UTI unique(COURRIEL_UTI),
constraint FK_NOM_UTILISATEUR_UTI foreign key(NOM_UTILISATEUR) references
TP2_ITEM_FAVORI(NOM_UTILISATEUR));
A foreign key needs to be a primary key or unique column of another table: https://www.techonthenet.com/oracle/errors/ora02270.php.
In TP2_UTILISATEUR, you references TP2_ITEM_FAVORI(NOM_UTILISATEUR) as a foreign key, but NOM_UTILISATEUR is not the primary key of TP2_ITEM_FAVORI. TP2_ITEM_FAVORI's primary key is (NOM_UTILISATEUR, NO_ITEM).
Resolution is to change TP2_ITEM_FAVORI's primary key to NOM_UTILISATEUR instead of both columns.

How to select a common attribute from 2 different tables, with the highest appearence of a mutual attribute?

So i have a table "accident", and the attribute "pess_segura" which is the number of an ensured person envolved in an accidente.
I also have a table "acid_pess" which has 2 relevant attributes for this problem "accident" and "pessoa", this table refers to people envolved in accidents that are not ensured (they were involved in an accident but they were not driving).
So i want to select the accident with most people envolved.
These are the relevant tables for this exercise
create table person(
n_pess bigint not null,
nif integer not null,
cart_cond varchar(15),
pais_cart_cond varchar(30),
doc_id varchar(15) not null,
pais_doc_id varchar(30) not null,
nome varchar(80) not null,
primary key(n_pess),
unique(nif),
unique(cart_cond),
unique(doc_id)
);
create table ensurance(
apolice bigint not null,
ensured bigint not null,
veiculo bigint not null,
datai date not null,
dataf date not null,
cobertura numeric(10,2) not null,
primary key(apolice),
unique(ensured, veiculo),
foreign key (ensured) references person(n_pess),
foreign key (veiculo) references veiculo(n_veic)
);
create table accident(
n_acid bigint not null,
pess_segura bigint not null,
veic_seguro bigint not null,
data date not null,
local varchar(255) not null,
descr text not null,
primary key(n_acid),
unique(n_acid, veic_seguro),
foreign key (pess_segura,veic_seguro) references ensurance(ensured, veiculo)
);
create table acid_pess(
accident bigint not null,
person bigint not null,
veiculo bigint not null,
descr text not null,
primary key(accident, person),
foreign key (accident) references accident(n_acid),
foreign key (person) references person(n_pess),
foreign key (accident, veiculo) references accident(n_acid, veic_seguro)
);
I want to select the n_acid(number of the accident) that had the most people envolved.

Why do my CREATE TABLES work alone but not together? [duplicate]

This question already has answers here:
How do I execute multiple SQL Statements in Access' Query Editor?
(6 answers)
Closed 4 years ago.
I am trying to run a script with multiple CREATE TABLE statements in MS Access.
When I try to run the script all together I get a CREATE TABLE syntax error on the 2nd CREATE TABLE, but when I run each CREATE TABLE on its own through a SQL validator they pass successfully.
What am I doing wrong by lumping all the CREATE TABLEs into one script?
CREATE TABLE MODEL (
ModelNum INT NOT NULL,
Capacity INT NOT NULL,
MaxRange INT NOT NULL,
CONSTRAINT ModelPK PRIMARY KEY(ModelNum)
);
CREATE TABLE AIRPLANE (
RegNum INT NOT NULL,
ModelNum INT NOT NULL,
CONSTRAINT AirplanePK PRIMARY KEY(RegNum),
CONSTRAINT AirplaneFK FOREIGN KEY(ModelNum) REFERENCES MODEL(ModelNum)
);
CREATE TABLE EMPLOYEE (
SSN INT NOT NULL,
EmpName VARCHAR(100) NOT NULL,
Phone INT NULL,
UnionMemberNum INT NULL,
CONSTRAINT EmployeePK PRIMARY KEY(SSN)
);
CREATE TABLE TECHNICIAN (
SSN INT NOT NULL,
Salary INT NOT NULL,
CONSTRAINT TechnicianPK PRIMARY KEY(SSN),
CONSTRAINT TechnicianFK FOREIGN KEY(SSN) REFERENCES EMPLOYEE(SSN)
);
CREATE TABLE TRAFFICCONTROLLER (
SSN INT NOT NULL,
DateOfTraining DATE NULL,
CONSTRAINT TrafficControllerPK1 PRIMARY KEY(SSN),
CONSTRAINT TrafficControllerFK1 FOREIGN KEY(SSN) REFERENCES EMPLOYEE(SSN)
);
CREATE TABLE EXPERT(
SSN INT NOT NULL,
ModelNum INT NOT NULL,
CONSTRAINT ExpertPK1 PRIMARY KEY(SSN),
CONSTRAINT ExpertFK1 FOREIGN KEY(ModelNum) REFERENCES AIRPLANE(ModelNum)
);
CREATE TABLE TEST(
Test_Num INT NOT NULL,
TestName VARCHAR(100) NOT NULL,
MaxScore INT NOT NULL,
CONSTRAINT TestPK PRIMARY KEY(Test_Num)
);
CREATE TABLE TESTEVENTS(
RegNum INT NOT NULL,
Test_Num INT NOT NULL,
SSN INT NOT NULL,
TestDate DATE NOT NULL,
Score INT NULL,
CONSTRAINT TestEventsPK1 PRIMARY KEY(RegNum),
CONSTRAINT TestEventsFK1 FOREIGN KEY(RegNum) REFERENCES AIRPLANE(RegNum),
CONSTRAINT TestEventsPK2 PRIMARY KEY(Test_Num),
CONSTRAINT TestEventsFK2 FOREIGN KEY(Test_Num) REFERENCES TEST(Test_Num),
CONSTRAINT TestEventsPK3 PRIMARY KEY(SSN),
CONSTRAINT TestEventsFK3 FOREIGN KEY(SSN) REFERENCES TECHNICIAN(SSN),
CONSTRAINT TestEventsPK4 PRIMARY KEY(TestDate)
);
MS Access supports only one statement at a time.

SQL DB Creation Error

I used BIT DEFAULT 0, as a Boolean dont know if its right like that. Well, the problem is that I cannot create any table because there is a problem in FK, dont get why? I'ev also added ERD Image for better view.
Maybe someone will/can be so nice and explain it to me, ty!
Maybe someone know any good program or online sandbox for turtnind ERD to the MSSQL Code or MySQL code? It would be nice if its free for use. Thanks in advance!
CREATE DataBase LO;
GO
USE LO;
CREATE TABLE Weapon
(
ClassWeapon CHAR(50) NULL,
Level INT NULL,
MinDmg INT NULL,
MaxDmg INT NULL,
AtkSpeed NUMERIC(3,2) NULL,
PerSecondDmg NUMERIC(6,2) NULL,
PlayerLevel INT NULL,
SkillLevel INT NULL,
Image BIT DEFAULT 0 NULL,
Name CHAR(100) NULL,
Weapons CHAR(50) NOT NULL,
Monsters CHAR(50) NULL,
PRIMARY KEY (Weapons),
FOREIGN KEY (Monsters) REFERENCES Monster(Monsters)
);
CREATE TABLE Armor
(
ClassArmor CHAR(50) NULL,
Name CHAR(100) NULL,
Level INT NULL,
Defense INT NULL,
Image BIT DEFAULT 0 NULL,
Armors CHAR(50) NOT NULL,
Monsters CHAR(50) NULL,
PRIMARY KEY (Armors),
FOREIGN KEY (Monsters) REFERENCES Monster(Monsters)
);
CREATE TABLE Menu
(
Weapons CHAR(50) NULL,
Armors CHAR(50) NULL,
Monsters CHAR(50) NULL,
FOREIGN KEY (Weapons) REFERENCES Weapon(Weapons),
FOREIGN KEY (Armors) REFERENCES Armor(Armors),
FOREIGN KEY (Monsters) REFERENCES Monster(Monsters)
);
CREATE TABLE Monster
(
Name CHAR(100) NULL,
Level INT NULL,
MinHealth INT NULL,
MaxHealth INT NULL,
MinDmg INT NULL,
MaxDmg INT NULL,
AtkSpeed NUMERIC(3,2) NULL,
Location CHAR(100) NULL,
Monsters CHAR(50) NOT NULL,
DropItems CHAR(100) NULL,
Image BIT DEFAULT 0 NULL,
PRIMARY KEY (Monsters),
FOREIGN KEY (DropItems) REFERENCES Items(DropItems)
);
CREATE TABLE Items
(
DropItems CHAR(100) NOT NULL,
Weapons CHAR(50) NULL,
Armors CHAR(50) NULL,
PRIMARY KEY (DropItems),
FOREIGN KEY (Weapons) REFERENCES Weapon(Weapons),
FOREIGN KEY (Armors) REFERENCES Armor(Armors)
);
Well, the problem is that I cannot create any table because there is a problem in FK, dont get why?
Simply, you cannot reference object that does not exist.
You should first create tables and then add FK relationships:
CREATE DataBase LO;
GO
USE LO;
CREATE TABLE Weapon
(
ClassWeapon CHAR(50) NULL,
Level INT NULL,
MinDmg INT NULL,
MaxDmg INT NULL,
AtkSpeed NUMERIC(3,2) NULL,
PerSecondDmg NUMERIC(6,2) NULL,
PlayerLevel INT NULL,
SkillLevel INT NULL,
Image BIT DEFAULT 0 NULL,
Name CHAR(100) NULL,
Weapons CHAR(50) NOT NULL,
Monsters CHAR(50) NULL,
PRIMARY KEY (Weapons),
--FOREIGN KEY (Monsters) REFERENCES Monster(Monsters)
);
CREATE TABLE Armor
(
ClassArmor CHAR(50) NULL,
Name CHAR(100) NULL,
Level INT NULL,
Defense INT NULL,
Image BIT DEFAULT 0 NULL,
Armors CHAR(50) NOT NULL,
Monsters CHAR(50) NULL,
PRIMARY KEY (Armors),
---- FOREIGN KEY (Monsters) REFERENCES Monster(Monsters)
);
CREATE TABLE Menu
(
Weapons CHAR(50) NULL,
Armors CHAR(50) NULL,
Monsters CHAR(50) NULL,
--FOREIGN KEY (Weapons) REFERENCES Weapon(Weapons),
-- FOREIGN KEY (Armors) REFERENCES Armor(Armors),
-- FOREIGN KEY (Monsters) REFERENCES Monster(Monsters)
);
CREATE TABLE Monster
(
Name CHAR(100) NULL,
Level INT NULL,
MinHealth INT NULL,
MaxHealth INT NULL,
MinDmg INT NULL,
MaxDmg INT NULL,
AtkSpeed NUMERIC(3,2) NULL,
Location CHAR(100) NULL,
Monsters CHAR(50) NOT NULL,
DropItems CHAR(100) NULL,
Image BIT DEFAULT 0 NULL,
PRIMARY KEY (Monsters),
--FOREIGN KEY (DropItems) REFERENCES Items(DropItems)
);
CREATE TABLE Items
(
DropItems CHAR(100) NOT NULL,
Weapons CHAR(50) NULL,
Armors CHAR(50) NULL,
PRIMARY KEY (DropItems),
--FOREIGN KEY (Weapons) REFERENCES Weapon(Weapons),
-- FOREIGN KEY (Armors) REFERENCES Armor(Armors)
);
ALTER TABLE Weapon
ADD FOREIGN KEY (Monsters) REFERENCES Monster(Monsters);
ALTER TABLE Armor
ADD FOREIGN KEY (Monsters) REFERENCES Monster(Monsters);
ALTER TABLE Menu
ADD
FOREIGN KEY (Weapons) REFERENCES Weapon(Weapons),
FOREIGN KEY (Armors) REFERENCES Armor(Armors),
FOREIGN KEY (Monsters) REFERENCES Monster(Monsters);
ALTER TABLE Monster
ADD FOREIGN KEY (DropItems) REFERENCES Items(DropItems);
ALTER TABLE Items
ADD FOREIGN KEY (Weapons) REFERENCES Weapon(Weapons),
FOREIGN KEY (Armors) REFERENCES Armor(Armors);
Thanks #lad2025
Here how it worked by adding Tables and then alter the FK.
CREATE DataBase LO;
GO
USE LO;
CREATE TABLE Weapon
(
ClassWeapon CHAR(50) NULL,
Level INT NULL,
MinDmg INT NULL,
MaxDmg INT NULL,
AtkSpeed NUMERIC(3,2) NULL,
PerSecondDmg NUMERIC(6,2) NULL,
PlayerLevel INT NULL,
SkillLevel INT NULL,
Image BIT DEFAULT 0 NULL,
Name CHAR(100) NULL,
Weapons CHAR(50) NOT NULL,
Monsters CHAR(50) NULL,
PRIMARY KEY (Weapons)
);
ALTER TABLE Weapon ADD FOREIGN KEY (Monsters) REFERENCES Monster(Monsters);
CREATE TABLE Armor
(
ClassArmor CHAR(50) NULL,
Name CHAR(100) NULL,
Level INT NULL,
Defense INT NULL,
Image BIT DEFAULT 0 NULL,
Armors CHAR(50) NOT NULL,
Monsters CHAR(50) NULL,
PRIMARY KEY (Armors)
);
ALTER TABLE Armor ADD FOREIGN KEY (Monsters) REFERENCES Monster(Monsters);
CREATE TABLE Menu
(
Weapons CHAR(50) NULL,
Armors CHAR(50) NULL,
Monsters CHAR(50) NULL
);
ALTER TABLE Menu ADD FOREIGN KEY (Weapons) REFERENCES Weapon(Weapons);
ALTER TABLE Menu ADD FOREIGN KEY (Armors) REFERENCES Armor(Armors);
ALTER TABLE Menu ADD FOREIGN KEY (Monsters) REFERENCES Monster(Monsters);
CREATE TABLE Monster
(
Name CHAR(100) NULL,
Level INT NULL,
MinHealth INT NULL,
MaxHealth INT NULL,
MinDmg INT NULL,
MaxDmg INT NULL,
AtkSpeed NUMERIC(3,2) NULL,
Location CHAR(100) NULL,
Monsters CHAR(50) NOT NULL,
DropItems CHAR(100) NULL,
Image BIT DEFAULT 0 NULL,
PRIMARY KEY (Monsters)
);
ALTER TABLE Monster ADD FOREIGN KEY (DropItems) REFERENCES Items(DropItems);
CREATE TABLE Items
(
DropItems CHAR(100) NOT NULL,
Weapons CHAR(50) NULL,
Armors CHAR(50) NULL,
PRIMARY KEY (DropItems)
);
ALTER TABLE Items ADD FOREIGN KEY (Weapons) REFERENCES Weapon(Weapons);
ALTER TABLE Items ADD FOREIGN KEY (Armors) REFERENCES Armor(Armors);

Run sql command line, issue. Creating tables

I'm trying to create a table in run sql command line of oracle database 11 xe.
My problem is when I finish typing my code:
create table vigilantes(
idVigilantes integer(3) not null,
nombre varchar(100) not null,
paterno varchar(100) not null,
materno varchar(100) not null,
id_caseta integer(3) null,
id_turno integer(3) null,
edad integer(3) not null,
id_genero integer(1) not null,
idEmpresa integer(3) not null,
constraint pk_idVigilantes PRIMARY KEY (idVigilantes)
constraint fk_id_caseta FOREIGN KEY (id_caseta)
references Caseta(id_caseta)
constraint fk_id_turno FOREIGN KEY(id_turno)
references Turno(id_turno)
constraint fk_id_genero FOREIGN KEY(id_genero)
references Generos(id_genero)
constraint fk_idEmpresa FOREIGN KEY(idEmpresa)
references Empresa(idEmpresa)
);
I get "ORA-00907: missing right parenthesis" issue.
I read that this is often caused by not defining a value.
e.g:
create table vigilantes(
idVigilantes integer not null,
.......
But still no solution here.
Any help or clue will be a lot of help.
You are missing comma after constraints. Also, integer has no precision.
create table vigilantes (
idVigilantes integer not null,
nombre varchar(100) not null,
paterno varchar(100) not null,
materno varchar(100) not null,
id_caseta integer null,
id_turno integer null,
edad integer not null,
id_genero integer not null,
idEmpresa integer not null,
constraint pk_idVigilantes primary key (idVigilantes),
constraint fk_id_caseta foreign key (id_caseta) references Caseta(id_caseta),
constraint fk_id_turno foreign key (id_turno) references Turno(id_turno),
constraint fk_id_genero foreign key (id_genero) references Generos(id_genero),
constraint fk_idEmpresa foreign key (idEmpresa) references Empresa(idEmpresa)
);
If you must define precision, use number datatype:
create table vigilantes (
idVigilantes number(3, 0) not null,
nombre varchar(100) not null,
paterno varchar(100) not null,
materno varchar(100) not null,
id_caseta number(3, 0) null,
id_turno number(3, 0) null,
edad number(3, 0) not null,
id_genero number(1, 0) not null,
idEmpresa number(3, 0) not null,
constraint pk_idVigilantes primary key (idVigilantes),
constraint fk_id_caseta foreign key (id_caseta) references Caseta(id_caseta),
constraint fk_id_turno foreign key (id_turno) references Turno(id_turno),
constraint fk_id_genero foreign key (id_genero) references Generos(id_genero),
constraint fk_idEmpresa foreign key (idEmpresa) references Empresa(idEmpresa)
);