Trying to do an assignment but stuck at ORA-00906: missing left parenthesis - sql

Im doing a course on DB, this is one of the first assignments, I'm about to be done, but there is an issue somewhere in my REL_CURSOS_ALUMNOS query which ends up with the ORA-00906 error.
REL_CURSOS_ALUMNOS is a relationship table for "one to many" 1 ' CURSOS(Id_Curso) to many ALUMNOS(NIF_Alumno).
This is my query till now:
CREATE TABLE PROFESORES(
NIF_Profesor VARCHAR2(15) CONSTRAINT Prof_NIF_PK PRIMARY KEY,
Nombre VARCHAR2(30),
Apellido1 VARCHAR2(30),
Apellido2 VARCHAR2(30),
Direccion VARCHAR2(4000),
Titulacion VARCHAR2(500),
Salario FLOAT(10) CONSTRAINT Prof_sal_NN NOT NULL
);
CREATE TABLE ALUMNOS(
NIF_Alumno VARCHAR2(15) CONSTRAINT Alum_NIF_PK PRIMARY KEY,
Nombre VARCHAR2(30),
Apellido1 VARCHAR2(30),
Apellido2 VARCHAR2(30),
Direccion VARCHAR2(4000),
Sexo CHAR(1),
Fecha_Nacimiento DATE
);
CREATE TABLE CURSOS (
Id_Curso VARCHAR2(15) CONSTRAINT Curs_Id_PK PRIMARY KEY,
Nombre VARCHAR2(400) UNIQUE,
NIF_Profesor VARCHAR2(15) REFERENCES PROFESORES (NIF_Profesor),
Max_Alumnos NUMBER(5),
Inicio_Fecha DATE,
Final_Fecha DATE,
Num_Horas NUMBER(10) NOT NULL,
CONSTRAINT Curs_FechasIncorrectas CHECK (Final_Fecha > Inicio_Fecha));
CREATE TABLE REL_CURSOS_ALUMNOS (
Id_Curso VARCHAR2(15) REFERENCES CURSOS(Id_Cursos),
NIF_Alumno VARCHAR2(15) REFERENCES ALUMNOS(NIF_Alumno) CONSTRAINT RCA_NIFAlum_UQ UNIQUE,
UNIQUE KEY (Id_Curso, NIF_Alumno));
Thank you.

You have a couple of small errors in the final table definition:
CREATE TABLE REL_CURSOS_ALUMNOS (
Id_Curso VARCHAR2(15) REFERENCES CURSOS(Id_Curso),
NIF_Alumno VARCHAR2(15) REFERENCES ALUMNOS(NIF_Alumno) CONSTRAINT RCA_NIFAlum_UQ UNIQUE,
UNIQUE (Id_Curso, NIF_Alumno)
);
Your specific problem is caused by KEY. That is not needed for a unique constraint definition. Further, the reference for CURSOS is id_curso, not id_cursos.
I left both unique definitions. But if NIF_Alumno is unique, then the pair (Id_Curso, NIF_Alumno) is also unique, so that constraint is redundant.

Related

Is it possible to create to tables that reference each other without using ALTER? [duplicate]

This question already has an answer here:
Is there any other way to create constraints during SQL table creation?
(1 answer)
Closed 2 years ago.
I am new to databases in general and would like to have an answer to a problem that I am facing.
I would like to know if there is away two create two tables that reference each other without creating one and Altering it later and adding the reference.
CREATE TABLE Cats(
name VARCHAR2(15) CONSTRAINT cat_name_nn NOT NULL,
gender VARCHAR2(1) CONSTRAINT cat_gd CHECK(gender IN('W', 'M')),
nickname VARCHAR2(15) CONSTRAINT cat_pk PRIMARY KEY,
function VARCHAR2(10) CONSTRAINT cat_fn REFERENCES Functions(function),
chief VARCHAR2(15) CONSTRAINT cat_chf REFERENCES Cats(nickname),
in_herd_sinnce DATE DEFAULT SYSDATE,
mice_ration NUMBER(3),
mice_extra NUMBER(3),
band_no NUMBER(2) CONSTRAINT cat_bno REFERENCES Bands(band_no))
CREATE TABLE Bands(
band_no NUMBER(2) CONSTRAINT bd_pk PRIMARY KEY,
name VARCHAR2(20) CONSTRAINT bd_name_nn NOT NULL,
site VARCHAR2(15) CONSTRAINT bd_site_un UNIQUE,
band_chief VARCHAR2(15) CONSTRAINT bd_chf_un UNIQUE
CONSTRAINT bd_chf_nm REFERENCES Cats(nickname)
);
As far as I remember I can not do it; I am right?
No, it cannot be done. Whichever table you CREATE first cannot have a foreign key to the other table as that other table does not exist yet; instead you need to create the tables and then use ALTER TABLE ADD CONSTRAINT to create the constraint from the first table to the second.
For example:
CREATE TABLE Functions ( function VARCHAR2(10) CONSTRAINT fn_fn PRIMARY KEY );
CREATE TABLE Cats(
name VARCHAR2(15) CONSTRAINT cat_name_nn NOT NULL,
gender VARCHAR2(1) CONSTRAINT cat_gd CHECK(gender IN('W', 'M')),
nickname VARCHAR2(15) CONSTRAINT cat_pk PRIMARY KEY,
function VARCHAR2(10) CONSTRAINT cat_fn REFERENCES Functions(function),
chief VARCHAR2(15) CONSTRAINT cat_chf REFERENCES Cats(nickname),
in_herd_sinnce DATE DEFAULT SYSDATE,
mice_ration NUMBER(3),
mice_extra NUMBER(3),
band_no NUMBER(2)
);
CREATE TABLE Bands(
band_no NUMBER(2) CONSTRAINT bd_pk PRIMARY KEY,
name VARCHAR2(20) CONSTRAINT bd_name_nn NOT NULL,
site VARCHAR2(15) CONSTRAINT bd_site_un UNIQUE,
band_chief VARCHAR2(15) CONSTRAINT bd_chf_un UNIQUE
CONSTRAINT bd_chf_nm REFERENCES Cats(nickname)
);
ALTER TABLE CATS ADD CONSTRAINT cat_bno FOREIGN KEY ( band_no )
REFERENCES Bands(band_no);
db<>fiddle here

Cant create tables, sql error ORA-02270

Yeah, not too sure on this one. New to sql and I thought everything was done right, any ideas? If this is not the way to add foreign keys, could someone please explain to me how the correct way to do it is please? Would appreciate it, thanks.
CREATE TABLE customer (
reference NUMBER(5) PRIMARY KEY,
company_name VARCHAR2(30),
address VARCHAR2(30),
post_code VARCHAR2(10),
telephone VARCHAR(20),
contact_fname VARCHAR2(20),
contact_sname VARCHAR2(20),
contact_email VARCHAR2(30)
);
CREATE TABLE manifest (
barcode NUMBER(10) PRIMARY KEY,
trip_id NUMBER(10),
pickup_customer_ref VARCHAR2(30),
delivery_customer_ref VARCHAR2(30),
category NUMBER(1),
weight NUMBER(10)
);
CREATE TABLE category (
category NUMBER(1) PRIMARY KEY,
description VARCHAR2(15),
requirements VARCHAR2(30),
FOREIGN KEY (category) REFERENCES manifest(category)
);
CREATE TABLE trip (
trip_id NUMBER(10) PRIMARY KEY,
departure_date DATE,
return_date DATE,
vehicle_id VARCHAR2(10),
employee_no NUMBER(10),
FOREIGN KEY (trip_id) REFERENCES manifest(trip_id)
);
CREATE TABLE vehicle (
registration VARCHAR2(10) PRIMARY KEY,
vehicle_type_id VARCHAR2(10),
model VARCHAR2(15),
make VARCHAR2(15),
body VARCHAR2(15),
year NUMBER(4),
FOREIGN KEY (registration) REFERENCES trip(registration)
);
CREATE TABLE model (
vehicle_type_id VARCHAR(10) PRIMARY KEY,
make VARCHAR2(15),
model VARCHAR2(15),
FOREIGN KEY (vehicle_type_id) REFERENCES vehicle(vehicle_type_id)
);
CREATE TABLE driver (
employee_no NUMBER(10) PRIMARY KEY,
first_name VARCHAR2(20),
last_name VARCHAR(20),
ni_no VARCHAR2(15),
telephone VARCHAR2(20),
mobile VARCHAR2(12),
hazardous_goods VARCHAR2(1),
FOREIGN KEY (employee_no) REFERENCES trip(employee_no)
);
and the error message I get is
SQL Error: ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
I get this error for every table after manifest btw
The error message is pretty clear. The reference for a foreign key needs to be a unique or primary key in the other table. So this is wrong in the category table.
FOREIGN KEY (category) REFERENCES manifest(category)
I assume you intend for the manifest to look like this:
CREATE TABLE manifest (
barcode NUMBER(10) PRIMARY KEY,
trip_id NUMBER(10) REFERENCES trip(trip_id),
pickup_customer_ref VARCHAR2(30),
delivery_customer_ref VARCHAR2(30),
category NUMBER(1) REFERENCES category(category),
weight NUMBER(10)
);
And so on for the other places where foreign keys are used. (This uses a short-hand notation for the foreign key reference; a separate clause in CREATE TABLE is also fine.)
Of course, the definition of manifest has to go after category and trip, in this example.
In other words, the reference goes in the other table, not where the primary key is defined.
Also, I would recommend being consistent with the names of the foreign keys. At the very least, they should generally have the same name as the primary key, where possible.
trip_id, vehicle_type_id, and employee_no all need to be marked as UNIQUE.

I am trying to create a table but its not getting created. The issue is with constraints. Mostly NOT NULL and FOREIGN KEY

CREATE TABLE Customer(
Cust_Id Number(4),
Cust_First_Name VarChar2(20),
Cust_Mid_Name VarChar2(20),
Cust_Last_Name VarChar2(20),
Account_Type Varchar2(15),
Cust_Email VarChar2(30),
Cust_Mobile Number(10),
Bank_Id NUMBER(4),
Account_No Number(4),
CONSTRAINT Cust_Id_Pri_Key PRIMARY KEY (Cust_Id),
CONSTRAINT Cust_Last_Name_Not_Null NOT NULL (Cust_Last_Name),
CONSTRAINT Cust_First_Name_Not_Null NOT NULL (Cust_First_Name),
CONSTRAINT Cust_Email_Unq UNIQUE(Cust_Email),
CONSTRAINT Cust_Mobile_Unq UNIQUE(Cust_Mobile),
CONSTRAINT Bank_Id_For_Key FOREIGN KEY REFERENCES bank(bankId)
);
Working SQL Fiddle: http://sqlfiddle.com/#!4/05311
Here's how your SQL should look like.
CREATE TABLE Customer(
Cust_Id Number(4) NOT NULL,
Cust_First_Name VarChar2(20) NOT NULL,
Cust_Mid_Name VarChar2(20),
Cust_Last_Name VarChar2(20) NOT NULL,
Account_Type Varchar2(15),
Cust_Email VarChar2(30),
Cust_Mobile Number(10),
Bank_Id NUMBER(4),
Account_No Number(4),
CONSTRAINT Cust_Id_Pri_Key PRIMARY KEY (Cust_Id),
CONSTRAINT Cust_Email_Unq UNIQUE(Cust_Email),
CONSTRAINT Cust_Mobile_Unq UNIQUE(Cust_Mobile),
CONSTRAINT Bank_Id_For_Key FOREIGN KEY (Bank_Id) REFERENCES bank(bankId)
);
Almost all your errors are from the Syntax.
NOT NULL is specified in the Column name itself, not as a separate constraint.
FOREIGN KEY CONSTRAINT requires you to specify the source column name, and the FOREIGN KEYcolumn in the table that it is defined as aPRIMARY KEY`
You have two problems: you can't specify NOT NULL as an out-of-line constraint and you are missing the column name in the foreign key constraint. The following should work:
CREATE TABLE customer
(
cust_id NUMBER (4),
cust_first_name VARCHAR2 (20) CONSTRAINT cust_first_name_not_null NOT NULL,
cust_mid_name VARCHAR2 (20),
cust_last_name VARCHAR2 (20) CONSTRAINT cust_last_name_not_null NOT NULL,
account_type VARCHAR2 (15),
cust_email VARCHAR2 (30),
cust_mobile NUMBER (10),
bank_id NUMBER (4),
account_no NUMBER (4),
CONSTRAINT cust_id_pri_key PRIMARY KEY (cust_id),
CONSTRAINT cust_email_unq UNIQUE (cust_email),
CONSTRAINT cust_mobile_unq UNIQUE (cust_mobile),
CONSTRAINT bank_id_for_key FOREIGN KEY (bank_id) REFERENCES bank (bankid)
);
However, I wouldn't bother with naming the NOT NULL constraints. Naming them doesn't really add very much manageability, as they can be updated by altering the table without knowing the name.

foreign key Oracle

I'm working with Oracle Database and I have this script :
create table Guardians (
noCage number(3),
nomE varchar2(20),
CONSTRAINT nc_ne1 PRIMARY KEY(noCage, nomE)
);
create table Animals(
nomA varchar2(20) PRIMARY KEY,
type varchar2(15) NOT NULL,
country varchar2(20) NOT NULL,
noCage number(3),
CONSTRAINT fk_anim_cage0 FOREIGN KEY(noCage) REFERENCES Guardians(noCage)
);
Upon executing the script, the table guardians is created, an error is prompted and the table Animals is not created. I did some manipulations and i think that it's got to do with the
CONSTRAINT nc_ne1 PRIMARY KEY(noCage, nomE)
Since Guardians Table has composite primary key you need to include both the columns in foreign key.
CONSTRAINT fk_anim_cage0 FOREIGN KEY(noCage,nomA) REFERENCES Guardians(noCage, nomE)
Your Animals table would be like this
create table Animals(
nomA varchar2(20) PRIMARY KEY,
type varchar2(15) NOT NULL,
country varchar2(20) NOT NULL,
noCage number(3),
CONSTRAINT fk_anim_cage0 FOREIGN KEY(noCage,nomA)
REFERENCES Guardians(noCage, nomE)
);

Oracle table error: table creating with constraints

create table prmr(
emp_id number(10) primary key,
name varchar2(10),
mob number(10) unique key,
id varchar2(10),
email varchar2(20)
);
When I create this table, it gives me the error "Missing Parenthesis". Any help?
Your syntax is wrong. Remove key from unique key
CREATE TABLE prmr(
emp_id number(10) PRIMARY KEY,
name varchar2(10),
mob number(10) UNIQUE,
id varchar2(10),
email varchar2(20)
);
There is also a way to specify constraints separately:
CREATE TABLE prmr(
emp_id number(10) NOT NULL,
name varchar2(10),
mob number(10),
id varchar2(10),
email varchar2(20),
CONSTRAINT mob_unique UNIQUE (mob),
CONSTRAINT emp_id_pk PRIMARY KEY (emp_id)
);
I also added NOT NULL to the emp_id (Although you don't need to specify it explicitly as far as emp_id is PK, that could, probably, help avoid the confusion anyway).
Try this :
create table prmr(
emp_id number(10),
name varchar2(10),
mob number(10),
id varchar2(10),
email varchar2(20),
CONSTRAINT PK_table PRIMARY KEY (emp_id),
CONSTRAINT unique_mob UNIQUE(mob)
);