Is it possible to pre-define column data types? - oracle

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

Related

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

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.

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.

OOP concepts in oracle 11.g

i have a question related to Object Orienting programming in oracle database(11.g),generally i have studyed how to create object types or how to create table from existing object,but i am stoped on one topic,namely,suppose that we have created following type
create or replace type mono.Item_type as object(
item_id integer,
part REF mono.Part_type,
quantity integer);
where Part_type is already existed object type.
i know that i can create table from this object like this:
create table tablename of Item_type,for instance
create table item_list of Item_type
what what would be different,if instead of this,we use
create or replace type mono.Item_List as table of Item_type;
here inspite of this,that we use type keyword,we are creating table again and what is different between create table tablename of Item_type and
create or replace type mono.Item_List as table of Item_type;?
When you use create table tablename of Item_type, you're creating a table in the DB.
This table is a structure that can be used to store user's data .
When you use create or replace type mono.Item_List as table you're defining a new Datatype (similar to array).
This Datatype can be used as a type of a column in a DB table or as a type of a variable in plsql code.

want an optimal solution for inserting more than one data in a single column of a single row

DROP TABLE District_Info;
CREATE TABLE District_Info(
Dname VARCHAR2(20) primary key,
Boundary_dist VARCHAR2(20)
);
for this code, for each Dname there are more than one Boundary_dist. it would be better if i could use an array of varchar2(20) and insert as much boundary_dist as i need to. want some helpful suggestions.
As already mentioned, the ideal way to do this would be to store an additional row for each DName/Boundary_Dist combination. The simplest way to do with a structure this simple is to just change your primary key:
CREATE TABLE District_Info(
Dname VARCHAR2(20) primary key,
Boundary_dist VARCHAR2(20) primary key);
If you're going to need other data in that table that has a 1:1 correlation to District, you would be better off splitting BoundayDist into a separate table:
CREATE TABLE District_Info(
Dname VARCHAR2(20) primary key,
Other_info VARCHAR2(20)
);
CREATE TABLE District_Boundary(
Dname VARCHAR2(20) primary key,
Boundary_dist VARCHAR2(20) primary key);
If you really insist on storing more that one value per row, you can use a user-defined datatype:
create type varchar_20_list as table of varchar2(20);
CREATE TABLE District_Info(
Dname VARCHAR2(20) primary key,
Boundary_dist varchar_20_list);
you need to create a separate table for Boundary_dist and create a foreign key which references the District_Info table.
Agreed, it'd be best to have Boundary_dist in a second table and connect them with an ID, but if you must typically what I do is store it as "1,2,3,4,5" that way in the code I can manipulate that pretty easily with PHP using explode or with mysql using IN.

Resources