SQL Server : error creating two foreign keys on one table - sql

create table TiposPeliculas
(
id_tipoPelicula int identity (1, 1),
descipcion varchar (30)
constraint pk_tipoPelicula primary key(id_tipoPelicula)
)
create table Generos
(
id_genero int identity (1,1),
descripcion varchar(20)
constraint pk_generos primary key(id_genero)
)
create table Peliculas
(
id_pelicula int not null,
id_tipoPelicula int,
id_genero int,
titulo varchar (30),
duracion time,
idioma varchar (30),
sub bit
constraint pk_pelicula primary key (id_pelicula, id_tipoPelicula)
constraint fk_generoPelicula foreign key (id_genero) references Generos (id_genero),
constraint fk_tipoPeliculasDePeli foreign key (id_tipoPelicula) references TipoPeliculas (id_tipoPelicula)
)
The FOREIGN KEY for id_tipoPelicula throws an error:
foreign key "fk_tipoPeliculasDePeli" references invalid table "TipoPeliculas"

create table Peliculas
(
id_pelicula int not null,
id_tipoPelicula int,
id_genero int,
titulo varchar (30),
duracion time,
idioma varchar (30),
sub bit
constraint pk_pelicula primary key (id_pelicula, id_tipoPelicula)
constraint fk_generoPelicula foreign key (id_genero) references Generos (id_genero),
constraint fk_tiposPeliculasDePeli foreign key (id_tiposPelicula) references TiposPeliculas (id_tiposPelicula)
)
This will work the error in spelling mistake in relation name TiposPeliculas.

The error:
foreign key "fk_tipoPeliculasDePeli" references invalid table "TipoPeliculas"
It says, the foreign key is reference to invalid Table "TipoPeliculas", Which means table "TipoPeliculas" doesn't exists.
In your question the table name is "TiposPeliculas". Hence it seems you need to correct table name in reference.
Also the column name should be "id_tipoPelicula" instead of "id_tiposPelicula" in reference.
Run the below code
create table TiposPeliculas
(
id_tipoPelicula int identity (1, 1),
descipcion varchar (30)
constraint pk_tipoPelicula primary key(id_tipoPelicula)
)
create table Generos
(
id_genero int identity (1,1),
descripcion varchar(20)
constraint pk_generos primary key(id_genero)
)
create table Peliculas
(
id_pelicula int not null,
id_tipoPelicula int,
id_genero int,
titulo varchar (30),
duracion time,
idioma varchar (30),
sub bit
constraint pk_pelicula primary key (id_pelicula, id_tipoPelicula)
constraint fk_generoPelicula foreign key (id_genero) references Generos (id_genero),
constraint fk_tipoPeliculasDePeli foreign key (id_tipoPelicula) references TiposPeliculas (id_tipoPelicula)
)

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.

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

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/

Table 'tableName' contains a constraint definition with column 'columnName' which is not in the table Java Derby

I am trying to run a SQL script to my database created on Java Derby:
CREATE TABLE USUARIO (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY,
EMAIL VARCHAR(40) NOT NULL UNIQUE,
NOMBRES VARCHAR(20) NOT NULL,
APELLIDOS VARCHAR(20) NOT NULL,
CONTRASEÑA VARCHAR(20) NOT NULL,
CEDULA INTEGER,
TELEFONO INTEGER,
CONSTRAINT ID_USUARIO_PK PRIMARY KEY (ID) -- Primary Key
);
CREATE TABLE ORGANIZACION (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY, -- Id autonumérico
NOMBRE VARCHAR(20) NOT NULL,
CONSTRAINT ID_ORGANIZACION_PK PRIMARY KEY (ID),
CONSTRAINT ID_DIRECCION_ORG_FK FOREIGN KEY (DIRECCION_ORG) REFERENCES DIRECCION_ORG (ID),
CONSTRAINT ID_TELEFONO_ORG_FK FOREIGN KEY (TELEFONO_ORG) REFERENCES TELEFONO_ORG (ID)
);
CREATE TABLE TELEFONO_ORG (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY, -- Id autonumérico
TELEFONO INTEGER NOT NULL,
CONSTRAINT ID_TELEFONO_ORG_PK PRIMARY KEY (ID)
);
CREATE TABLE DIRECCION_ORG (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY, -- Id autonumérico
DIRECCION VARCHAR(300) NOT NULL,
CONSTRAINT ID_DIRECCION_ORG_PK PRIMARY KEY (ID)
);
CREATE TABLE PRODUCTO (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY,
RUTA_IMAGEN VARCHAR(400) NOT NULL,
NOMBRE VARCHAR(20) NOT NULL,
CANTIDAD INTEGER,
PRECIO INTEGER,
COSTO INTEGER,
CONSTRAINT ID_PRODUCTO_PK PRIMARY KEY (ID), -- Primary Key
CONSTRAINT ID_ORGANIZACION_FK FOREIGN KEY (ORGANIZACION) REFERENCES ORGANIZACION (ID)
);
But I am getting this error:
Table ORGANIZACION contains a constraint definition with column
DIRECCION_ORG which is not in the table.
What can be wrong here?
You are making constraints to foreign keys but haven't actually created the columns to contain those keys yet. Make those columns and make the constraints point to them (instead of just using the table's name again). Shown below with -- comments indicating where to add it.
CREATE TABLE USUARIO (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY,
EMAIL VARCHAR(40) NOT NULL UNIQUE,
NOMBRES VARCHAR(20) NOT NULL,
APELLIDOS VARCHAR(20) NOT NULL,
CONTRASEÑA VARCHAR(20) NOT NULL,
CEDULA INTEGER,
TELEFONO INTEGER,
CONSTRAINT ID_USUARIO_PK PRIMARY KEY (ID) -- Primary Key
);
CREATE TABLE ORGANIZACION (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY, -- Id autonumérico
NOMBRE VARCHAR(20) NOT NULL,
DIRECCION_ORG_ID INTEGER NOT NULL, -- ADD THIS and change constraint FK name
TELEFONO_ORG_ID INTEGER NOT NULL, -- ADD THIS and change constraint FK name
CONSTRAINT ID_ORGANIZACION_PK PRIMARY KEY (ID),
CONSTRAINT ID_DIRECCION_ORG_FK FOREIGN KEY (DIRECCION_ORG_ID) REFERENCES DIRECCION_ORG (ID),
CONSTRAINT ID_TELEFONO_ORG_FK FOREIGN KEY (TELEFONO_ORG_ID) REFERENCES TELEFONO_ORG (ID)
);
CREATE TABLE TELEFONO_ORG (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY, -- Id autonumérico
TELEFONO INTEGER NOT NULL,
CONSTRAINT ID_TELEFONO_ORG_PK PRIMARY KEY (ID)
);
CREATE TABLE DIRECCION_ORG (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY, -- Id autonumérico
DIRECCION VARCHAR(300) NOT NULL,
CONSTRAINT ID_DIRECCION_ORG_PK PRIMARY KEY (ID)
);
CREATE TABLE PRODUCTO (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY,
RUTA_IMAGEN VARCHAR(400) NOT NULL,
NOMBRE VARCHAR(20) NOT NULL,
CANTIDAD INTEGER,
PRECIO INTEGER,
COSTO INTEGER,
ORGANIZACION_ID INTEGER NOT NULL, --ADD THIS and change constraint FK name
CONSTRAINT ID_PRODUCTO_PK PRIMARY KEY (ID), -- Primary Key
CONSTRAINT ID_ORGANIZACION_FK FOREIGN KEY (ORGANIZACION_ID) REFERENCES ORGANIZACION (ID)
);
Both the columns DIRECCION_ORG and TELEFONO_ORG are not defined in the table ORGANIZACION
The same problem occurs in the table PRODUCTO where the column ORGANIZACION is not defined
You have an error in your CONSTRAINT clause.
The format of the FOREIGN KEY constraint is:
CONSTRAINT {constraint name} FOREIGN KEY ([This_table_column],...) REFERENCES [Other_table] ([Other_table_column], ...).
Read this: https://db.apache.org/derby/docs/10.1/ref/rrefsqlj13590.html#rrefsqlj13590

"unknown command" shown when creating a foreign key constraint

The following script produces an error:
CREATE TABLE BOTTLE
(
Bottle_ID VARCHAR(30) NOT NULL,
BottleSize NUMBER(10,0) ,
Shape VARCHAR(30) ,
GlassColor VARCHAR(30) ,
BottleInventory NUMBER(10,0) ,
CostPerBottle NUMBER(5,2) ,
constraint BOTTLE_PK PRIMARY KEY (Bottle_ID)
);
create table BOTTLE_VENDOR
(
Vendor_ID VARCHAR(30) not null,
VendorName VARCHAR(20),
Vendor_Straddr1 VARCHAR(100),
Vendor_Straddr2 VARCHAR(100),
Vendor_City VARCHAR(20),
Vendor_State VARCHAR(20),
Vendor_Zip CHAR(5),
Vendor_Phone VARCHAR(20),
PrincipalContact VARCHAR(30),
constraint pk_BOTTLE_VENDOR primary key (Vendor_ID)
);
create table BOTTLE_ORDER
(
Bottle_Order_ID VARCHAR(30) not null,
BottleOrderDate DATE,
Vendor_ID VARCHAR(30),
constraint pk_BOTTLE_ORDER primary key (Bottle_Order_ID),
constraint fk_BOTTLE_ORDER foreign key (Vendor_ID) references BOTTLE_VENDOR(Vendor_ID)
);
create table SHIPMENT
(
Shipment_ID VARCHAR(30) not null,
ShipmentDate DATE,
ReceivedQuantity INTEGER,
ReceivedDate DATE,
Bottle_Order_ID VARCHAR(30),
constraint pk_SHIPMENT primary key (Shipment_ID),
constraint fk_SHIPMENT foreign key (Bottle_Order_ID) references BOTTLE_ORDER(Bottle_Order_ID)
);
create table BOTTLE_ORDER_LINE
(
Bottle_Order_ID VARCHAR(30) not null,
Bottle_ID VARCHAR(30) not null,
BottleOrderQuantity INTEGER,
ActualPricePerBottle NUMBER(5,2),
constraint pk_BOTTLE_ORDER_LINE primary key (Bottle_Order_ID,Bottle_ID),
constraint fk_BOTTLE_ORDER_LINE foreign key (Bottle_Order_ID) references BOTTLE_ORDER(Bottle_Order_ID),
constraint fk_BOTTLE_ORDER_LINE2 foreign key (Bottle_Order_ID) references BOTTLE(Bottle_ID)
);
Here is what exactly shown:
Error starting at line : 37 in command - constraint fk_SHIPMENT foreign key (Bottle_Order_ID) references BOTTLE_ORDER(Bottle_Order_ID)
Error report -
Unknown Command
The error drives me crazy and don't know how to deal with it.
The following line (the last line you've posted) does not match your table definitions:
constraint fk_BOTTLE_ORDER_LINE2 foreign key (Bottle_Order_ID) references BOTTLE(Bottle_ID)
it should read:
constraint fk_BOTTLE_ORDER_LINE2 foreign key (Bottle_ID) references BOTTLE(Bottle_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