How to declare an object as private - oracle

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";

Related

How to create a nested table of refs in Oracle SQL?

Consider the following problem:
There's the Transaction type and the Service type. Each transaction covers one or more services at a time. However, two different transactions may cover same services.
Now, it's pretty easy to consolidate this using relational database design. You'd have a table for transactions transaction id, and then some data fields, a table for services service id, some data fields and a third table that links transaction id's to service id's. This way, the service data is not duplicated and if you ever needed to, say, retrieve all services implicated in a transaction, you just do two joins transactions join third_table join service.
As far as I understand, ref simply stores the id of the record (object) it's pointing to and implicitly does a join upon deref(). So, say, if any of the transactions had to cover just one of the services, I could use a ref to the service object instead of the service id + an explicit join.
A nested table, as far as I understand, is just a private table that links the id of one object to a whole another object and that is shared among all objects of the type that declared the nested table. This concept also uses joins under the hood, if I'm not being horribly wrong. So if the services were unique among all transactions, I could in principle just use a nested table of services inside the transaction.
However, the problem is that there could be more than one service for every transaction and they could repeat. So the object-oriented solution, in my mind, is a nested table of ref to service inside of the transaction. However, I'm not able to find any clues on the Internet for the syntax for this idea and whether it is at all possible to do this.
Here's Oracle SQL sort of pseudocode (it doesn't work) of what I'm aiming for:
create type Service_Type as object(
id number(5),
-- some other data --
cost number(4),
name varchar2(32),
description varchar2(32)
---------------------
);
/
create table Service_Table of Service_Type (id primary key);
/
create type Service_Reference_Type as ref Service_Type scope is Service_Table;
/
create type Service_Reference_Table_Type as table of Service_Reference_Type;
/
create type Transaction_Type as object(
id number(5),
services Service_Reference_Table_Type,
-- some other data --
name varchar2(32),
description varchar2(32)
---------------------
);
nested table services store as Transaction_Service_Nested_Reference_Table;
/
As a sidenote: I know Object Oriented DBMS is garbage and I'm not planning on using it in any real project. This is for a university assignment (I wonder why they teach this stuff).
I don't think you're too far off, but you don't need Service_Reference_Type, you can create a table of refs in one step:
create type Service_Type as object(
id number(5),
-- some other data --
cost number(4),
name varchar2(32),
description varchar2(32)
---------------------
);
/
create table Service_Table of Service_Type (id primary key);
create type Service_Reference_Table_Type as table of ref Service_Type;
/
create type Transaction_Type as object(
id number(5),
services Service_Reference_Table_Type,
-- some other data --
name varchar2(32),
description varchar2(32)
---------------------
)
/
create table Transaction_Table of Transaction_Type
nested table services store as Transaction_Service_Nested_Reference_Table;
db<>fiddle including inserting and querying the data.
Is there a way one could scope the refs too?
As in the answer #MTO linked to:
alter table Transaction_Service_Nested_Reference_Table
add scope for (column_value) is Service_Table;
db<>fiddle

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 create new schema and list all schema name in oracle

I want to create one new schema in oracle and I used sample code, which is available here
CREATE SCHEMA AUTHORIZATION oe
CREATE TABLE new_product
(color VARCHAR2(10) PRIMARY KEY, quantity NUMBER)
CREATE VIEW new_product_view
AS SELECT color, quantity FROM new_product WHERE color = 'RED'
GRANT select ON new_product_view TO scott
/
But, getting error
ERROR at line 1:
ORA-02421: missing or invalid schema authorization identifier
Also, Please help me how to list name of all available schema. I am using
select username from dba_users;
to list schema, but i think, its not a right approach, because, user and schema has many-to-many relation,which means I can't get all schema name here.
Please help me !!
From oracle documentation:
This statement does not actually create a schema. Oracle Database
automatically creates a schema when you create a user
So you first need to create a User with the schema name
As for your query it's fine, since username list is equal to schema names unavailable
UPDATE: I can't really test it now, but should be something like this:
CREATE USER oe IDENTIFIED BY oePSWRD;
CREATE SCHEMA AUTHORIZATION oe
CREATE TABLE new_product
(color VARCHAR2(10) PRIMARY KEY, quantity NUMBER)
CREATE VIEW new_product_view
AS SELECT color, quantity FROM new_product WHERE color = 'RED'
GRANT select ON new_product_view TO scott;
From the docs: http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_6014.htm
The schema name must be the same as your Oracle Database username.
Do you want to find all users, or all users for which a table (for example) exists? If the latter then ...
select distinct
owner
from
dba_tables
where
owner not in ('SYS','SYSTEM')
Add in other usernames that you're not interested in listing as required.

How to Create a Foreign Key from a Table to a Custom Datatype

Working with Oracle Express 11g, learning the interactions for object-relational databases.
Running into trouble while trying to create a foreign key from a normal table, to a table of a custom object (customers)
The customer object is as follows (customer_id NUMBER, fname VARCHAR2, lname VARCHAR2), for our setup, we wish to join a table Applications via customer_id NUMBER. Within the SQL Workshop, selecting the customers table is not even an option, and we cannot find syntax to manually create this relationship.
Any experts have some clues to help figure this one out?
create or replace type customer is object
(
customer_id NUMBER,
fname VARCHAR2(100),
lname VARCHAR2(100)
);
/
create table customers of customer
(
constraint customer_pk primary key(customer_id)
);
create table applications
(
application_id number,
customer_id number,
constraint applications_fk1 foreign key (customer_id)
references customers(customer_id)
);

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.

Resources