‎2008 Feb 06 12:07 PM
what is difference between unique and non-unique key?plz explain with e.g.
‎2008 Feb 06 12:10 PM
‎2008 Feb 06 12:10 PM
‎2008 Feb 06 12:16 PM
hi,
The data type of an internal table is fully specified by its:
Line type
The line type defines the attributes of the individual fields. You can specify any ABAP data type.
Key definition
The key fields and their sequence determine the criteria by which the system identifies table lines.
Key type
You can define the key as either unique or non-unique. The uniqueness of the key must be compatible
with the access type you have chosen for the table. If the key is unique, there can be no duplicate
entries in the table.
Access type
Unlike database tables, the system assigns line numbers to certain kinds of internal tables. This means
that you can use the index to access lines as well as the key. We sometimes use the term "table type"
to refer to this.
A unique key is only possible with sorted and hashed tables
With a standard table, inserting an entry has the same effect as appending. With sorted tables with a nonunique
key, the entry is inserted before the first (if any) entry with the same key.
To read individual data records using the first variant, all fields of <wa1> that are key fields of <itab> must
be filled. <wa1> and <wa2> can be identical. If you use the WITH TABLE KEY addition in the second
variant, you must also specify the key fully. Otherwise, the system searches according to the sequence of
fields that you have specified, using a binary search where possible.
t_cust_list TYPE TABLE OF t_cust
WITH NON-UNIQUE KEY id.
If you want to access your data using the index and do not need your table to be kept in sorted order or to
have a unique key, that is, when the sequence of the entries is the most important thing, not sorting by
key or having unique entries, you should use standard tables.
flight_list TYPE SORTED
TABLE OF sflight
WITH UNIQUE KEY carrid connid fldate.
When you choose to use a sorted table, it will normally be because you want to define a unique key. The
mere fact that the table is kept in sorted order is not that significant, since you can sort any kind of internal
table. However, with sorted tables (unlike hashed tables), new data records are inserted in the correct sort
order. If you have a table with few entries but lots of accesses that change the contents, a sorted table
may be more efficient than a hashed table in terms of runtime.
Since the internal table represents a database table in this case, you should ensure that its records have
unique keys.
t_city_list TYPE HASHED
TABLE OF t_city
WITH UNIQUE KEY city country.
The hash algorithm calculates the address of an entry based on the key. This means that, with larger
tables, the access time is reduced significantly in comparison with a binary search. In a loop, however, the
hashed table has to search the entire table (full table scan). Since the table entries are stored unsorted, it
would be better to use a sorted table if you needed to run a loop through a left-justified portion of the key. It
can also be worth using a hashed table but sorting it.
A typical use for hashed tables is to buffer detailed information that you need repeatedly and can identify
using a unique key.
Hope this is helpful, Do reward.
Edited by: Runal Singh on Feb 6, 2008 5:46 PM