Thursday 25 April 2013

Creating Database Views


Introduction

Database views are used to combine application data often distributed over several tables. The structure of such views is defined by specifying the tables and fields that are required. Fields which are not required can be hidden, thereby minimizing the interfaces. A view can be used in ABAP programs for data selection.
Following demo shows how to create database view for two database tables having foreign key relationship.
We will be creating a database view for below shown tables YZ14_BANK & YZ14_ACCOUNTS. Note that YZ14_ACCOUNTS is foreign key table (dependent table) and YZ14_BANK is check table (referenced table) for field BRANCH_ID. It should be kept in the mind that we can only include transparent tables in database view.
Steps
1.     Go to transaction SE11 -> select radio button 'View' -> enter the name of the view -> press 'Create'.
  
2.     Below pop up screen will be displayed. Select 'Database View' and press  button.
    
3.     Below screen gets displayed, enter suitable short description.
   
4.     In 'Tables' enter the name of the base tables which we want to include in our view. In this case we will be entering tables as YZ14_BANK & YZ14_ACCOUNTS.
5.     Next, we need to link the entered tables by specifying the fields in join condition. We can also derive the join conditions from existing foreign keys between the base tables of the view. To do this, position the cursor on the table names and click on  present at the bottom.  
6.     Below pop up comes wherein linked tables are present. Select the tables and press 'Copy'.  
   
7.     On clicking copy button, join condition will be derived from the base tables. In our case, we have the below shown conditions.  
 
8.     In the 'View Flds' tab, we need to enter all the fields, we want in our view from the database tables. We can either enter the fields directly or we can copy them from base tables. Later can be achieved by pressing  button present in the tab 'View Flds'. We can also include complete table in a view by entering * in 'View field' & table name in 'Table'. If fields are inserted or deleted from this table, similar modification will be automatically made in view structure.
 
9.     On pressing 'Table fields' button we get a popup likewise shown below. Choose the table whose fields we want to include in our view. Another popup will be shown select the fields we want to include and press 'Copy'.
10.  We repeat the above step to include below shown fields from table YZ14_ACCOUNTS.
 
11.  In above steps do not include 'MANDT' & 'BRANCH_ID' twice. While activating the view, it will lead to an error.  
12.  As shown, we have the below fields in our view.  
13.   We can also formulate the selection criteria using 'Selection Conditions' tab likewise coded below. Here we have entered the condition for account type and status. All the data in the base table satisfying the below condition will be selected based on other condition present.
14.  Now save the view by pressing  button present in standard tool bar. Then activate the view with  present in application tool bar. Once view got activated, press  to see the contents. Below screen gets displayed, do not enter any value, just execute it.
  
15.  Because of the condition we have put on acc_typ & status fields, entries in the base table got filtered. We can see that all the entries have account type as 'CURRENT' & status as 'X'.
 Summary
Above steps demonstrate how to create database view from multiple tables having foreign key relationships.

No comments:

Post a Comment