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 - oracle

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 .

Related

How to grant privilege to a row in Oracle?

I'm just start learning about Oracle database. I want to ask that how can I grant privilege to a row. For example: employee A just can read, update info about him and he can't read info about employee B or others
Here is my table Employee
create table Employee(
Emp_ID varchar2(20),
Emp_Name varchar2(255),
primary key(Emp_ID)
)
You can add a Data_Owner field to the table populated by the USER function. Don't allow direct access to the table. All access is through a view that exposes only those rows matching the USER value of whoever queries the view. Use the USER function to control the DML.
Here is a sample. Test to see if it meets your needs
create view Sample as
select col_1, col_2, col3
from Sample_Table
where Data_Owner = USER;

How to join dba_users table with HRMS oracle tables

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.

Hibernate - How to get single value from another table without primary/Foreign key relationship?

I have 2 tables(Employee & Department).
Employee(ID, employee name, dept id, employee address) --> ID primary key
Department(ID, department name, address) --> ID primary key
Note: Consider no primary key / foreign key relationship between these 2 tables
Now based on dept id in Employee, i need to fetch its corresponding dept name from Department & display it in Employee.
Employee(ID, employee name, dept id(Display dept name instead of dept id, query department table to get dept name through passing dept id), employee address)
What are all possible ways to achieve this?
Regards
Raj
I see at least three possible solutions:
Create a "normal" SQL query for this, joins can be perfectly done without a PK/FK-relationship.
Perform two SQL queries: One query for fetching the employee, then extract the ID and then a second query to fetch the department by ID.
Change your JPA entities to include the relationship as you would normally do having a #OneToMany relationship so you can load the employee with its department as usual. Same as 1 applies, joins (whether done by you or by the JPA provider) don't need a PK/FK-relationship.
Whilst 1 can be done with plain SQL, 2 can be done using the entity manager without the need to write "plain" SQL. 3 would be the easiest solution, obviously.
You can #Query in repository and write your JPA query using JOIN.
Sample Query:
#Query( value = "select new map(employee.name as empName, employee.id as id, department.id as depId, department.name as depName) from Employee employee INNER JOIN Department department ON employee.depId = department.id where employee.id = :id" )
public Map<String,Object> getEmployeeById(long id);

Updating rows with same id and name in oracle database

i have a large data inside a table two employees have same name and same id in accident so i want to update one of the two employee so it becomes hard to me do you have any answer or solution
when i write this it updates the two rows
update EMPLOYEE
set id = 2 where Name='ahmed' and id=1
you can use ROWNUM
update EMPLOYEE
set id = 2 where Name='ahmed' and id=1 and ROWNUM = 1

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.

Resources