CREATE TABLE MEDICO (
DNIMedico CHAR(9) NOT NULL,
NumeroColegiado VARCHAR(200) NOT NULL,
Nombre VARCHAR(200) NOT NULL,
FechaNacimiento DATE NOT NULL,
CONSTRAINT pk_MEDICO PRIMARY KEY (DNIMedico)
)
CREATE TABLE PACIENTE (
DNIPaciente CHAR(9) NOT NULL,
Nombre VARCHAR(200) NOT NULL,
Direccion VARCHAR (200) NOT NULL,
Edad INT NOT NULL,
Peso FLOAT NOT NULL,
Altura FLOAT NOT NULL,
CONSTRAINT pk_PACIENTE PRIMARY KEY (DNIPaciente)
)
CREATE TABLE MEDICO_PACIENTE (
CONSTRAINT fkMP_MEDICO FOREIGN KEY (DNIMedico) REFERENCES
MEDICO (DNIMedico),
CONSTRAINT fkMP_PACIENTE FOREIGN KEY (DNIPaciente) REFERENCES
PACIENTE (DNIPaciente),
Especialidad VARCHAR(200) NOT NULL
)
Msg 1769, Level 16, State 1, Line 18
Foreign key 'fkMP_MEDICO' references invalid column 'DNIMedico' in referencing table 'MEDICO_PACIENTE'.
Msg 1750, Level 16, State 0, Line 18
Could not create constraint or index. See previous errors.
Declaring a foreign key does not automagically create a column. You need to declare the column first, then the foreign key.
Hence:
CREATE TABLE MEDICO_PACIENTE (
DNIMedico CHAR(9) NOT NULL,
DNIPaciente CHAR(9) NOT NULL,
Especialidad VARCHAR(200) NOT NULL,
CONSTRAINT fkMP_MEDICO FOREIGN KEY (DNIMedico) REFERENCES MEDICO (DNIMedico),
CONSTRAINT fkMP_PACIENTE FOREIGN KEY (DNIPaciente) REFERENCES PACIENTE (DNIPaciente)
);
Note that each refering columns should have the same datatype and length as the column they reference.
It would also be a good idea to declare a primary key for the MEDICO_PACIENTE table. Possibly, you want a compound primary key on (DNIMedico, DNIPaciente ):
CREATE TABLE MEDICO_PACIENTE (
DNIMedico CHAR(9) NOT NULL,
DNIPaciente CHAR(9) NOT NULL,
Especialidad VARCHAR(200) NOT NULL,
CONSTRAINT fkMP_MEDICO FOREIGN KEY (DNIMedico) REFERENCES MEDICO (DNIMedico),
CONSTRAINT fkMP_PACIENTE FOREIGN KEY (DNIPaciente) REFERENCES PACIENTE (DNIPaciente),
CONSTRAINT pk_MEDICO_PACIENTE PRIMARY KEY (DNIMedico, DNIPaciente)
);
Demo on DB Fiddle
Related
CREATE TABLE Loan
(
Customerid Char(9) NOT NULL,
EquipmentCode Char(5) NOT NULL,
StartDate DateTime NOT NULL,
EndDate DateTime NULL,
CONSTRAINT CHK_ID
CHECK (Customerid LIKE '[ST][0-9][0-9][0-9][0-9][0-9][0-9][0-9][A-Z]'),
CONSTRAINT CHK_Date CHECK (EndDate >= StartDate),
CONSTRAINT Loan_PK PRIMARY KEY(Customerid),
CONSTRAINT CUST_FK
FOREIGN KEY(Customerid) REFERENCES CUST(CustomerID)
ON UPDATE CASCADE
ON DELETE CASCADE
);
CREATE TABLE EQUIPMENT
(
EquipmentCode CHAR(5) NOT NULL,
EquipmentName VARCHAR(50) NOT NULL,
Description VARCHAR(255) NULL,
RentalRatePerDay DECIMAL(4,2) NOT NULL,
CONSTRAINT EQP_PK PRIMARY KEY(EquipmentCode),
CONSTRAINT CHK_Rate CHECK (RentalRatePerDay BETWEEN 4 AND 50),
CONSTRAINT LOAN_FK
FOREIGN KEY(EquipmentCode) REFERENCES Loan(EquipmentCode)
ON UPDATE CASCADE
ON DELETE CASCADE
);
I was able to reference earlier on in the above loan.table but for the equipment table it states
Msg 1776, Level 16, State 0, Line 49
There are no primary or candidate keys in the referenced table 'Loan' that match the referencing column list in the foreign key 'LOAN_FK'.
Msg 1750, Level 16, State 1, Line 49
Could not create constraint or index. See previous errors.
Please advise.
When we create a ForeignKey on a dependent table, it MUST refer back to the PrimaryKey (or a Unique Key see: https://stackoverflow.com/a/18435114/1690217) on the principal table.
In your case, EQUIPMENT is the principal end of the relationship, and Loan is the dependent. What this means is that the FK needs to be on the Loan table instead, so you should have this:
CREATE TABLE EQUIPMENT
(
EquipmentCode CHAR(5) NOT NULL,
EquipmentName VARCHAR(50) NOT NULL,
Description VARCHAR(255) NULL,
RentalRatePerDay DECIMAL(4,2) NOT NULL,
CONSTRAINT EQP_PK PRIMARY KEY(EquipmentCode),
CONSTRAINT CHK_Rate CHECK (RentalRatePerDay BETWEEN 4 AND 50),
);
CREATE TABLE Loan
(
Customerid Char(9) NOT NULL,
EquipmentCode Char(5) NOT NULL,
StartDate DateTime NOT NULL,
EndDate DateTime NULL,
CONSTRAINT CHK_ID
CHECK (Customerid LIKE '[ST][0-9][0-9][0-9][0-9][0-9][0-9][0-9][A-Z]'),
CONSTRAINT CHK_Date CHECK (EndDate >= StartDate),
CONSTRAINT Loan_PK PRIMARY KEY(Customerid),
CONSTRAINT CUST_FK
FOREIGN KEY(Customerid) REFERENCES CUST(CustomerID)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT EQUIPMENT_FK
FOREIGN KEY(EquipmentCode) REFERENCES EQUIPMENT(EquipmentCode)
ON UPDATE CASCADE
ON DELETE CASCADE
);
A foreign key references a primary key on another table. You have the FK on the wrong table
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
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.
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)
);
I am trying to create a table on SQL Server which has foreign key constraints.I checked all my parent tables and their primary keys match with my new foreign key constraints. Can someone help me out in resolving this issue. My DDL and error message are as below.
DDL
CREATE TABLE I_IPV_LOB_PROG_PROV_MO_METRIC_TRNS(
AARP_ORG_ID int NOT NULL,
LOB_ID int NOT NULL,
PGM_CAT_ID int NOT NULL,
PGM_ID int NOT NULL,
PROV_ID int NOT NULL,
CAT_OF_MEAS_ID int NOT NULL,
SUBCAT_OF_MEAS_ID int NOT NULL,
MEAS_ID int NOT NULL,
TYPE_OF_METRIC_ID int NOT NULL,
METRIC_VAL_ROLE_ID int NOT NULL,
MO_ID int NOT NULL,
OPER_TXT varchar(5) NULL,
METRIC_VAL decimal(19, 2) NOT NULL,
LST_UPD_DT datetime NOT NULL,
LOAD_DT datetime NULL,
LST_UPD_USERID char(20) NOT NULL,
CONSTRAINT PK79 PRIMARY KEY NONCLUSTERED (TYPE_OF_METRIC_ID, METRIC_VAL_ROLE_ID, AARP_ORG_ID, LOB_ID, PGM_CAT_ID, PGM_ID, PROV_ID, MEAS_ID, MO_ID, CAT_OF_MEAS_ID, SUBCAT_OF_MEAS_ID),
CONSTRAINT RefI_IPV_TYPE_OF_METRIC_TRNS73 FOREIGN KEY (TYPE_OF_METRIC_ID)
REFERENCES I_IPV_TYPE_OF_METRIC_TRNS(TYPE_OF_METRIC_ID),
CONSTRAINT RefI_IPV_METRIC_VAL_ROLE_TRNS75 FOREIGN KEY (METRIC_VAL_ROLE_ID)
REFERENCES I_IPV_METRIC_VAL_ROLE_TRNS(METRIC_VAL_ROLE_ID),
CONSTRAINT RefI_IPV_LOB_PROG_PROV_MEAS_TRNS345 FOREIGN KEY (AARP_ORG_ID,LOB_ID,PGM_CAT_ID,PGM_ID,PROV_ID,MEAS_ID,CAT_OF_MEAS_ID,SUBCAT_OF_MEAS_ID)
REFERENCES I_IPV_LOB_PROG_PROV_MEAS_TRNS(AARP_ORG_ID,LOB_ID,PGM_CAT_ID,PGM_ID,PROV_ID,MEAS_ID,CAT_OF_MEAS_ID,SUB_CAT_OF_MEAS_ID)
)
go
Parent Table DDL
CREATE TABLE I_IPV_LOB_PROG_PROV_MEAS_TRNS(
AARP_ORG_ID int NOT NULL,
LOB_ID int NOT NULL,
PGM_CAT_ID int NOT NULL,
PGM_ID int NOT NULL,
PROV_ID int NOT NULL,
CAT_OF_MEAS_ID int NOT NULL,
SUBCAT_OF_MEAS_ID int NOT NULL,
MEAS_ID int NOT NULL,
LOAD_DT datetime NULL,
LST_UPD_USERID char(20) NOT NULL,
LST_UPD_DT datetime NOT NULL,
CONSTRAINT PK115 PRIMARY KEY NONCLUSTERED (MEAS_ID, PROV_ID, AARP_ORG_ID, LOB_ID, PGM_ID, PGM_CAT_ID, CAT_OF_MEAS_ID, SUBCAT_OF_MEAS_ID),
CONSTRAINT RefI_IPV_LOB_PROG_CAT_PROV_TRNS322 FOREIGN KEY (PROV_ID, AARP_ORG_ID, LOB_ID, PGM_ID, PGM_CAT_ID)
REFERENCES I_IPV_LOB_PROG_CAT_PROV_TRNS(PROV_ID, AARP_ORG_ID, LOB_ID, PGM_ID, PGM_CAT_ID),
CONSTRAINT RefI_IPV_MEAS_CAT_TRNS342 FOREIGN KEY (MEAS_ID, CAT_OF_MEAS_ID, SUBCAT_OF_MEAS_ID)
REFERENCES I_IPV_MEAS_CAT_TRNS(MEAS_ID, CAT_OF_MEAS_ID, SUBCAT_OF_MEAS_ID)
)
go
Error Msg
Msg 1776, Level 16, State 0, Line 1
There are no primary or candidate keys in the referenced table 'I_IPV_LOB_PROG_PROV_MEAS_TRNS' that match the referencing column list in the foreign key 'RefI_IPV_LOB_PROG_PROV_MEAS_TRNS345'.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.
One of the fields in the target table must be its primary key (best) or be very quickly findable via some index using the at least one of nthe fields you've listed. If there are no keys in your list it will have to read the whole table to find the specific row and this is not a good thing to do, so it won't.
Create a unique index on the target table containing some/all of the fields you're constraining and it should be OK.