(Oracle DB) - 10. View

kynoh·2023년 3월 2일
0

Oracle Database

목록 보기
14/30
/* View */
	/*
	#	Database Object for selecting data from one or more tables, or other view.
	#	Actual data is stored in the referenced table which is selected by view,
		but view can be used as the referenced table.
	#	Can be used as a new view with referencing other view(s)
	#	'Selecting data' is meaningful function of view,
		so the definition of view is made of SELECT statements.
	*/

/* CREATE View */
	
	-- Why View Is Needed?
SELECT
	a.employee_id,
	a.emp_name,
	a.department_id,
	b.department_name
	FROM employees a, departments b
WHERE a.department_id = b.department_id
;	/*
	Table 'employee' doesn't have the information about department_name,
	'departments' have that information. So if we try to check the department
	name of any employees, we need to refer to the table 'departments'
		
	However, it's not a good way to write this long query several times if some people
	try this query. It's NOT efficient!
	*/

CREATE OR REPLACE VIEW emp_dept_v1 AS	-- Creating a view
SELECT
	a.employee_id,
	a.emp_name,
	a.department_id,
	b.department_name
	FROM employees a, departments b
WHERE a.department_id = b.department_id
;

SELECT *
	FROM emp_dept_v1;	-- Use this view!
	
/*
#	For processing and using needed-data from some tables, view is the best way. 
#	View selects only column and data, and hide the source table.
	It's good for security of data.	
*/

/* DROP View */
DROP VIEW emp_dept_v1;
profile
On-orbit

0개의 댓글

관련 채용 정보