Can someone tell me how do I create a class in sqldeveloper? - oracle

Recently I'm learning sql by myself and it's really tough to me.
For example I'd like create a sql class called XXX and also I'd like add some attributes into the class like country, population, etc.
So how do I write the code? Can anyone show me how to do that with a simple example?
I would fully appreciate for that.

You can create a Type like below.
CREATE OR REPLACE TYPE MYCLASS AS OBJECT
(Country varchar2(30),
Population varchar2(30)
);
/
CREATE TABLE MYTABLE of MYCLASS;

You can create table like this:
create table your_class_table
(
country varchar2(2000 char),
population number
)
then you can insert data into it then select.

Related

Oracle 12c, export table with sequence as default value, schema is attached

I'm use oracle 12c with a user u1. With user u1 I create table T1 like this,
create SEQUENCE T1_SEQ START WITH 1;
CREATE table T1(
id number(11) DEFAULT T1_SEQ.nextval PRIMARY KEY ,
name varchar2(255)
);
Now I want to export the schema to others, and use below command to export
expdp u1/password dumpfile=u1.dmp schemas=u1
, but when others use u1.dmp to import the schema with a new user named u2,
impdp u2/password remap_schema=u1:u2 DUMPFILE=u1.DMP TABLE_EXISTS_ACTION=REPLACE
error happend, beacause default value of table T1 column id add U1 as prefix.
U1.T1_SEQ.nextval
and here is the new create table statement
CREATE table T1(
id number(11) DEFAULT U1.T1_SEQ.nextval PRIMARY KEY ,
name varchar2(255)
);
How can I remove the U1's influence while I want to move one schema to another schema.
Could you give me some suggestions? Thanks in advance!.
this is a known and ongoing issue according to Ask Tom.
So You can,
excluding the affected objects from the import
using the sqlfile option to import the affected objects
amending the sqlfile script output to point to the correct objects before running it
Please See relevant topic

'Error: ORA-01722: invalid number' when inserting into a table?

I'm very new to SQL and I am having trouble with an assignment that involves inserting records into a table. So far I have:
insert into show (show_id,production_company_id,
genre,name_of_show,mpaa_rating,feature_type) values
(1,1,'comedy','click','r','movie');
It keeps telling me the specified number is invalid and to specify a number but I don't know what that means since there is already a number there. I tried looking at other similar questions on the site, but most of them were a lot more complicated than what I have here. Thanks for any help.
edit: also, production_company_id is a foreign key if that helps at all.
here is the original table:
CREATE TABLE show (
SHOW_ID number(6,0) PRIMARY KEY,
PRODUCTION_COMPANY_ID NUMBER(6,0) NOT NULL REFERENCES PRODUCTION_COMPANY (PRODUCTION_COMPANY_ID),
GENRE VARCHAR(25),
NAME_OF_SHOW VARCHAR(25),
MPAA_RATING VARCHAR(25),
FEATURE_TYPE VARCHAR(25)
);
Please make sure that PRODUCTION_COMPANY_ID is defined as number in the table PRODUCTION_COMPANY, because in your table creation of show, production_company_id refers to that table.
If not, you can modify the column type like:
ALTER TABLE PRODUCTION_COMPANY
MODIFY PRODUCTION_COMPANY_ID number;
Or if you do not want to change the type of that column for some reason, you can try:
insert into show (show_id,production_company_id,
genre,name_of_show,mpaa_rating,feature_type) values
(1,'1','comedy','click','r','movie');

Is it possible to pre-define column data types?

When creating a table
Create table user(
username varchar2(30)
);
Is it possible to somewhere create a static or pre-defined data type?
short_name varchar2(30)
And then use this in the create table statement?
Create table user(
username short_name
);
Edit:
My initial thought was to build up and structure different varchar2 (and other data types) for easy reference when writing sql statements, example:
tiny_name varchar2(10)
short_name varchar2(30)
medium_name varchar2(60)
long_name varchar2(120)
The closest (I have seen so far) seems to be the use of TYPE, but that does not seem to cover what I am specifying in my question

How to declare an object as private

I am using following code to create an object
CREATE OR REPLACE TYPE sales_object IS OBJECT (
dates DATE,
product_id VARCHAR2(20),
product_name VARCHAR2(50),
sale DECIMAL(15,2)
);
which I am using to create a table with following code
CREATE OR REPLACE TYPE year_sales_tab IS TABLE OF sales_object;
Now this table is being used in a function to store and return a grid.
If there is any way to create the sales_object as private?
If there is any way to create the sales_object as private?
Yes.
Instead of creating type sales_object as OBJECT, create it as a RECORD in the package and use it in your code. When you create as an object, it is stored in the database, while if you create as a record, it could only be used in your code not elsewhere.
TYPE sales_object
IS
RECORD
(
DATES DATE,
PRODUCT_ID VARCHAR2(20),
PRODUCT_NAME VARCHAR2(50),
sale DECIMAL(15,2)
);
Update OP is actually talking about the privileges.
You could simply create the type by logging as the user to which you want the privileges to e given, by doing only the current user will have the owner privileges, while other users cannot access the object.
If you are intending to grant privilege to specific users, then
GRANT EXECUTE ON "USER"."TYPENAME" TO "OTHER_USER";

Control constraints of a nested table of references in oracle

I'm doing some Object-Relational stuff in oracle. I'm having problems with a nested table of references, in which I cannot control the PK as usual, so I have to create a trigger to control it.
The types are:
CREATE OR REPLACE TYPE Tipo_Persona AS OBJECT (
id number(5),
nombre varchar2(20),
apellidos varchar2(30),
dni varchar2(9),
telefonos ListaTelefonos,
fecha_alta date,
MAP MEMBER FUNCTION getPID RETURN NUMBER
)NOT FINAL;
/
CREATE OR REPLACE TYPE Tipo_HoraActividad AS OBJECT
(
idact NUMBER(3),
Hora_Inicio DATE,
Dia VARCHAR2(10),
Duracion NUMBER(4,2),
...
Monitor REF Tipo_Monitor
);
/
CREATE OR REPLACE TYPE Tipo_Monitor_Horas AS TABLE OF REF Tipo_HoraActividad;
/
CREATE OR REPLACE TYPE Tipo_Monitor UNDER Tipo_Persona
(
Actividades Tipo_Monitor_Horas,
Sueldo_Hora NUMBER(4,2),
MEMBER FUNCTION sueldo return NUMBER,
PRAGMA RESTRICT_REFERENCES (sueldo, WNDS)
);
/
-- Creation of Tables
...
CREATE TABLE Tabla_Monitor OF Tipo_Monitor
(
constraint PK_MONITOR PRIMARY KEY(id),
constraint UNIQUE_DNI_MONITOR UNIQUE(dni),
dni NOT NULL,
fecha_alta NOT NULL,
nombre NOT NULL
)
NESTED TABLE Actividades STORE AS Actividades_Impartidas;
...
So now... my trigger:
CREATE OR REPLACE TRIGGER TRG_name
BEFORE INSERT OR UPDATE ON Actividades_Impartidas
FOR EACH ROW
...
-- I can deref the :new row of Tipo_HoraActividad and check if
-- their values are NULL. But I don't know how to control if
-- it exists any other ref within the current table pointing to
-- the same HoraActividad(unique constraint). My main problem is
-- that I cannot access to this current table (Actividades_Impartidas)
-- because it is a nested table of a concrete row of Tabla_Monitor.
Can anyone help me?
Thanks in advance.
I myself looked for an answer for this a lot but couldn't find it. Visited the URL that Martin Drautzbug provided in that URL and my opinion changed about this question. You should not make things complicated just because you can. That's one of the foremost rules of database design- reduce anomalies for DML statements as much as possible.
It has been my personal experience that using nested tables as a table column just makes things more complicated and difficult to process with simple SQL statements. I read it here and they confirm this. We try to denormalize database design for saving some amount of efforts by using nested tables but it just results into us putting more efforts trying to un-nest them all the time. Plus, it is difficult to enforce constraints on these nested tables.
One good solution would be to use separate tables instead of the nested tables and normalize as much as you can. If you are designing a real transactional database and going to do a lot of DML you should always escape the un-nesting of tables all the time.
Nested tables is a great option in PL/SQL where you have to bulk collect results from a query temporarily in a collection and process it. It is not the same with creating tables with nested table columns.
I am posting this URL again here for you to visit for more details.

Resources