Invalid table name without using a reserved word? - sql

I'm creating a table using Oracle SQL, and when trying to create a table called 'Card' I keep getting the error that it's an invalid table name. Here's my code:
CREATE TABLE Card
(
CardNumber varchar2(20) Not Null,
CardType varchar2(5) Not Null,
CONSTRAINT CustomerIDForeignKey FOREIGN KEY (CustomerID) REFERENCES (Customer),
CONSTRAINT CardTypeConstraint CHECK (CardType='Credit' or CardType='Debit' or CardType='Gift')
)
And here's the error code I'm getting:
Error starting at line : 1 in command -
CREATE TABLE Card
(
CardNumber varchar2(20) Not Null,
CardType varchar2(5) Not Null,
CONSTRAINT CustomerIDForeignKey FOREIGN KEY (CustomerID) REFERENCES (Customer),
CONSTRAINT CardTypeConstraint CHECK (CardType='Credit' or CardType='Debit' or CardType='Gift')
)
Error report -
SQL Error: ORA-00903: invalid table name
00903. 00000 - "invalid table name"
*Cause:
*Action:
Any help would be appreciated! I didn't find 'Card' on the list of Oracle SQL's reserved words, so I'm not sure what the issue is. Thank you!

You need to add column CustomerId to Card table and change refrences to table_name(column_name):
SqlFiddleDEMO
CREATE TABLE Customer(CustomerID INT PRIMARY KEY);
CREATE TABLE Card(
CardNumber varchar2(20) Not Null,
CardType varchar2(5) Not Null,
CustomerId INT NOT NULL,
CONSTRAINT CustomerIDForeignKey FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerId),
CONSTRAINT CardTypeConstraint CHECK (CardType IN ('Credit', 'Debit' ,'Gift'))
);
Also you can replace:
CHECK (CardType='Credit' or CardType='Debit' or CardType='Gift')
with:
CHECK (CardType IN ('Credit', 'Debit' ,'Gift'))
You can think about:
Adding Primary Key to Card table
Hashing/encrypting card number (do not store as plain text)
Creating CreditCardType dictionary table

Related

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

Oracle SQL "invalid identifier error" when trying to create a table

I have successfully built these tables:
CREATE TABLE Tables
(
tnum NUMBER(4) PRIMARY KEY,
floor NUMBER(1), CHECK (floor >= 0 AND floor <= 4),
capacity NUMBER(2), CHECK(capacity >= 0 AND capacity <= 25),
location VARCHAR(50)
);
CREATE TABLE Customer
(
name VARCHAR(25),
phone VARCHAR(16),
city VARCHAR(25),
PRIMARY KEY(name, phone)
);
CREATE TABLE Menu
(
pid NUMBER(4) PRIMARY KEY,
pname VARCHAR(25),
price NUMBER(4, 1)
);
BUT when I try build this one, I got an error:
Error report -
ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
CREATE TABLE Orders
(
orderid NUMBER(4) PRIMARY KEY,
custname VARCHAR(25),
CONSTRAINT fk_customer
FOREIGN KEY (custname)
REFERENCES Customer(customer.name),
phone VARCHAR(16),
CONSTRAINT fk_customer
FOREIGN KEY(phone)
REFERENCES Customer(customer.phone),
date DATE,
tnum NUMBER(4),
CONSTRAINT fk_tables
FOREIGN KEY(tnum)
REFERENCES Tables(tables.tnum),
numofdiners NUMBER(2)
);
I tried to understand what can cause the problem, even though I checked this link and some more other links, I didn't manage to solve the problem.
You can specify a column level constraint directly after the column, but then you have to get rid of the comma and the foreign key part as it's clear which column is meant.
But you need to include all columns of the primary key in the foreign key definition. And because the primary key of the customer table consist of two columns, you can't use an inline foreign key in the table orders.
The foreign key "references" part needs to specify the column name in the target table without a prefix for the table (the table is already clear because of the references table_xxx part, so there is no need to prefix the target column with the table name).
Additionally DATE is a reserved keyword.
CREATE TABLE Orders
(
orderid NUMBER(4) PRIMARY KEY,
custname VARCHAR(25) not null
phone VARCHAR(16) not null,
reservation_date DATE,
tnum NUMBER(4) --<< no comma here!
-- no FOREIGN KEY keyword here
CONSTRAINT fk_tables
REFERENCES Tables(tnum), --<< only the column name between the (...)
numofdiners NUMBER(2), --<< you need a comma before the table level constraints
CONSTRAINT fk_customer
FOREIGN KEY(name, phone) --<< list both columns here
REFERENCES Customer(name, phone) --<< and here
);
Note that the columns in a multi-column foreign key are matched by position, not by name.
Therefore, the following would be wrong
CONSTRAINT fk_customer
FOREIGN KEY(phone, name)
REFERENCES Customer(name, phone)

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

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.

Crreate table with foreign keys

I m trying to create a table in sql, but it gives me an error when I add the foreign key from the table COURSE. With the other foreign keys it; Can anybody tell me why.
Here is my query
CREATE TABLE ENROLL (
Stu_ID NUMBER NOT NULL,
Prog_ID VARCHAR2(20) NOT NULL,
Crs_ID NUMBER NOT NULL,
Crs_Sec_ID VARCHAR2(20) NOT NULL,
Enroll_Outcome CHAR(1) NOT NULL,
PRIMARY KEY(Stu_ID),
FOREIGN KEY(Stu_ID) REFERENCES STUDENT(Stu_ID),
FOREIGN KEY(Prog_ID) REFERENCES PROGRAMS(Prog_ID),
FOREIGN KEY (Crs_Sec_ID) REFERENCES COURSE_SECTION (Crs_Sec_ID),
FOREIGN KEY(Crs_ID) REFERENCES COURSE(Crs_ID));
and here's the output
Error starting at line : 126 in command -
CREATE TABLE ENROLL (
Stu_ID NUMBER NOT NULL,
Prog_ID VARCHAR2(20) NOT NULL,
Crs_ID NUMBER NOT NULL,
Crs_Sec_ID VARCHAR2(20) NOT NULL,
Enroll_Outcome CHAR(1) NOT NULL,
PRIMARY KEY(Stu_ID),
FOREIGN KEY(Stu_ID) REFERENCES STUDENT(Stu_ID),
FOREIGN KEY(Prog_ID) REFERENCES PROGRAMS(Prog_ID),
FOREIGN KEY (Crs_Sec_ID) REFERENCES COURSE_SECTION (Crs_Sec_ID),
FOREIGN KEY(Crs_ID) REFERENCES COURSE(Crs_ID))
Error report -
SQL Error: ORA-02267: column type incompatible with referenced column type
02267. 00000 - "column type incompatible with referenced column type"
*Cause: The datatype of the referencing column is incompatible with the
AGAIN, it runs well with all of the other foreign key without the Crs_ID.
Basically what is says is Couse ID in Enroll table and Couse ID in Couse table are incompatible. Both columns should have the same types and properties (not null) in order to make a foreign key relationship.
Things to check:
Are they both have the same Column type (should be Number)?
Both are not support null?

Invalid Field Definition

I'm trying to create a table with a foreign key using SQL command but I keep getting this error
Invalid field definition 'CUS_CODE' in definition of index or relationship.
I'm using this command:
CREATE TABLE INVOICE(
INV_NUMBER CHAR(5) NOT NULL,
INV_DATE DateTime NOT NULL,
CONSTRAINT INV_PK PRIMARY KEY(INV_NUMBER),
CONSTRAINT INV_FK FOREIGN KEY(CUS_CODE) REFERENCES CUSTOMER(CUS_CODE)
);
It's because Cus_code is not a field in your invoice table.
It should be :
FOREIGN KEY(Your column name in your invoice table) REFERENCES Customer(Cus_Code)
Here's an tutorial about SQL FOREIGN KEY
It should help you to illustrate what it should looks like