CREATE TABLE
Problem: “invalid datatype” error
Expected result: Create table “Appointment”
Have created multiple tables previously with the same datatypes, minus time. All foreign keys reference existing tables.
Chat GPT says the code is free of error. Have tried changing NUMERIC to DECIMAL, no avail. Not sure how to proceed.
yvonne chen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
As per Oracle documentation and my understanding, there is no TIME
datatype.
Character Data Types
CHAR(size)
VARCHAR2(size)
NCHAR(size)
NVARCHAR2(size)
CLOB
NCLOB
Numeric Data Types
NUMBER(p,s)
FLOAT
BINARY_FLOAT
BINARY_DOUBLE
Date and Time Data Types
DATE
TIMESTAMP
TIMESTAMP WITH TIME ZONE
TIMESTAMP WITH LOCAL TIME ZONE
INTERVAL YEAR TO MONTH
INTERVAL DAY TO SECOND
Binary Data Types
RAW(size)
LONG RAW
BLOB
Rowid Data Types
ROWID
UROWID
Other Data Types
BFILE
XMLType
Object Types
Collection Types
In Oracle, a DATE
is a binary data-type that consists of 7-bytes representing: century, year-of-century, month, day, hour, minute and second. It ALWAYS has those components so you can always store the time component with the date. Oracle does NOT have a separate TIME
data-type.
Your statement should be:
CREATE TABLE Appointment
(
Appt_ID INTEGER,
Pt_ID,
Proc_ID,
Prov_ID,
Tooth_Num INTEGER,
Tx_Cost NUMBER(8,2),
Appt_Date DATE,
CONSTRAINT PK_Appointment PRIMARY KEY (Appt_ID),
CONSTRAINT FK_Appointment_Pt_ID FOREIGN KEY (Pt_ID) REFERENCES Patient (Pt_ID),
CONSTRAINT FK_Appointment_Proc_ID FOREIGN KEY (Proc_ID) REFERENCES Procedure (Proc_ID),
CONSTRAINT FK_Appointment_Prov_ID FOREIGN KEY (Prov_ID) REFERENCES Provider (Prov_ID)
);
Note: you do not need to specify the data-type for columns that are part of a foreign key constraint. If you omit the data-type then Oracle will infer the data-type from the referenced columns and this ensures that your columns always have data-types that match the columns that they reference.
Note 2: If the client application you are using is not displaying the time component for the DATE
then change the settings on the client application (i.e. SQL Developer) or use TO_CHAR(appt_date, 'YYYY-MM-DD HH24:MI:SS')
to format the date when you display it – Oracle ALWAYS stores both date and time components for a DATE
data-type.
Note 3: PROCEDURE
is a keyword in Oracle – while you can use it as an identifier (as it is not a reserved word), it might be better to call the table something else, such as PROCEDURES
, to avoid confusion.
If you really must have separate date and time columns then you can use an INTERVAL DAY(0) TO SECOND(0)
data-type to store a time – but then you would have a time component in both appt_date
and appt_time
which may be confusing (and redundant).
fiddle