reference primary key from another table [closed] - sql

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
I have to create 2 tables.
the first one
CREATE TABLE orders
( order_id number(10) NOT NULL,
order_name varchar2(50) NOT NULL,
payment_id number(10) NOT NULL,
CONSTRAINT order_id PRIMARY KEY (order_id),
);
and when creating the second one I got this error
ORA-02270: no matching unique or primary key for this column-list
CREATE TABLE payment
(
payments_id number(10) NOT NULL,
payment_name varchar(50) NOT NULL,
CONSTRAINT payments_id PRIMARY KEY (payments_id),
FOREIGN KEY (payments_id) REFERENCES orders(payment_id)
);
not sure what I'm doing wrong
please help

You need to reference a UNIQUE or PRIMARY KEY column. The payment_id column does not have one of those constraints on it.
From the Oracle constraint documentation:
Foreign Key Constraints
A foreign key constraint (also called a referential integrity constraint) designates a column as the foreign key and establishes a relationship between that foreign key and a specified primary or unique key, called the referenced key.
Instead, you can add an order_id column to your table:
CREATE TABLE orders(
order_id NUMBER(10) NOT NULL,
order_name VARCHAR2(50) NOT NULL,
CONSTRAINT orders__order_id__pk PRIMARY KEY (order_id)
);
CREATE TABLE payment(
payments_id NUMBER(10) NOT NULL,
payment_name VARCHAR2(50) NOT NULL,
order_id NOT NULL,
CONSTRAINT payment__payments_id__pk PRIMARY KEY (payments_id),
CONSTRAINT payment__order_id__fk FOREIGN KEY (order_id)
REFERENCES orders (order_id)
);
db<>fiddle here

Related

me I get the error ORA-00904: : invalid identifier (im a new in sql) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
create table Factura
(
FacturaID smallint not null,
FacturaFecha date,
FacturaObservacion varchar(1000),
constraint pkFactura primary key (FacturaID),
constraint fkFactura foreign key (LocalID) references Cliente(ClienteID),
constraint fkFactura foreign key (VendedorID) references Vendedor(VendedorID),
constraint fkFactura foreign key (ClienteID) references Local1(LocalID),
);
create table Local1
(
LocalID smallint not null,
LocalNombre varchar(100),
constraint pkLocal1 primary key (LocalID)
);
create table Vendedor
(
VendedorID smallint not null,
VendedorNombres varchar(40),
VendedorApellidos varchar(40),
constraint pkVendedor primary key (VendedorID)
);
create table Cliente
(
ClienteID smallint not null,
ClienteDireccion varchar(40) not null,
ClienteCiudad varchar(30) not null,
constraint pkCliente primary key (ClienteID)
);
It seems you probably got your code from another database such as MySQL, SQL Server, or DB2.
Oracle doesn't have the SMALLINT data type (that I replaced with NUMBER(6)), and discourages the use of the VARCHAR data type (that I replaced by VARCHAR2). You also forgot to add a few columns.
I modified your SQL statements and now they run in Oracle. See below:
create table Local1
(
LocalID number(6) not null,
LocalNombre varchar2(100),
constraint pkLocal1 primary key (LocalID)
);
create table Vendedor
(
VendedorID number(6) not null,
VendedorNombres varchar2(40),
VendedorApellidos varchar2(40),
constraint pkVendedor primary key (VendedorID)
);
create table Cliente
(
ClienteID number(6) not null,
ClienteDireccion varchar2(40) not null,
ClienteCiudad varchar2(30) not null,
constraint pkCliente primary key (ClienteID)
);
create table Factura
(
FacturaID number(6) not null,
FacturaFecha date,
FacturaObservacion varchar2(1000),
LocalID number(6),
VendedorID number(6),
ClienteID number(6),
constraint pkFactura primary key (FacturaID),
constraint fkFactura1 foreign key (LocalID) references Cliente(ClienteID),
constraint fkFactura2 foreign key (VendedorID) references Vendedor(VendedorID),
constraint fkFactura3 foreign key (ClienteID) references Local1(LocalID)
);
See them running at SQL Fiddle.

MySQL Incorrect Foreign Key [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
MySQL (mariadb Ver 15.1 Distrib 10.1.44-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2) gave me this error:
ERROR 1005 (HY000) at line 129: Can't create table `Houdini`.`stamp` (errno: 150 "Foreign key constraint is incorrectly formed")
when trying to create this table:
DROP TABLE IF EXISTS stamp;
CREATE TABLE stamp (
id INT NOT NULL,
name VARCHAR(50) NOT NULL,
group_id SMALLINT NOT NULL,
member BOOLEAN NOT NULL DEFAULT FALSE,
rank SMALLINT NOT NULL DEFAULT 1,
description VARCHAR(255) NOT NULL DEFAULT '',
PRIMARY KEY(id),
CONSTRAINT stamp_ibfk_1 FOREIGN KEY (group_id) REFERENCES stamp_group (id) ON DELETE CASCADE ON UPDATE CASCADE
);
What is the correct foreign key constraint for this? I presume group_id can't be used for some reason
The foreign keys are members of the primary key of the quest_award_item table, so they must be the same data type as themselves, but that's not the problem.
You seem to think that they must have the same data type as the primary key of their own table, but this is in fact not required.
The requirement is that foreign key column(s) must have the same data type as the column(s) they reference.
In this case, quest_award_item.quest_id must be the same data type as quest.id.
Likewise, quest_award_item.item_id must be the same data type as item.id.
You haven't shown the table definitions of the quest or item tables, so we can only guess at their data types. But one or other other may have an incompatible data type.
Re your comment:
So how do I fix this?
You posted in a comment below that the quest.id column is defined as SERIAL, which MySQL translates into BIGINT UNSIGNED AUTO_INCREMENT.
The foreign key column that references quest.id must be the same data type as the column it references (the AUTO_INCREMENT part is not necessary to match the data type).
So change your CREATE TABLE:
CREATE TABLE quest_award_item (
quest_id BIGINT UNSIGNED NOT NULL,
item_id INT NOT NULL,
PRIMARY KEY (quest_id, item_id),
CONSTRAINT quest_award_item_ibfk_1 FOREIGN KEY (quest_id) REFERENCES quest (id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT quest_award_item_ibfk_2 FOREIGN KEY (item_id) REFERENCES item (id) ON DELETE CASCADE ON UPDATE CASCADE
);
An alternative solution is to modify the quest.id to be an INT, and then your original CREATE TABLE quest_award_item will work.
ALTER TABLE quest MODIFY COLUMN id INT AUTO_INCREMENT;
But if you already have rows in that table with id values great enough that they need to be BIGINT UNSIGNED (i.e. greater than 231-1), then you can't do that.
could be the data type between the two key are not the same
DROP TABLE IF EXISTS stamp;
CREATE TABLE stamp (
id INT NOT NULL,
name VARCHAR(50) NOT NULL,
group_id INT NOT NULL,
member BOOLEAN NOT NULL DEFAULT FALSE,
rank SMALLINT NOT NULL DEFAULT 1,
description VARCHAR(255) NOT NULL DEFAULT '',
PRIMARY KEY(id),
CONSTRAINT stamp_ibfk_1 FOREIGN KEY (group_id) REFERENCES stamp_group (id) ON DELETE CASCADE ON UPDATE CASCADE
);
if you use int for id you should use int for group_id

Recursive relationship SQL error

I am pretty new to SQL and I have a problem. I want to make a recursive relationship (a table that relates to itself), but I get an error when I try to execute my code. It's working fine without the Coordinator_Office_ID foreign key.
The error is:
The number of columns in the foreign-key referencing list is not equal
to the number of columns in the referenced list.
Create table Logistican (
Office_ID Number(10) Constraint nb_office Not NULL,
Worker_ID Number(15) Constraint lg_worker not null,
Name_logistican Varchar(20),
Room Varchar(10) constraint log_room UNIQUE,
Coordinator_Office_ID Integer,
Primary key (Office_ID, Worker_ID),
Constraint work_id Foreign key (Worker_ID) References worker(worker_ID) on delete cascade,
Constraint lg_cord_id Foreign key (Coordinator_Office_ID) References Logistican(Office_ID)
);
Yes, that's cause you have defined composite primary key like Primary key (Office_ID, Worker_ID) and thus your FK should include both of them else it will result in PFD (partial functional dependency)
Add the constraint with alter table:
Create table Logistican (
Office_ID Number(10) Constraint nb_office Not NULL,
Worker_ID Number(15) Constraint lg_worker not null,
Name_logistican Varchar(20),
Room Varchar(10) constraint log_room UNIQUE,
Coordinator_Office_ID Integer,
Primary key (Office_ID, Worker_ID),
Constraint work_id Foreign key (Worker_ID) References worker(worker_ID) on delete cascade
);
alter table Logistican
add Constraint lg_cord_id
Foreign key (Coordinator_Office_ID, Worker_Id) References Logistican(Office_ID, Worker_Id);
The relationship needs all elements of the primary key to be valid. I'm not sure if it needs to be a separate statement in Oracle.

name already used by an existing constraint [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
Create Table Resources_user
(
Resources_userID INTEGER NOT NULL,
Resources_ID INTEGER NOT NULL,
User_ID INTEGER NOT NULL,
Data Accessed DATE,
CONSTRAINT PK_Resources_user PRIMARY KEY (Resources_userID),
constraint fk_Resources_user1 Foreign key (Resources_ID ) references Resources,
constraint fk_Resources_user2 Foreign key (User_ID) references User1);
Create table Staff_Position
(
Staff_Position_ID INTEGER NOT NULL,
Position_ID INTEGER NOT NULL,
User_ID INTEGER NOT NULL,
CONSTRAINT PK_Staff_Position PRIMARY KEY (Staff_Position_ID),
Constraint fk_Staff_Position1 foreign key (Position_ID) references position,
Constraint fk_Staff_Position2 foreign key (User_ID) references User1);
Thank you the problem been solved, I realized that i need to number my foreign keys for avoiding the error of repeating .
Your foreign key constraints are incomplete. You did not specify a field. This:
, constraint fk_Resources_user Foreign key (Resources_ID ) references Resources
should be something like this:
, constraint fk_Resources_user Foreign key (Resources_ID )
references Resources (resources_id)
Some Oracle error messages are misleading. This is one of them.
Edit Starts Here
You also have duplicate constraint names for your foreign keys. You will likely get a message indicating such once the other problems are resolved.

SQL Oracle CREATE - unknown issue [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
After copping much abuse for the first time I asked this question I have calmed down and I am trying again and trying to be more specific.
I have done an assignment for Uni and this was the following prompt for one of the questions:
Write Create Table SQL statements for the relational schema that you have created
Place the text in the specified location in the file: ASS1_SQL.TXT
• All tables must have primary keys.
• All tables must have appropriate foreign key constraints.
• Each foreign key column must have identical column name, datatype and size of the primary key
that it refers to
• Add any NOT NULL constraints as dictated by the ERD
• The following columns data types and sizes must be used
suppid, stkid number(2)
suppname, stkname varchar2(30)
sellprice, purchaseprice number(6,2)
MY response to this was:
CREATE Table SUPPLIER(
suppid Number(2) NOT NULL,
suppname varchar2(30),
stkid Number(2) NOT NULL,
citycode Number(2) NOT NULL,
Primary Key (suppid),
Foreign Key (citycode) references CITY
)
CREATE Table STOCKITEM(
stkid Number(2) NOT NULL,
stkname varchar2(30) ,
sellprice Number(6,2) ,
purchaseprice Number(6,2) ,
suppid Number(2) ,
Primary Key (stkid) ,
whid Number(2) NOT NULL,
suppid Number(2) Foreign Key references SUPPLIER ,
whid Number(4) Foreign Key references WAREHOUSE
)
Before you say that I am pointing to tables that I haven't created (and mark my Question down), Please note that the WAREHOUSE and CITY tables have been created in the Database that I am using already.
This code works and creates the tables. However, I received 0 marks out of 10 with no explanation. The above code is slightly improved from initially as (I believe) I have fixed up the NOT NULL attributes.
Do my NOT NULL and FOREIGN KEY Constraints seem to have the right syntax?
The ERD can be found in a pdf at https://www.dropbox.com/sh/eohlj5h073kwp4u/Ot08kbdY7Q
Before voting this question down please consult me first and I can adjust it. I am new to this website so give me a chance please
Your foreign key syntax is incorrect.
Try this:
CONSTRAINT fk1 FOREIGN KEY (suppid)
REFERENCES STOCKITEM(suppid)
Change your syntax to resemble the above.
Also, your primary key should be declared before any of your variables.
Lastly, the Primary key syntax in oracle looks like this:
CONSTRAINT pk PRIMARY KEY (suppid));
Complete Code:
CREATE Table SUPPLIER(
suppid Number(2) NOT NULL,
suppname varchar2(30),
stkid Number(2) NOT NULL,
citycode Number(2) NOT NULL,
CONSTRAINT pk1 PRIMARY KEY (suppid),
CONSTRAINT fk1 FOREIGN KEY (citycode) References ParentTable(primary_key_column)
)
CREATE Table STOCKITEM(
stkid Number(2) NOT NULL,
stkname varchar2(30) ,
sellprice Number(6,2) ,
purchaseprice Number(6,2) ,
suppid Number(2) ,
whid Number(2) NOT NULL,
CONSTRAINT pk2 PRIMARY KEY (stkid),
CONSTRAINT fk2 FOREIGN KEY (suppid) References SUPPLIER(primary_key_column),
CONSTRAINT fk3 FOREIGN KEY (whid) References WAREHOUSE (primary_key_column)
)
Note:
You will, of course, need to change *primary_key_column* in the above example to your column name.
This is wrong:
Foreign Key (citycode) references CITY
because it doesn't reference a field in that table.
Also, these appear to be in the wrong order:
Primary Key (stkid) ,
whid Number(2) NOT NULL,
I always declare all my fields before my keys.