invalide identifier oracle - sql

CREATE type recommendation as object(
descriptions varchar2(200)
);
create type recomand as table of recommendation;
create type traitement_type as object (
id_traitement number(7),
duree varchar2(25),
recome recomand,
description varchar2(250)
);
CREATE TABLE Traitement (primary key(id_traitement))
nested table recome store as Les_recommendations;
i execute these commands and at the last query i got the error :
ORA-00904: : invalid identifier
any solutions ?

Your create table statement doesn't include a column list. It's interpreting "primary key" a column and spaces are not allowed. Look up the proper format for create table statements and PK constraints.

Related

Why is the column not altering when I try to convert it to UUID?

I have a primary key column in my SQL table in PostgreSQL named "id". It is a "bigseries" column. I want to convert the column to a "UUID" column. It entered the below command in the terminal:
alter table people alter column id uuid;
and
alter table people alter column id uuid using (uuid_generate_v4());
but neither of them worked.
In both tries I got the error message
ERROR: syntax error at or near "uuid"
LINE 1: alter table people alter column id uuid using (uuid_generate...
What is the correct syntax?
First of all uuid_generate_v4() is a function which is provided by an extension called uuid-ossp. You should have install that extension by using;
CREATE EXTENSION uuid-ossp;
Postgresql 13 introduced a new function which does basically the same without installing extension. The function is called gen_random_uuid()
Suppose that we have a table like the one below;
CREATE TABLE people (
id bigserial primary key,
data text
);
The bigserial is not a real type. It's a macro which basically creates bigint column with default value and a sequence. The default value is next value of that sequence.
For your use case, to change data type, you first should drop the old default value. Then, alter the type and finally add new default value expression. Here is the sample:
ALTER TABLE people
ALTER id DROP DEFAULT,
ALTER id TYPE uuid using (gen_random_uuid() /* or uuid_generate_v4() */ ),
ALTER id SET DEFAULT gen_random_uuid() /* or uuid_generate_v4() */ ;
CREATE TABLE IF NOT EXISTS people (
id uuid NOT NULL CONSTRAINT people_pkey PRIMARY KEY,
address varchar,
city varchar(255),
country varchar(255),
email varchar(255),
phone varchar(255)
);
This is the correct syntax to create table in postgres SQL, it's better to do these constraints at beginning to avoid any error.
For using alter command you would do the following:
ALTER TABLE customer ADD COLUMN cid uuid PRIMARY KEY;
Most of errors that you could find while writing command either lower case or undefined correct the table name or column.

Missing Right Parenthesis in SQL

I'm new to learning SQL. When I create this table, it has an Asterix (*) under the first parenthesis of the "(dbClassID)" and says "missing right parenthesis"
Does anyone know why it does that and how I can fix it?
CREATE TABLE vod_classification (
dbClassId CHAR(4) NOT NULL,
dbDescription VARCHAR2(100)
CONSTRAINT vod_classification_PK PRIMARY KEY (dbClassId)
);
CONSTRAINT is part of table creation and need to be comma delimited as other column:
CREATE TABLE zz_classification (
dbClassId CHAR(4) NOT NULL,
dbDescription VARCHAR2(100),
CONSTRAINT vod_classification_PK PRIMARY KEY( dbClassId)
);
Tables contain columns and constraints
you are missing , here try this VARCHAR2(100),
For a single-column constraint, it's neater to define it inline as part of the column:
create table vod_classification
( dbclassid varchar2(4) not null constraint vod_classification_pk primary key
, dbdescription varchar2(100) not null constraint vod_classification_uk unique
);
I have corrected the CHAR column to the standard string type which is VARCHAR2 in Oracle.
(PK columns will be not null automatically, but I've left it in for completeness and in case you later create table as select.)
When using the "Create" code, you must use a comma in the line where you define each column of the table. Except the last column. You can read the oracle sql syntax link as follows: https://docs.oracle.com/cd/E11882_01/server.112/e41085/sqlqr01001.htm#SQLQR110

Oracle SQL - Table Create Issue

i am currently trying to create a person table that allows a person to be either a client or a staff member. Currently this is my code :-
Create Or Replace Type Person As OBJECT
(
person_id number(5) not null,
firstname varchar2(15) not null,
surname varchar2(15) not null,
address1 varchar2(70) not null,
address2 varchar2(70),
address3 varchar2(70),
postcode varchar2(9) not null
);
/
Create Or Replace Type Client Under Person
(
marital_status varchar2(10),
no_of_children number(2)
);
/
Create Or Replace Type Staff Under Person
(
job_title varchar2(15) not null,
salary number(4,2) not null,
manager_id number(5) not null
);
/
Create Table Person Of Person
(
person_id Primary Key
);
The object types all compile but when it goes to create the table i get the following error:-
SQL Error: ORA-00902: invalid datatype
00902. 00000 - "invalid datatype"
what is causing this error to occur and what can be done to rectify it. Any help is greatly appreciated.
********EDIT**********
I have changed the name of the table to person_tbl and still get the same error. For some reason, this error now appears in the compiler log:-
Error: PL/SQL: Compilation unit analysis terminated
Error(1,18): PLS-00905: object [ConnectionName].PERSON is invalid
I have no idea why it isn't letting me use the Person type as I have triede this method before successfully.
Don't know, why you get these error. You should get the error
00955. 00000 - "name is already used by an existing object"
when trying to create a table with the same name as a type.
Try
Create Table Persons Of Person
(
person_id Primary Key
);
You have a few problems. As #tonirush mentioned, you can't have more than one object in a database with the same name.
Additionally, the person type is compiling with errors (specifically PLS-00218: a variable declared NOT NULL must have an initialization assignment). Until you resolve these errors, you can't build an object table based on the object.
Your subtypes also have compilation errors: PLS-00590: attempting to create a subtype UNDER a FINAL type, but that's not relevant to the inability to create the object table.
As a footnote, the word "object" in this answer (and in Oracle databases, in general) is overloaded. In the first paragraph, I'm speaking of "database objects", which is pretty much anything that is created in the database with a create command. For the rest I'm speaking of "object types" which are object created by create type ... object specifically.

error: ORA-01730: invalid number of column names specified

Please help. I want to create an object view from the ffl tables but i keep getting the above error and can't find any solution.
create table COPY_BOOK (
NUM number(4,0),
DATE_Purchase date,
PRICE number(5,2),
LOAN_code varchar2(20) ,
STATUS varchar2(15) check (STATUS in ('GOOD','DAMAGED')),
CONSTRAINT CP_PK primary key (num) ,
constraint Loan_code_D check (LOAN_CODE in ('NO', 'LOAN'))
);
create or replace type copy_book_t as object(
num number(4, 0),
loan_code varchar2 (20)
);
/
create or replace view Vcopy_book of copy_book_t
with object oid (num)
as select cb.num, cb.date_purchase, cb.price, cb.loan_code, cb.status
from copy_book cb;
/
Is there a problem with the type definition?
From the documentation:
The procedure for defining an object view is:
Define an object type, where each attribute of the type corresponds to an existing column in a relational table.
Write a query that specifies how to extract the data from the relational table. Specify the columns in the same order as the
attributes in the object type.
You have created your object type with two attributes, but you're trying to populate it with five columns from your relational table.
You either need to change your object type to have the same five attributes (demo), or only select the num and loan_code columns in the view (demo).
Incidentally, the documentation also recommends using with object identifier rather than with object oid.

oracle11g sql: Warning: Type created with compilation errors

I'm trying to create a supertype customer service and subtype agent and supervisor, so they can inherent values however when I try to run this in oracle sql: a message comes up
Warning: Type created with compilation errors.
What is wrong with the code below?
Create or replace type customer_s_type as object (
csID number,
csName varchar(15),
csType number ) NOT FINAL;
Create or replace type supervisor_type UNDER customer_s_type ( title varchar (10) );
Create or replace type agent_type UNDER customer_s_type (title varchar (10));
Create table supervisor of supervisor_type (
CONSTRAINT supervisor_PK PRIMARY KEY (csID));
Create table agent of agent_type (CONSTRAINT agent_PK PRIMARY KEY (csID));
create table customer_service(
csID number(10),
csType number(10),
constraint supervisor_pk primary key(csID) );
You can use show errors in SQL*Plus or SQL Developer, or select * from user_errors, to see the error details.
Since you've shown six commands and the warning is about one of the first three (since it refers to type), and they appear OK independently apart from the constraint pointed put in comments, it looks like the whole script is being imterpreted as one command. It depends on your client settings, but you probably just need to seperate the commands with a / to cause them to execute. Becuase types can include PL/SQL the ; isn't treated as a statement seperator. So:
Create or replace type customer_s_type as object (
csID number,
csName varchar(15),
csType number ) NOT FINAL;
/
Create or replace type supervisor_type UNDER customer_s_type ( title varchar (10) );
/
Create or replace type agent_type UNDER customer_s_type (title varchar (10));
/
Create table supervisor of supervisor_type (
CONSTRAINT supervisor_PK PRIMARY KEY (csID));
Create table agent of agent_type (CONSTRAINT agent_PK PRIMARY KEY (csID));
create table customer_service(
csID number(10),
csType number(10),
constraint customer_service_pk primary key(csID) );