Wednesday, 4 October 2017

ABAP: Database Table

Database table is a collection of records, where each record is a collection of fields. You can define database tables using built-in types (or) by using data elements.

I am going to show you how to create below tables.

ZStudent
student_id
first_name
last_name
primary_email
secondary_email
phone_number
date_of_join








ZParent
parent_id
first_name
last_name
primary_email
secondary_email
phone_number







ZTeaching_Faculty
faculty_id
first_name
last_name
primary_email
work_email
phone_number
date_of_join
Salary









ZNon_Teaching_faculty
non_teaching_faculty_id
first_name
last_name
primary_email
work_email
phone_number
date_of_join
Salary









ZFaculty_student
faculty_id
student_id
Year




        
ZFtudent_parent
student_id
parent_id


        
ZFaculty_rating
faculty_id
student_id
faculty_rating




ZStudent_rating
student_id
faculty_id
student_rating



I am going to define the above tables using the data elements that I defined in my previous post.

Create Student table
Field
Data Element
student_id
ZSTUDENT_ID
first_name
ZSTUDENT_FIRST_NAME
last_name
ZSTUDENT_LAST_NAME
primary_email
ZSTUDENT_PRIMARY_EMAIL
secondary_email
ZSTUDENT_SECONDARY_EMAIL
phone_number
Z_PHONE_NUM
date_of_join
ZSTUDENT_DATE_OF_JOIN
                                                  
Use the transaction ‘SE11’ to open ABAP dictionary.

Select the radio button ‘Database table’ and give the table name as ZSTUDNT.
Click the button ‘Create’.

It opens below screen.
Update the ‘Short Description’ as ‘Student information’.

Click on the button ‘Delivery Class’ and choose ‘Application Table’.
Select the option ‘Delivery/Maintenance Allowed’.
Go to the Fields section by clicking on the tab ‘Fields’.

Specify the fields and data elements here.
Press Enter after updating the fields and data elements. While working with application tables, Your table first field must be MANDT. Since Application tables are client dependent, we require to add the field ‘MANDT’.

What is client dependent?
The date inserted into the table from one client can be accessed from that client only. Suppose if you inserted student details with id ‘12345’,  from the client 234, then the data with id ‘12345’ can be accessed from the client 234 only.
Mark the MANDT, STUDENT_ID as primary key by selecting the checkbox under the section Key.
Save the data.

Set the enhancement category to this table
Extras -> Enhancement Category.
Press Ok button.
Select the option ‘Can be Enhanced (Deep)’ and press the button Copy.

Save the settings.

Updating Technical Settings
Click on the button ‘Technical Setting’ on tool bar.

Click on Data Class section, and select the class ‘APPL0’.
Select the size category. I choose the size category as 1.
Save the settings and activate the table.

Create table ZTEACHINGFACULTY
faculty_id
first_name
last_name
primary_email
work_email
phone_number
date_of_join
salary









Field
Data Element
faculty_id
ZFACULTY_ID
first_name
ZFACULTY_FIRST_NAME
last_name
ZFACULTY_LAST_NAME
primary_email
ZFACULTY_PRIMARY_EMAIL
work_email
ZFACULTY_WORK_EMAIL
phone_number
Z_PHONE_NUM
date_of_join
ZFACULTY_DATE_OF_JOIN
salary
ZSALARY


Procedure is same like creating ZSTUDNT.

In my next post, I am going to show you how to define ZFaculty_rating and ZStudent_rating tables by maintaining foreign key constraint.

Previous                                                 Next                                                 Home

Tuesday, 3 October 2017

ABAP: Table Type

What is Table Type?
Table types are used to define internal tables. When you create a table type in the ABAP dictionary, you should specify the line type, access type and key.

Internal table used to store table data in working memory (RAM) in ABAP. If you are from Java programming, you can assume it is like a collection.

Suppose you want toget all the students information whose rating is Good (or) Excellent from the table ‘ZSTUDENT_RATING’ and store it in an internal table ‘ZSTUDENT_RATING_INTERNAL’.
You need to set below things while defining table types.

a.   Specify the line type: it is the structure of table type
b.   Access mode
c.   Set the keys (primary key and secondary key) to the table type.

What is line type?
It is any data type from ABAP dictionary, it can be a data element, structure, table type (or) type of a database table.

How to define line type?
You can define line type using existing built in types (CHAR, INT4, DEC etc.,), ABAP Dictionary types (Data elements, structures, table types, database tables, views), reference types (Using class or interface).

What is Access mode?
It specifies how the data is accessed and managed in internal table while performing CRUD operations. It can be Standard table (Data arranged sequentially, search is not efficient, since it uses linear search), Hashed table (Data arranged using hash function, it do not support indexing), Index table, sorted table (Data arranged using key, search is efficient since it uses binary search) etc.,

I am going to show you, how to create table type ‘ZSTUDENT_TT’, to hold student information using the structure 'ZSTUDENT_STR' that I created in my previous post.

Creating table type ‘ZSTUDENT_TT’
Use the transaction ‘SE11’ to open ABAP Dictionary.

Populate Data type field with ‘ZSTUDENT_TT’ and click on Create button.
Select the radio button ‘Table type’ and press ok.
It opens below window.
a. Update Short description as ‘Table type to store student information’.
b. Give the Line Type as 'ZSTUDENT_STR'. ‘ZSTUDENT_STR’ is structure I created in my previous post.
Click on ‘Initialization and Access’ tab.

Give ‘Initial Number of Rows’ as 25’, it is used to allocate initial memory for an internal table. This memory will increase dynamically based on number of records.

Select the Access mode as ‘Standard Table’.
Click on the save button and activate the table type.

You can create below table types using the corresponding structures we created in my previous post.

Table Type
Structure
ZPARENT_TT
ZPARENT_STR
ZTEACHING_FACULTY_TT
ZTEACHING_FACULTY_STR
ZNON_TECHING_FACULTY_TT
ZNON_TECHING_FACULTY_STR
ZFACULTY_STUDENT_TT
ZFACULTY_STUDENT_STR
ZSTUDENT_PARENT_TT
ZSTUDENT_PARENT_STR
ZFACULTY_RATING_TT
ZFACULTY_RATING_STR
ZSTUDENT_RATING_TT
ZSTUDENT_RATING_STR



Previous                                                 Next                                                 Home

Print all the file names recursively

java.io.File class provides listFiles() method to get all the files presented in a directory. Below application uses listFiles() method and print all the file names in a directory.

FileUtil.java
import java.io.File;

public class FileUtil {

 /**
  * It print all the file names in a directory. if dirPath is null (or) empty
  * (or) not pointing to a directory, it prints nothing.
  * 
  * @param dirPath
  */
 public static void printFileNames(String dirPath) {
  if (dirPath == null || dirPath.isEmpty()) {
   return;
  }

  File file = new File(dirPath);
  if (!file.exists()) {
   return;
  }

  printAllFileNames(dirPath);

 }

 private static void printAllFileNames(String dirPath) {
  File file = new File(dirPath);
  File[] listOfFiles = file.listFiles();

  for (File tempFile : listOfFiles) {
   if (tempFile.isDirectory()) {
    printAllFileNames(tempFile.getAbsolutePath());
   } else {
    System.out.println(tempFile.getName());
   }
  }

 }
}


Sunday, 1 October 2017

ABAP: Structures

Structure is a user defined complex data type in ABAP. By using structure, we can group relevant fields.

For example, we can group the student details like student_id, first_name, last_name, email and phone_num in one structure.

How can we define a structure?
You can define a structure using built in types, data elements, table type, structures etc.,

Can a structure be defined using other structure?
Yes, you can define a structure using other structure.

For example, Address structure has following fields.
         a. Street
         b. City
         c. Country
         d. PIN   
You can define student structure with below fields.
         a. student_id : char(30)
         b. first_name : char(30)
         c. last_name : char(30)
         d. email : char(30)
         e. phone_num : char(20)
         f. address (of type Address structure)

Where can I define structures?
Structures can be defined locally or globally in ABAP Dictionary.

Creating structures
I am going to show, how to create the structure ZSTUDENT_STR using the existing data elements.

COMPONENT
TYPE
STUDENT_ID
ZSTUDENT_ID
FIRST_NAME
ZSTUDENT_FIRST_NAME
LAST_NAME
ZSTUDENT_LAST_NAME
PRIMARY_EMAIL
ZSTUDENT_PRIMARY_EMAIL
SECONDARY_EMAIL
ZSTUDENT_SECONDARY_EMAIL
PHONE_NUMBER
ZPHONE_NUM
DATE_OF_JOIN
ZSTUDENT_DATE_OF_JOIN


Types like ZSTUDENT_ID, ZSTUDENT_FIRST_NAME, ZSTUDENT_LAST_NAME, ZSTUDENT_PRIMARY_EMAIL, ZSTUDENT_SECONDARY_EMAIL, ZPHONE_NUM, ZSTUDENT_DATE_OF_JOIN are the data elements that we created in my previous post.

Open ABAP Dictionary by using the transaction ‘SE11’.

Give the technical name as ‘ZSTUDENT_STR’ in the Data type field, and press Create button.

Select the type as structure.

Press Ok. It opens below screen.
Give the short description as ‘Student structure’.


Update the component and their component types.

Press enter, ABAP system automatically update the semantic information.
Save the structure.

Set the enhancement category.

Go to Extras -> Enhancement Category…
It opens below window, press OK.
Select the option ‘Can Be Enhanced (Deep)’. By selecting the option 'Can be Enhanced (Deep)', I am telling to the ABAP system that the structure can be enhanced in future.
Press copy.
Activate the structure.

You can try creating below structures.

ZPARENT_STR
Component
Component Type
PARENT_ID
ZPARENT_ID
FIRST_NAME
ZPARENT_FIRST_NAME
LAST_NAME
ZPARENT_LAST_NAME
PRIMARY_EMAIL
ZPARENT_PRIMARY_EMAIL
SECONDARY_EMAIL
ZPARENT_SECONDARY_EMAIL
PHONE_NUMBER
ZPHONE_NUM


ZTEACHING_FACULTY_STR
COMPONENT
COMPONENT TYPE
FACULTY_ID
ZFACULTY_ID
FIRST_NAME
ZFACULTY_FIRST_NAME
LAST_NAME
ZFACULTY_LAST_NAME
PRIMARY_EMAIL
ZFACULTY_PRIMARY_EMAIL
WORK_EMAIL
ZFACULTY_WORK_EMAIL
PHONE_NUMBER
Z_PHONE_NUM
DATE_OF_JOIN
ZFACULTY_DATE_OF_JOIN
SALARY
ZSALARY


ZNON_TECHING_FACULTY_STR
COMPONENT
COMPONENT TYPE
NON_TEACHING_FACULTY_ID
ZNON_FACULTY_ID
FIRST_NAME
ZFACULTY_FIRST_NAME
LAST_NAME
ZFACULTY_LAST_NAME
PRIMARY_EMAIL
ZFACULTY_PRIMARY_EMAIL
WORK_EMAIL
ZFACULTY_WORK_EMAIL
PHONE_NUMBER
Z_PHONE_NUM
DATE_OF_JOIN
ZFACULTY_DATE_OF_JOIN
SALARY
ZSALARY

ZFACULTY_STUDENT_STR
COMPONENT
COMPONENT TYPE
FACULTY_ID
ZFACULTY_ID
STUDENT_ID
ZSTUDENT_ID
YEAR_OF_TEACHING
ZYEAR_OF_TEACHING

ZSTUDENT_PARENT_STR
COMPONENT
COMPONENT TYPE
STUDENT_ID
ZSTUDENT_ID
PARENT_ID
ZPARENT_ID

ZFACULTY_RATING_STR
COMPONENT
COMPONENT TYPE
FACULTY_ID
ZFACULTY_ID
STUDENT_ID
ZSTUDENT_ID
FACULTY_RATING
ZFACULTY_RATING
        
ZSTUDENT_RATING_STR
COMPONENT
COMPONENT TYPE
STUDENT_ID
ZSTUDENT_ID
FACULTY_ID
ZFACULTY_ID
STUDENT_RATING
ZSTUDENT_RATING

In this post we created below structures.
ZSTUDENT_STR, ZPARENT_STR, ZTEACHING_FACULTY_STR, ZNON_TECHING_FACULTY_STR, ZFACULTY_STUDENT_STR, ZSTUDENT_PARENT_STR, ZFACULTY_RATING_STR, ZSTUDENT_RATING_STR



Previous                                                 Next                                                 Home