ORA-01748: only simple column names allowed here in Oracle - sql

What I am trying to do ?
I am trying to create two tables and at the same time i am trying to link them together using foreign and primary keys. However I successfully create my parent table ( Student with primary key ) but failed to create child table ( Attendence with foreign key ).
What is the problem ?
I get the following error while creating Attendence table:
ERROR at line 5: ORA-01748: only simple column names allowed here
My code:
Student table:
create table Student (
ST_ROLLNO NUMBER(6) constraint s_pk primary key,
ST_NAME VARCHAR(30) not null,
ST_ADDRESS varchar(35) not null
);
Attendence table:
create table Attendence (
ST_ROLLNO NUMBER(6),
ST_DATE VARCHAR(30) not null,
ST_PRESENT_ABSENT varchar(1) not null,
constraint f_pk Attendence.ST_ROLLNO foreign key references Student(ST_ROLLNO)
);

Your foreign key constraint syntax is wrong; it should be:
constraint f_pk foreign key (ST_ROLLNO) references Student(ST_ROLLNO)
You are preceding the FK column name with the table name, which is wrong in itself, but also have it in the wrong place.
create table Student (
ST_ROLLNO NUMBER(6) constraint s_pk primary key,
ST_NAME VARCHAR(30) not null,
ST_ADDRESS varchar(35) not null
);
Table STUDENT created.
create table Attendence (
ST_ROLLNO NUMBER(6),
ST_DATE VARCHAR(30) not null,
ST_PRESENT_ABSENT varchar(1) not null,
constraint f_pk foreign key (ST_ROLLNO) references Student(ST_ROLLNO)
);
Table ATTENDENCE created.

According to oracle documentation,
ORA ERR
ORA-01748 only simple column names allowed here
The following is the cause of this error:
This SQL statement does not allow a qualified column name, such as
username.table.column or table.column.
Action you can take to resolve this issue: Remove the qualifications
from the column and retry the operation.
In your case, you are trying to refer to the table name while defining a constraint -
Attendence.ST_ROLLNO - WRONG.
It must contain a simple name without the table name or schema name.

Related

"no matching unique or primary key for this column-list" Error in SQL

Was creating some tables in SQL and got stuck when I had to design the following tables:
As you can see it is impossible to create Room details and services details Table without customer receipt table as they contain Receipt_no as the primary key.
Similarly, it is impossible to create a Customer Receipt table without Room_charges and Service_charges attributes without first creating the two details tables.
Hence, I first created a customer receipt table but without FK constraints on Room_Charges and Service_charges and then I created Services Details and Room Details tables.
Later, using ALTER command I tried to add FK Constraints on Customer Receipt table but it gives me this error
ORA-02270: no matching unique or primary key for this column-list
Now, after researching a bit about it on StackOverflow, there might be three possible cases as mentioned in the approved answer (# Oracle (ORA-02270) : no matching unique or primary key for this column-list error )
I think my case is number 3 as I have ensured the first two cases to be implemented.
Can anyone help me resolve it?
I am attaching SQL Code as a reference:
CREATE TABLE Customer_Receipt
(
Receipt_no VARCHAR2(12) PRIMARY KEY,
Booking_no NUMBER NOT NULL,
Total_charges NUMBER(12,2),
CONSTRAINT bookingnocustrec
FOREIGN KEY(Booking_no) REFERENCES Room_booking (Booking_no)
);
CREATE TABLE Services_Details
(
Receipt_no VARCHAR2(12) NOT NULL,
Service_offered VARCHAR2(8) NOT NULL,
Service_charges NUMBER(12,2),
PRIMARY KEY(Receipt_no, Service_offered),
CONSTRAINT recno
FOREIGN KEY(Receipt_no) REFERENCES Customer_receipt (Receipt_no)
);
ALTER TABLE Services_Details
MODIFY Service_charges NOT NULL;
CREATE TABLE Room_Details
(
Receipt_no VARCHAR2(12) NOT NULL,
Category_name VARCHAR2(9) NOT NULL,
Days_stayed INT,
Room_charges NUMBER(12,2),
PRIMARY KEY(Receipt_no, Category_name),
CONSTRAINT recno1
FOREIGN KEY(Receipt_no) REFERENCES Customer_receipt (Receipt_no),
CONSTRAINT catname1
FOREIGN KEY(Category_name) REFERENCES Room_category (Category_name)
);
ALTER TABLE Customer_receipt
ADD Room_charges NUMBER(12,2) NOT NULL;
ALTER TABLE Customer_receipt
ADD CONSTRAINT FK_RC
FOREIGN KEY (Room_charges) REFERENCES Room_Details (Room_charges);
As a frame challenge.
Can anyone help me resolve it?
Yes, do not violate Third Normal Form and do not duplicate data by storing Total_Charges, Service_Charges or Room_Charges in the Customer_Receipt table when the data is already stored in the Service_Details and Room_Details tables.
If you are storing the same data in two locations then you are likely get into the situation where the data is inconsistent between those two location; just store each piece of data in a single location so there is a single source of truth in your database.
CREATE TABLE Customer_Receipt
(
Receipt_no VARCHAR2(12)
CONSTRAINT custreceipt__recno__pk PRIMARY KEY,
Booking_no CONSTRAINT custreceipt__bookingno__fk REFERENCES Room_booking
NOT NULL
);
CREATE TABLE Services_Details
(
Receipt_no CONSTRAINT servicedetails__recno__fk REFERENCES Customer_receipt
NOT NULL,
Service_offered VARCHAR2(8)
NOT NULL,
Service_charges NUMBER(12,2),
CONSTRAINT servicedetails__recno_servoff__pk PRIMARY KEY(Receipt_no, Service_offered)
);
CREATE TABLE Room_Details
(
Receipt_no CONSTRAINT roomdetails__recno__fk REFERENCES Customer_receipt
NOT NULL,
Category_name CONSTRAINT roomdetails__catname__fk REFERENCES Room_category
NOT NULL,
Days_stayed INT,
Room_charges NUMBER(12,2),
CONSTRAINT roomdetails__recno_catname__pk PRIMARY KEY(Receipt_no, Category_name)
);
If you want to display the Total_Charges, Service_Charges and Room_Charges then use a JOIN and get the data from the related tables. Something like:
SELECT cr.*,
COALESCE(s.service_charges, 0) AS service_charges,
COALESCE(r.room_charges, 0) AS room_charges,
COALESCE(s.service_charges, 0) + COALESCE(r.room_charges, 0)
AS total_charges
FROM customer_receipt cr
LEFT OUTER JOIN (
SELECT receipt_no,
SUM(service_charges) AS service_charges
FROM services_details
GROUP BY receipt_no
) s
ON cr.receipt_no = s.receipt_no
LEFT OUTER JOIN (
SELECT receipt_no,
SUM(days_stayed * room_charges) AS room_charges
FROM room_details
GROUP BY receipt_no
) r
ON cr.receipt_no = r.receipt_no;
Or create a view (or a materialized view).

Can we update a record that is defined as a foreign key of a unique record define in a different table?

I am new to PostgreSQL and how i can update the records while they are related with a foreign key definition.
I am getting an error and that would be great if you can guide me with any hints
Let's say we have two different tables like below :
CREATE TABLE IF NOT EXISTS student
(
id_num VARCHAR(40) NOT NULL REFERENCES registered(student_id),
first_name VARCHAR(32) NOT NULL,
last_name VARCHAR(32) NOT NULL,
birthdate DATE NOT NULL,
PRIMARY KEY(id_num)
);
CREATE TABLE IF NOT EXISTS registered
(
student_id VARCHAR(40) NOT NULL UNIQUE,
paid_tuition BOOL NOT NULL,
PRIMARY KEY(paid_tuition)
);
Based on my understating i need to fill the registered table and then try to insert values to the student table that their id match the student_id value in registered table.
But when I try it I get the following error?
Any idea or recommendation?
Message:"update or delete on table "registered" violates foreign key
constraint "student_id_num_id_fkey" on table "student"",
Detail:"Key (id_num)=(idNum-1) is still referenced from table
"student".", Hint:"", Position:0, InternalPosition:0,
InternalQuery:"", Where:"", SchemaName:"", TableName:"student",
ColumnName:"", DataTypeName:"",
ConstraintName:"student_id_num_id_fkey", File:"ri_triggers.c",
Line:2490, Routine:"ri_ReportViolation"}
seems like you are linking tables in opposite direction , to me it makes more sense that "registered" table has been linked to student table by fk studentid .
also in your "student" table definition you are saying that column "id" is primary key while no "id" column has been declared.
so here is what I mean:
CREATE TABLE IF NOT EXISTS student
(
id_num VARCHAR(40) NOT NULL REFERENCES registered(student_id),
first_name VARCHAR(32) NOT NULL,
last_name VARCHAR(32) NOT NULL,
birthdate DATE NOT NULL,
PRIMARY KEY(id_num)
);
CREATE TABLE IF NOT EXISTS registered
(
student_id VARCHAR(40) NOT NULL REFERENCES student(id_num)
paid_tuition BOOL NOT NULL,
PRIMARY KEY(id_num)
);

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

Missing Parenthesis in Apex

I'm trying to write a script in Apex to create multiple tables. The first table is created with no issues but every table after that one gives me a missing parenthesis issue. Sometimes it's the left, sometimes it's the right. I've tried everything with no avail.
I have debugged it numerous times, spoken with the others and have not found the solution.
Create Table Employee -- Creates Employee table and references it to ProjDept table
(
EmployeeID Number(4) Not Null,
FirstName VarChar2(15) Not Null,
LastName VarChar2(20) Not Null,
ProjDeptID Number(4) Not Null,
PhoneNumber Number(10),
Constraint Employee_pk Primary Key (EmployeeID), -- sets primary key for table
Constraint Employee_FK Foreign Key References ProjDept(ProjDeptID)-- identifies foreign key
);
This is the second table in the script that won't work, the next 2 tables generate similar errors.
You forgot to include the name of the column which is referencing another in the foreign key:
Create Table Employee (
EmployeeID Number(4) Not Null,
FirstName VarChar2(15) Not Null,
LastName VarChar2(20) Not Null,
ProjDeptID Number(4) Not Null,
PhoneNumber Number(10),
Constraint Employee_pk Primary Key (EmployeeID), -- sets primary key for table
Constraint Employee_FK Foreign Key (ProjDeptId) References ProjDept(ProjDeptID)-- identifies foreign key
);
db<>fiddle
Where exactly in Apex are you executing those commands?
If SQL Workshop's SQL Commands, then you can't have more than a single command in there, i.e. you should create tables one by one:
create the first table
delete that create table command and write another one, for the second table; then create it
the same goes for other tables as well
Alternatively, go to SQL Workshop's SQL Scripts and put all your commands into a script, e.g.
create table a (id number, name varchar2(20), ...);
create table b (cdate date, ...);
save & run the script.

There is no unique constraint matching given keys for referenced table "employee" 1

Here is my main table:
create table employee
(
eno SERIAL primary key,
ename varchar(100) not null,
title varchar(100) not null
);
I want to referenced the title only because I've already referenced the eno in another table.
create table pay
(
title varchar(100),
sal money not null,
foreign key(title) references employee(title)
);
I get an error that there is no unique constraint matching given keys for referenced table "employee" 1
Please help me. I'm having a hard time solving this error. I'm still a beginner at SQL. thanks a lot
There is no limit to the number of tables that may reference a given table.
Foreign keys may only reference primary keys.
Use a foreign key to eno:
create table pay
(
eno varchar(100),
sal money not null,
foreign key(eno) references employee(eno)
);