How to join dba_users table with HRMS oracle tables - oracle

I have request and i would like assistance. I have created this query:
select username, profile, r.GRANTED_ROLE, decode(account_status,'OPEN','ACTIVE','EXPIRED','EXPIRED','INACTIVE') "ACCOUNT STATUS",created,
s.PTIME "Password Change Time", last_login
from dba_users, dba_role_privs r, sys.user$ s
where username=r.grantee(+)
and username=s.NAME
order by 1;
So i am ok with it, but i wanted to know how can i join personal database accounts with some of Oracle's HRMS table in order to get for them details like email, employee_id etc.

Related

How to find missing grant on all tables for one role

i have some problem on my student Database schema. I want to find with query which Tables don't have: for example 'SELECT' grant to role XXX. Second example is that in Tables i have like Grants for delete,alter but now i want to check all Tables with one query to find which Tables don't have Select grant to role 'STUDENT_DBA' or where this role don't have grant for Select...
Please help 😅😅😅
SELECT table_name
FROM dba_tables
WHERE owner = 'STUDENT'
AND table_name NOT IN
(SELECT table_name
FROM dba_tab_privs
WHERE owner = 'STUDENT'
AND privilege = 'SELECT'
AND grantee = 'STUDENT_DBA');
This will return all tables in the STUDENT schema that do not have select permissions directly granted to the STUDENT_DBA role.

Oracle query I have to fetch the records based on department(admin, user, emp) and respective role(1,2,3,4,5) from employee table

There are two tables 1. employess (column emp_name, emp_dept, emp_address ) 2. department (dept, role)
I have to fetch the records from employee table
based on department(admin, user, emp) and respective role(1,2,3,4,5) from employee and department table.
If employee is admin then records with role 1 and 3 should be fetched if user then only records with role 5.
Please help me to write the query.
Following is the query which I tried:
select emp_name, emp_dept,
(select role from department d where d.dept= e.emp_dept) role, emp_address
from employee e
where role IN (case emp_dept
when 'admin'
then('1','3')
when 'user'
then ('5')
when 'emp'
then('4')
end
)
Could be good to start with a Users table , where you will store all users independently if they are admin or employee . After that try to write a draft of E/R model , to make us understand what you need .

Select from multiple tables oracle

I'm new in Oracle. I have a table that lists tablenames of database. Its name is "AD_Table". I want to select ID table, and createdby from ad_table list. For example in ad_table it has one column name tablename that represents table name in database:
tablename
---------
AD_Tab1
AD_Tab2
AD_Tab3
AD_Tab4
AD_Tab5
AD_Tab6
AD_Tab7
AD_Tab8
AD_Tab9
AD_Tab10
I want query like this :
SELECT
createdby
from (SELECT TABLENAME FROM AD_TABLE)
but it won't work. Can anyone help?
In Oracle you can have many tables with the same name, on different schemas;
assuming that you need to find all the tables, and their owners, whose names are contained in your table, you can try with something like this:
select owner, table_name
from AD_table AD
inner join dba_tables DBA ON ( dba.table_name = UPPER(ad.tableName))
Notice that you need to log in with a user having rights to make a select on DBA_TABLES to run this query.

How do you query for blank schemas?

I'm trying to locate blank schemas within my oracle server.
I've tried using DBA_TAB_COLS, but no success. Eg:
select * from
(
select OWNER, COUNT(distinct(TABLE_NAME)) as TABLE_COUNT
from DBA_TAB_COLS
group by OWNER
)
where TABLE_COUNT = 0
Is there a way to query for a list of blank schemas?
If "blank schema" means a schema that does not own any tables
SELECT username
FROM dba_users u
WHERE NOT EXISTS(
SELECT 1
FROM dba_tables t
WHERE t.owner = u.username
);
If you want to look for schemas that own no object, rather than schemas that own no tables, you'd use dba_objects in the subquery rather than dba_tables. For either of these, though, you'd need to have privileges to query the dba_* objects.

Oracle SQL Query for listing all Schemas in a DB

I wanted to delete some unused schemas on our oracle DB.
How can I query for all schema names ?
Using sqlplus
sqlplus / as sysdba
run:
SELECT *
FROM dba_users
Should you only want the usernames do the following:
SELECT username
FROM dba_users
Most likely, you want
SELECT username
FROM dba_users
That will show you all the users in the system (and thus all the potential schemas). If your definition of "schema" allows for a schema to be empty, that's what you want. However, there can be a semantic distinction where people only want to call something a schema if it actually owns at least one object so that the hundreds of user accounts that will never own any objects are excluded. In that case
SELECT username
FROM dba_users u
WHERE EXISTS (
SELECT 1
FROM dba_objects o
WHERE o.owner = u.username )
Assuming that whoever created the schemas was sensible about assigning default tablespaces and assuming that you are not interested in schemas that Oracle has delivered, you can filter out those schemas by adding predicates on the default_tablespace, i.e.
SELECT username
FROM dba_users
WHERE default_tablespace not in ('SYSTEM','SYSAUX')
or
SELECT username
FROM dba_users u
WHERE EXISTS (
SELECT 1
FROM dba_objects o
WHERE o.owner = u.username )
AND default_tablespace not in ('SYSTEM','SYSAUX')
It is not terribly uncommon to come across a system where someone has incorrectly given a non-system user a default_tablespace of SYSTEM, though, so be certain that the assumptions hold before trying to filter out the Oracle-delivered schemas this way.
SELECT username FROM all_users ORDER BY username;
select distinct owner
from dba_segments
where owner in (select username from dba_users where default_tablespace not in ('SYSTEM','SYSAUX'));
Below sql lists all the schema in oracle that are created after installation
ORACLE_MAINTAINED='N' is the filter. This column is new in 12c.
select distinct username,ORACLE_MAINTAINED from dba_users where ORACLE_MAINTAINED='N';
How about :
SQL> select * from all_users;
it will return list of all users/schemas, their ID's and date created in DB :
USERNAME USER_ID CREATED
------------------------------ ---------- ---------
SCHEMA1 120 09-SEP-15
SCHEMA2 119 09-SEP-15
SCHEMA3 118 09-SEP-15
Either of the following SQL will return all schema in Oracle DB.
select owner FROM all_tables group by owner;
select distinct owner FROM all_tables;

Resources