Error implementing relational model in Oracle - sql

MER
I need help creating this MER in Oracle. Specifically in FOTOS table as I have this code:
CREATE TABLE "FOTOS"
(
"ID_FOTO" INT NOT NULL ENABLE,
"ID_USU" INT NOT NULL ENABLE,
"FECHA" DATE NOT NULL ENABLE,
CONSTRAINT "FOTOS_PK"
PRIMARY KEY ("ID_FOTO") ENABLE,
CONSTRAINT "FOTOS_FK"
FOREIGN KEY ("ID_USU") REFERENCES "USUARIOS" ("ID_USU") ENABLE
)
However I keep getting this error:
ORA-02270: no matching unique or primary key for this column-list
ID_USU is one of two primary keys in USUARIOS
USUARIOS TABLE

ID_USU is one of two primary keys in USUARIOS
No. A table can have just one primary key. It can have more than one unique key, but you can only make one of them primary.
What you mistakenly did instead is create a composite primary key. You want the ID to be unique and the name to be unique, but instead you made the combination of ID and name unique, thus allowing duplicate IDs and duplicate names in the table.
What you have:
create table usuarios
(
id_usu number not null,
nomusu varchar2(50) not null,
...
constraint pk_usuarios primary key (id_usu, nomusu)
);
What you want instead:
create table usuarios
(
id_usu number not null,
nomusu varchar2(50) not null,
...
constraint pk_usuarios primary key (id_usu),
constraint uq_usuarios_nomusu unique (nomusu)
);

Foreign keys have to match the primary/unique key they reference column for column. Since the primary key of "USUARIOS" are columns "ID_USU" and "NOMUSU" you need two columns to add FK from "FOTOS" to "USUARIOS", so just add "NOMUSU" column to "FOTOS" and write:
CREATE TABLE "FOTOS"
(
"ID_FOTO" INT NOT NULL ENABLE,
"ID_USU" INT NOT NULL ENABLE,
"NOMUSU" VARCHAR2(50) NOT NULL ENABLE,
"FECHA" DATE NOT NULL ENABLE,
CONSTRAINT "FOTOS_PK"
PRIMARY KEY ("ID_FOTO") ENABLE,
CONSTRAINT "FOTOS_FK"
FOREIGN KEY ("ID_USU","NOMUSU") REFERENCES "USUARIOS" ("ID_USU","NOMUSU") ENABLE);

Related

Is it possible to find the row using something other than the primaryKey on SQLite?

I want to be able to find the value using either network_id or username.
Yet the following sintax gives the error of more than one primary key (as expected).
CREATE TABLE Player(
network_id TEXT not null,
username varchar2(50) not null,
value INTEGER,
CONSTRAINT player_pk1 PRIMARY KEY (username),
CONSTRAINT player_pk2 PRIMARY KEY (network_id)
);
Is there a way that I could do this in Sqlite?
A primary key has three components to its definition:
NOT NULL
UNIQUE
Only one per table
That is why you cannot have more than one. But you can have any number of NOT NULL UNIQUE columns:
CREATE TABLE Player(
network_id TEXT not null,
username varchar2(50) not null,
value INTEGER,
CONSTRAINT player_pk1 PRIMARY KEY (username),
CONSTRAINT unq_player_network_id UNIQUE (network_id)
);

I'm trying to create a primary key from 2 columns, but it doesn't work well

I'm learning Oracle by myself.
Here's my code:
create table Schedule
(
Schedule_SN number(10) primary key,
ScreeningDate date not null,
Price number(6) not null
);
create table Seat
(
Schedule_SN number(10) REFERENCES Schedule(Schedule_SN),
Seat_SN varchar2(4) not null
);
create table Reservation
(
Reservation_SN number(15) primary key,
DCtype number(2) not null,
DCamount number(7),
PaymentMethod number(1) not null,
TotalPrice number(7) not null,
ReservationDate date not null
);
create table Reservation_details ** I need help here **
(
Reservation_SN number(15) REFERENCES Reservation(Reservation_SN),
Schedule_SN number(10) REFERENCES Schedule(Schedule_SN),
Seat_SN varchar2(10) REFERENCES Seat(Seat_SN),
CONSTRAINT Reservation_detailesPK primary key (Reservation_SN, Schedule_SN)
);
Error messages:
Errors - 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
How can I make my 2 columns (Reservation_SN, Schedule_SN) into a primary key?
The problem is with seat_sn. You want child column in reservation_details to reference parent column in seat, but the parent column is not a primary or unique key. Actually, seat has no primary key; just make seat_sn the primay key of this table (if this fits your use case), and the rest should run fine:
create table seat (
schedule_sn nmber(10) references schedule(schedule_sn),
seat_sn varchar3(4) primary key
)
Demo on DB Fiddle

Cannot create foreign key on oracle

I'm trying to create a foreign key for a table named: Words_in_group, which holds all the words in a froup that the user creates.
I'm trying to refer the words to a table named: Words, which it's P-key is "Word".
Maybe this has something to do with the table "Words", that uses a composite key?
This is the script for creating the foreign key.
alter table "WORDS_IN_GROUP" add constraint
"WORDS_IN_GROUP_FK1" foreign key ("WORD") references "WORDS" ("WORD")
/
Here are the tables SQL:
CREATE TABLE "WORDS_IN_GROUP" ( "GROUP_CODE" NUMBER(9,0) NOT NULL ENABLE,
"WORD" VARCHAR2(45) NOT NULL ENABLE,
CONSTRAINT "WORDS_IN_GROUP_PK" PRIMARY KEY ("GROUP_CODE") ENABLE ) ;ALTER TABLE "WORDS_IN_GROUP" ADD CONSTRAINT "WORDS_IN_GROUP_FK" FOREIGN KEY ("GROUP_CODE")
REFERENCES "GROUP_OF_WORDS" ("GROUP_CODE") ON DELETE CASCADE ENABLE;CREATE OR REPLACE TRIGGER "BI_WORDS_IN_GROUP" before insert on "WORDS_IN_GROUP" for each row begin if :NEW."GROUP_CODE" is null then
select "GROUP_OF_WORDS_SEQ".nextval into :NEW."GROUP_CODE" from dual; end if; end; /ALTER TRIGGER "BI_WORDS_IN_GROUP" ENABLE;
***///////////
CREATE TABLE "WORDS" ( "WORD" VARCHAR2(45) NOT NULL ENABLE,
"FILE_SN" NUMBER(9,0) NOT NULL ENABLE,
"FILE_NAME" VARCHAR2(16) NOT NULL ENABLE,
"POSITION_NUM" NUMBER(5,0) NOT NULL ENABLE,
"SECTION_NUM" NUMBER(5,0) NOT NULL ENABLE,
"WORD_SECTION_POSITION" NUMBER(4,0) NOT NULL ENABLE,
CONSTRAINT "WORDS_PK" PRIMARY KEY ("WORD", "FILE_SN") ENABLE ) ;
This is the error that I get:
ORA-02270: no matching unique or primary key for this column-list
A foreign key must always point to a unique key (either explicitly defined as such or defined as a primary key). As you noted, word in words is not necessarily unique - only the combination of word, file_sn is. You could either make word itself unique or add a flie_sn column to the word_in_group table and have a composite foreign key, whichever fits your application logic better.
You created composite Primary key for the table WORDS. But you just refer only single column in the table WORDS_IN_GROUP. This is not allowed.
You can refer only those column/group of columns which are unique(either enable Primary key/Unique key). If it is composite then you can create composite FK.

Error creating database with multiple primary key columns and referencing foreign key

I've been having an issue when creating a database. Each table has a primary key with many foreign keys used also. The issue I have is that I keep getting the error
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 don't know what is causing this and is flagging the PROJECT_RECORDS table as the issue. I have used the same method in the PROJECT_TABLES table.
SQL/Oracle
CREATE TABLE PROJECT_DB
(DB_ID number (3) NOT NULL primary key,
DB_NAME varchar2 (25) NOT NULL,
DB_DESCRIPTION varchar2 (75) NOT NULL,
DB_DATE date NOT NULL);
CREATE TABLE PROJECT_DATATYPE
(DATATYPE_NAME varchar2 (20) NOT NULL PRIMARY KEY,
DATATYPE_DATATYPE varchar2(50) NOT NULL);
CREATE TABLE PROJECT_TABLES (
PROJECT_ID number(3) not null references PROJECT_DB(DB_ID) on delete cascade,
PROJECT_FIELDNAME varchar2(25) not null,
PROJECT_DATATYPE varchar2(50) not null references PROJECT_DATATYPE(DATATYPE_NAME),
PROJECT_LENGTH number(3),
PROJECT_REQUIRED varchar2(8),
PROJECT_LISTCOLUMNID number (3) not null,
primary key(PROJECT_ID, PROJECT_LISTCOLUMNID));
CREATE TABLE PROJECT_RECORDS (
RECORDS_ROWID number(3) not null,
RECORDS_LISTCOLUMNID number (3) not null references PROJECT_TABLES(PROJECT_LISTCOLUMNID)on delete cascade,
RECORDS_LISTID number (3) not null,
RECORDS_RECORDVALUE varchar2 (25),
primary key(RECORDS_ROWID, RECORDS_LISTCOLUMNID));
commit;
The reasoning for adding multiple primary keys to the PROJECT_TABLES table is that the listcolumnid isn't unique.
If listcolumnid is not unique, you cannot put a foreign key constraint on it. A foreign key always refrences exatcly one parent row. So you probably should use both columns in your foreign key (assuming there is really a 1:n relation):
CREATE TABLE PROJECT_RECORDS (
RECORDS_ROWID number(3) not null,
RECORDS_ID number(3) not null,
RECORDS_LISTCOLUMNID number (3) not null,
RECORDS_LISTID number (3) not null,
RECORDS_RECORDVALUE varchar2 (25),
primary key(RECORDS_ROWID, RECORDS_LISTCOLUMNID),
foreign key fk_project_projectrecords (RECORDS_ID, RECORDS_LISTCOLUMNID) references PROJECT_TABLES(PROJECT_ID, PROJECT_LISTCOLUMNID)on delete cascade,
);
(The example is using your prefix naming convention, I would change RECORDS_ID to PROJECT_ID, same for RECORDS_LISTCOLUMNID)
The problem is with your syntax. You don't do this:
, fieldname datatype references (something)
You do this:
, primary key(somefield)
, foreign key (somefield) references sometable(somefield)

Table in Derby with complex primary key

Is it possible to have a primary key composed of foreign keys fk1, fk2, fk3
Where:
fk1 should never be null
fk2 and fk3 can be null
I have the following SQL:
CREATE TABLE role_mappings (
ROLE_NAME VARCHAR(64) NOT NULL,
NESTED_ROLE_NAME VARCHAR(64) DEFAULT NULL,
PRIVILEGE_NAME VARCHAR(64) DEFAULT NULL,
PRIMARY KEY (ROLE_NAME, NESTED_ROLE_NAME, PRIVILEGE_NAME),
CONSTRAINT fk_access_roles_rm_1 FOREIGN KEY (ROLE_NAME) REFERENCES access_roles (ROLE_NAME),
CONSTRAINT fk_access_privileges FOREIGN KEY (PRIVILEGE_NAME) REFERENCES access_privileges(PRIVILEGE_NAME),
CONSTRAINT fk_access_roles_rm_2 FOREIGN KEY (NESTED_ROLE_NAME) REFERENCES access_roles(ROLE_NAME)
);
When I insert NULL into NESTED_ROLE_NAME or PRIVILEGE_NAME, I get:
Column 'NESTED_ROLE_NAME' cannot accept a NULL value.
What's the proper way of doing this?
Primary key values must be unique. If you allow null values into these fields that makeup the primary key you will quickly run into trouble with non-uniqueness. AVOId using null values would be my advice.