SQL Oracle CREATE - unknown issue [closed] - sql

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.

Related

reference primary key from another table [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 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

Foreign Key Reference

I addressed in class that the 2 foreign keys in the FlightData table are Depart_Code and Ariv_Code that there isn't any table to make references to them being a primary key in, in the relational schema we were given.
In class I was told that they reference Airport_Code in the Airport table. I was wondering I would go about doing that? I feel like I am missing something obvious. I appreciate any help offered I am still new to database in general and I am currently on Oracle 11g.
Airport table
CREATE TABLE Airport
(
Airport_Code VARCHAR2(7) CONSTRAINT pk_Airport Primary Key,
City_Code VARCHAR2(3),
CONSTRAINT fk_Airport_City_Code
FOREIGN KEY(City_Code) REFERENCES City,
Airport_Name VARCHAR2(30)
);
FlightData table:
CREATE TABLE FlightData
(
Flt_Nbr VARCHAR2(3) CONSTRAINT pk_FlightData Primary Key,
Depart_Code VARCHAR2(30),
Ariv_Code VARCHAR2(30)
);
To make sure Depart_Code and Ariv_Code always reference an airport in the Airport table you need to:
Make these columns NOT NULL.
Ensure they have the same data type as the key in Airport. Make them have a length of 7.
Add two foreign key constraints, each one based on each column.
For example, the second table could look like:
CREATE TABLE FlightData (
Flt_Nbr VARCHAR2(3) CONSTRAINT pk_FlightData Primary Key,
Depart_Code VARCHAR2(7) not null,
constraint fk1 foreign key (Depart_Code) references Airport (Airport_Code),
Ariv_Code VARCHAR2(7) not null,
constraint fk2 foreign key (Ariv_Code) references Airport (Airport_Code)
);

foreign keys: number of columns not equal to referenced columns

I'm getting an error from oracle that says "number of referencing columns must match referenced columns."
I want my column recorded_on in the table measurement to reference recorded_on in the table called sample
The column Recorded on in the Sample table must be part of a composite key together with Scientist_Num
The error is coming from
FOREIGN KEY (Recorded_On) REFERENCES Sample(Scientist, Recorded_On, Site_ID)
CREATE TABLE Sample (
Scientist_Num varchar2(5) not null,
Recorded_On date not null,
Site_ID varchar2(4) not null,
Comments clob,
Primary key (Scientist_Num, Recorded_On),
FOREIGN KEY (Scientist_Num) REFERENCES Scientist(Scientist_Num),
FOREIGN KEY (Site_ID) REFERENCES Site(Site_ID)
);
CREATE TABLE Measurement (
Site_ID varchar2(4) not null,
Recorded_On date not null,
Name varchar2(10) not null,
Value varchar2(10),
Outlier_Indicator varchar2(10),
Primary key (Site_ID, Recorded_On, Name),
FOREIGN KEY (Site_ID) REFERENCES Sample(Site_ID),
FOREIGN KEY (Recorded_On) REFERENCES Sample(Scientist, Recorded_On, Site_ID)
);
The Scientist_Num and Recorded_On columns must be in a composite key together.
The answer to my problem and an explanation of what went wrong would be greatly appreciated.
You can create virtual column in sample table:
Recorded_virtual varchar2(5) [GENERATED ALWAYS] AS
(Scientist||Recorded_On||Site_ID) [VIRTUAL]
And create reference to this column:
CONSTRAINT fk_column
FOREIGN KEY (Recorded_On)
REFERENCES Sample(Recorded_virtual )
Foreign key references need to match the primary keys in number and type. So I think you intend:
CREATE TABLE Measurement (
Site_ID varchar2(4) not null,
Scientist_Num varchar2(5) not null,
----^ added for foreign key reference
Recorded_On date not null,
Name varchar2(10) not null,
Value varchar2(10),
Outlier_Indicator varchar2(10),
Primary key (Site_ID, Recorded_On, Name),
FOREIGN KEY (Site_ID) REFERENCES Site(Site_ID),
-------------------------------------^ Presumably you intend the site table
FOREIGN KEY (Scientist_Num, Recorded_On) REFERENCES
Sample(Scientist_Num, Recorded_On)
-----------------^ two columns, both need to already be defined
);
I suspect there are other issues with your data model, but this should fix the syntax error. If you want further help, then ask another question.

Oracle SQL: Receiving 'no matching unique or primary key' error and don't know why

I'm receiving this error when trying to create a table and I don't know why:
[2016-07-05 14:08:02] [42000][2270] ORA-02270: no matching unique or primary key for this column-list
This question seems different (to me) from a similar question, because in that question the OP is referencing a table with a composite PK, while I am not.
And while this other question has the same error code, it is because the OP is incorrectly references the primary key, which I don't think I did.
May someone more experienced in SQL educate me?
(A couple of things to note: 1) I know the table/column names have small errors/deviations from convention, but this is for homework, and the teacher requires I have the tables and rows declared exactly his way, even if it's non-conventional. 2) Yes, that's silly; but no, I can't change it or I get marked down.)
CREATE TABLE Student_Course
(
Stu_ID NUMBER(5) NOT NULL,
Course_ID VARCHAR2(8) NOT NULL,
Section# NUMBER(3),
CONSTRAINT pk_stu_crse PRIMARY KEY (Stu_ID, Course_ID),
CONSTRAINT fk_course_id FOREIGN KEY (Course_ID) REFERENCES course(Course_ID),
CONSTRAINT fk_stu_id FOREIGN KEY (Stu_ID) REFERENCES student(Stu_ID),
CONSTRAINT fk_section FOREIGN KEY (Section#) REFERENCES course(Section#)
)
There are only two, small, referenced tables, which are:
CREATE TABLE student
(
Stu_ID NUMBER(5) PRIMARY KEY ,
Lname VARCHAR2(20),
Fname VARCHAR2(20),
Mi CHAR(1),
Sex CHAR(1),
Major VARCHAR2(15),
Home_State CHAR(2)
);
CREATE TABLE course
(
Course_ID VARCHAR2(8) PRIMARY KEY ,
Section# NUMBER(3),
C_Name VARCHAR2(30),
C_Description VARCHAR2(30)
);
A foreign key is a reference to a primary key in another table.
The last constraint CONSTRAINT fk_section FOREIGN KEY (Section#) REFERENCES course(Section#) won't work - Section# isn't a primary key in that table
Section# Must be at least UNIQUE in course table.
If you want to use Section# as a reference for a foreign key, it must be a UNIQUE or a PRIMARY KEY
More information about FOREIGN KEY and constraints
Thanks to good answers, I'm posting my code which I corrected based on help here. Hope my corrections help others in the future.
CREATE TABLE student
(
Stu_ID NUMBER(5) PRIMARY KEY ,
Lname VARCHAR2(20),
Fname VARCHAR2(20),
Mi CHAR(1),
Sex CHAR(1),
Major VARCHAR2(15),
Home_State CHAR(2)
);
CREATE TABLE course
(
Course_ID VARCHAR2(8) ,
Section# NUMBER(3) ,
C_Name VARCHAR2(30),
C_Description VARCHAR2(30),
CONSTRAINT pk_course PRIMARY KEY (Course_ID, Section#)
);
CREATE TABLE Student_Course
(
Stu_ID NUMBER(5) ,
Course_ID VARCHAR2(8) ,
Section# NUMBER(3) ,
CONSTRAINT pk_stu_crse PRIMARY KEY (Stu_ID, Course_ID, Section#),
CONSTRAINT fk_stu FOREIGN KEY (Stu_ID) REFERENCES student(Stu_ID),
CONSTRAINT fk_course_id FOREIGN KEY (Course_ID, Section#) REFERENCES course(Course_ID, Section#)
);

SQL - invalid identifier (not letting me create a foreign key constraint) [duplicate]

This question already has an answer here:
Foreign Key Creation issue in Oracle
(1 answer)
Closed 6 years ago.
I'm currently creating a database that models something like iTunes and am having trouble creating my foreign key constraints.
Here's my song table.
CREATE TABLE Song
(
SongName varchar2(30) NOT NULL,
Remixer varchar2(50) NULL,
TrackID int NOT NULL PRIMARY KEY,
Genre varchar2(50) NOT NULL,
ReleaseDate date NOT NULL,
Bpm int NOT NULL,
Key varchar2(10) NOT NULL,
ArtistID int NOT NULL,
LabelID int NOT NULL,
CONSTRAINT artist_artistID_fk
FOREIGN KEY (ArtistID) REFERENCES Artist(ArtistID),
CONSTRAINT label_labelID_fk
FOREIGN KEY (LabelID) REFERENCES Label(LabelID)
);
This runs fine but when I go to create my marketplace table it gives me this:
ORA-00904: "TRACKID": invalid identifier
Here is my marketplace CREATE statement:
CREATE TABLE Marketplace
(
MarketplaceID int NOT NULL PRIMARY KEY,
MarketplaceName varchar2(30) NOT NULL,
CONSTRAINT track_trackID_fk
FOREIGN KEY (TrackID) REFERENCES Song(TrackID),
CONSTRAINT artist_artistID_fk
FOREIGN KEY (ArtistID) REFERENCES Artist(ArtistID),
CONSTRAINT label_labelID_fk
FOREIGN KEY (LabelID) REFERENCES Label(LabelID),
CONSTRAINT ep_epID_fk
FOREIGN KEY (EpID) REFERENCES Ep(EpID)
);
I also have tables for Artist, Label and EP. Artist and label worked fine but Marketplace and EP didn't work. Any help is much appreciated.
There is no TrackID column being defined at Marketplace table