Oracle XE 11gR2 where my tables are located? - sql

I have created some tables under a system as sysdba session using
create table mytable
(
ID char(20) not null,
val1 char(150),
val2 numeric(4)
);
when i m trying to query the tables everything works fine but when i m trying to look the contents of all_tables or user_tables with : eg. SELECT table_name from all_tables my tables are nowhere to be found...
Why is that? am i missing something?
What i actualy want to do is to calculate the used blocks from a table i created
and the query i use is :
SELECT blocks - empty_blocks
FROM user_tables
WHERE table_name = 'mytable';
which because of the problem i explained above does not work.

Firstly,
Open cmd and write sqlplus then connect to user system like this system as sysdba,
Secondly,
create a new user TEST with password TEST with this command create user TEST identified by TEST; then grant all privileges to the user like this grant all privileges to TEST;
Finally,
connect to the new user with this command conn TEST/TEST and create your table

Related

Unable to create table from within package

I am attempting to create a package in which I drop and create a table using a CTAS query. This table needs to be refreshed frequently and columns are add/removed all the time from the underlying data. Since the structure of the table is constantly changing, it would be quite cumbersome to update merge/update queries each refresh to account for the new/missing columns. Currently, I have external scripts that do simple drops and creates but I need to centralize this in the database; therefore I am attempting to create a package to do it; however, I am having trouble with privileges.
As proof of concept, the following code works when ran as an anonymous block:
create table my_test_table as select * from dual; --create test table
declare
v_count int;
begin
select count(*) into v_count from all_tab_columns where table_name = upper('my_test_table');
if v_count >= 1 then
execute immediate 'drop table my_test_table';
end if;
execute immediate q'[
create table my_test_table as
select * from dual
]';
end;
select * from my_test_table; -- shows expected results
But when creating a package to do the same thing;
CREATE OR REPLACE PACKAGE test_pkg AS
PROCEDURE test_procedure;
END test_pkg;
CREATE OR REPLACE package body test_pkg as
procedure test_procedure
is
v_count int;
begin
select count(*) into v_count from all_tab_columns where table_name = upper('my_test_table');
if v_count >= 1 then
execute immediate 'drop table my_test_table';
end if;
execute immediate q'[
create table my_test_table as
select * from dual
]';
end test_procedure;
end test_pkg;
/
and testing with the following code:
create table my_test_table as select * from dual; --make sure table exists
execute TEST_PKG.TEST_PROCEDURE; --results in errors
select * from my_test_table; --table does not exist; therefore, DROP statement works but not CREATE
I get the following errors (in regards to executing TEST_PKG.TEST_PROCEDURE):
ORA-01031: insufficient privileges
ORA-06512: at test_pkg, line 15
When testing for the existence of the test table after executing the package, I can see that it no longer exists. This means the DROP statement is working but the CREATE TABLE statement is resulting in the insufficient privileges error.
Any and all insight into what privileges I need to create the table from within the package would be immensely helpful.
Create a table in procedure is only alowed when you have "Create table" or "create any table" privilege but granted directly to user (granted by role is not working).
https://docs.oracle.com/cd/B19306_01/network.102/b14266/authoriz.htm#i1008334
PL/SQL Blocks and Roles
The use of roles in a PL/SQL block depends on whether it is an
anonymous block or a named block (stored procedure, function, or
trigger), and whether it executes with definer's rights or invoker's
rights.
Named Blocks with Definer's Rights
All roles are disabled in any named PL/SQL block (stored procedure,
function, or trigger) that executes with definer's rights. Roles are
not used for privilege checking and you cannot set roles within a
definer's rights procedure.
To check system privileges granted directly to your user (not by role/roles), you can run this query from your user:
SELECT * FROM USER_SYS_PRIVS;
The package you've created, in the absence of a AUTHID CURRENT_USER clause is a definer's rights package. It can only do things that are allowed by privileges granted directly to the definer of the package. "Directly" is the key point here -- privileges granted through enabled roles are not honored during the package execution.
You've probably got the RESOURCE or similar role enabled for your user, which would explain why you can create the table during testing but not via your package procedure.
Try granting the CREATE TABLE and UNLIMITED TABLESPACE system privileges directly to your user and then recreate the package. (If that works, replace UNLIMITED TABLESPACE with quotas on the appropriate tablespace(s) in your database).

Trigger Oracle - Cant capture the userinformation who change the table

I was trying to capture the user who fires [dml-operation] in the table_name from any schema. While trying to do so when I wrote the following code its captures the wrong osuser.
Code
create ore replace trigger trigger_name
after insert on table_name
for each row
declare
v_username varchar2(20);
v_osuser varchar2(20);
begin
select distinct osuser, username into v_osuser, v_username from v$session where osuser in ( select sys_context('USERENV', 'os_user') from dual;
insert into audit_table values (v_osuser, v_username);
end;
/
How can I modify this code so that I can address/solve this issue?
Note:
I am using the trigger in one server, and calling from the other server. Is there any way we can store the user information of the calling server. currently, it is returning the user information from the trigger defined server.
Thank You.
Try using this code:
select osuser, username
from v$session
where sid=(select sid from v$mystat where rownum=1);
use sys_context('userenv','CURRENT_SCHEMA') and sys_context('userenv','OS_USER')
to get the schema name/os user. No need to do any "select into" or declare any local variables
CREATE OR REPLACE TRIGGER trigger_name AFTER
INSERT ON table_name
FOR EACH ROW
BEGIN
INSERT INTO audit_table VALUES (
sys_context(
'userenv','OS_USER'
),
sys_context(
'userenv','CURRENT_SCHEMA'
)
);
END trigger_name;
/
Might be worth reading the docs on sys_context
When an operation is executed over a db link, the "user" is the user defined in the db link, not the user that is invoking the db link. For instance, database MYDB
CREATE PUBLIC DATABASE LINK "MYLINK"
CONNECT TO "SCOTT" IDENTIFIED BY "TIGER"
USING 'YOURDB';
Now, when user FRED executes the following:
select empno from emp#mylink;
Then, in database 'YOURDB', the operation is being executed by YOURDB's user SCOTT, not by MYDB's user FRED.

Use Query result in ALTER statement (Oracle)

I am trying to create a script to update passwords for a large number of users listed in a given table.
alter user FOO identified by FOOWORD;
I can call the usernames via the following statement:
select owner from usertable_verson where rownum = 1
Is there a way to combine these two statements, so that the alter user command works for each result of the select command?
The eventual goal is to create a loop for each username in the selected column, and apply the password change to each.
you can do this via dynamic SQL
smth like this:
begin
for rc in (select owner from usertable_verson) loop
execute immediate 'alter user '||rc.owner||' identified by FOOWORD';
end loop;
end;

How can i give grant access to specific user-role for a frquently dropped and created table in Vertica?

I have a table which is frequently dropped and created with the same name in a schema. How can i create grant access to a user-role when the table is created in Vertica?
P.S: This question is specifc to vertica, but all ideas are welcome. Also dropping is business requirement and we cannot even truncate the table and clear data out.
Unfortunately Vertica does not support a fixed role that can run a SELECT statement against any table or view in the database(SQL Server db_datareader roles for example).
So depending on how you create the object(DBA creation,workflow or any ETL tool), you need to automate this.
You can create a UDP to do this for you and just call the UDP in your job,script,workflow.
Also you can create a UDP to extract the actual grants,access to the object and also stores it in a temp file to be executed after object recreation.
But the question is ? is recreating the table needed ? What is the use of this ? Maybe there is a better solution !
And #Kermit you gotta stop down voting !
You'll have to do it in steps when you recreate the table.
1) Generate the GRANT script based on the existing grants. (You'll need to change this to match how you handle grants)
select 'GRANT ' || privileges_description || ' ON ' || object_schema || '.' || object_name || ' TO ' || grantee || ';' from grants
where object_schema = 'MYSCHEMA' object_name = 'MYTABLE'
and grantee ilike 'role%';
2) Create the new table like the old one (be sure to save your projections!)
CREATE TABLE MYSCHEMA.NEW_MYTABLE LIKE MYSCHEMA.MYTABLE INCLUDING PROJECTIONS;
3) Drop old table
DROP TABLE MYSCHEMA.MYTABLE;
4) Rename new table
ALTER TABLE MYSCHEMA.NEW_MYTABLE RENAME TO MYTABLE;
5) Apply generated grants
GRANT ROLE_MYROLE TO MYSCHEMA.MYTABLE;

Allow only adding of column/table/view in Oracle SQL

Suppose I have a platform which allows users to enter some SQL queries and execute them.
DB is Oracle 11.2
Is there any way to limit users' operations to CREATE TABLE, CREATE VIEW and ALTER table ADD COLUMN?
The only way I see is to parse all user provided queries with the grammar for Oracle DB 11.2. But this way is a very tedious one since there is no complete grammar in a free access (at least I couldn't find one) and implementing one will take days, if not weeks.
Grant CREATE TABLE and CREATE VIEW privileges.
For ALTER TABLE, to limit just ADD COLUMN, it's possible to create procedure and grant EXECUTE:
create or replace add_column( tableName varchar2, columnDefinition varchar2 )
as
execute immediate 'alter table '|| tableName || ' add column ' || columnDefinition;
end;
/
(not tested)
and
GRANT EXECUTE on ADD_COLUMN to user2;