Index Vs Primary Key of a table/Internal table
Hello Friends,
What is the difference between Key and Index of a table?
What is the difference between creating an index and specifying "Is Unique" to "Yes" vs creating a Unique Key and specifying "Is Unique" to "Yes"?
Thanks
Simmi
Hi,
Check these posts:
http://saptechnicalinfo.blogspot.com/2008/08/table-types-in-sap-abap.html
http://blog.sqlauthority.com/2007/04/26/sql-server-difference-between-unique-index-vs-unique-constraint/
http://www.w3schools.com/Sql/sql_create.asp
http://drsql.spaces.live.com/Blog/cns!80677FB08B3162E4!1187.entry
Similar Messages
-
Concatenate Primary Key Fields of an internal table into a String.
Hi,
I have a dynamic internal table created.
In the dynamic internal table,
How to concatenate all the primary keys of that row concatenated into a string and non primary keys into a different string.
Can some one help me on this.
E.g:
Itab1 has 3 Primary fields and 3 Non primary key fields and it has 2 records.
Table col1 col2 col3 col4 col5 col6
Row1 AA AB AC 1 2 3
Row2 ZA ZB ZC 1 2 3
The following is needed.
Itab2 will have the concatenate strings
Key Nkey
Row1 AAABAC 010203
Row2 ZAZBZC 010203
Thanks,
ChaithanyaC,
Try something like this - Use an ASSIGN in a LOOP:
data: lv_PK type string.
data: lv_non_PK type string.
field-symbols: lv_str type string.
assign local copy of initial line of gt_table to <ls_table>.
loop at gt_table. " this is your table.
do n times. " n = 6 in your case
if n < 4.
assign component sy-index of structure <ls_table> to <lv_str>.
concatenate <lv_str> to lv_PK.
else.
assign component sy-index of structure <ls_table> to <lv_str>.
concatenate <lv_str> to lv_Non_PK.
endif.
enddo.
append <ls_table> to <gt_table>.
endloop. -
NULL in primary keys NOT logged to exceptions table
Problem: Inconsistent behavior when enabling constraints using the "EXCEPTIONS INTO" clause. RDBMS Version: 9.2.0.8.0 and 10.2.0.3.0
- NULL values in primary keys are NOT logged to exceptions table
- NOT NULL column constraints ARE logged to exceptions table
-- Demonstration
-- NULL values in primary keys NOT logged to exceptions table
TRUNCATE TABLE exceptions;
DROP TABLE t;
CREATE TABLE t ( x NUMBER );
INSERT INTO t VALUES ( NULL );
ALTER TABLE t
ADD ( CONSTRAINT tpk PRIMARY KEY (x) EXCEPTIONS INTO exceptions );
SELECT * FROM exceptions; -- returns no rows
-- NOT NULL column constraints logged to exceptions table
TRUNCATE TABLE exceptions;
DROP TABLE t;
CREATE TABLE t ( x NUMBER );
INSERT INTO t VALUES ( NULL );
ALTER TABLE t MODIFY ( X NOT NULL EXCEPTIONS INTO EXCEPTIONS );
SELECT * FROM exceptions; -- returns one row
I would have expected all constraint violations to be logged to exceptions. I was not able to find any documentation describing the behavior I describe above.
Can anyone tell me if this is the intended behavior and if so, where it is documented?
I would also appreciate it if others would confirm this behavior on their systems and say if it is what they expect.
Thanks.
- Doug
P.S. Apologies for the repost from an old thread, which someone else found objectionable.I should have posted the output. Here it is.
SQL>TRUNCATE TABLE exceptions;
Table truncated.
SQL>DROP TABLE t;
Table dropped.
SQL>CREATE TABLE t ( x NUMBER );
Table created.
SQL>INSERT INTO t VALUES ( NULL );
1 row created.
SQL>ALTER TABLE t ADD ( CONSTRAINT tpk PRIMARY KEY (x) EXCEPTIONS INTO exceptions );
ALTER TABLE t ADD ( CONSTRAINT tpk PRIMARY KEY (x) EXCEPTIONS INTO exceptions )
ERROR at line 1:
ORA-01449: column contains NULL values; cannot alter to NOT NULL
SQL>SELECT * FROM exceptions;
no rows selected
SQL>
SQL>TRUNCATE TABLE exceptions;
Table truncated.
SQL>DROP TABLE t;
Table dropped.
SQL>CREATE TABLE t ( x NUMBER );
Table created.
SQL>INSERT INTO t VALUES ( NULL );
1 row created.
SQL>ALTER TABLE t MODIFY ( X NOT NULL EXCEPTIONS INTO EXCEPTIONS );
ALTER TABLE t MODIFY ( X NOT NULL EXCEPTIONS INTO EXCEPTIONS )
ERROR at line 1:
ORA-02296: cannot enable (MYSCHEMA.) - null values found
SQL>SELECT * FROM exceptions;
ROW_ID OWNER TABLE_NAME CONSTRAINT
AAAkk5AAMAAAEByAAA MYSCHEMA T T
1 row selected.
As you can see, I get the expected error message. But I only end up with a record in the exceptions table for the NOT NULL column constraint, not for the null primary key value. -
Adding 2 new fields as Primary Key field in a Z Table which is existing PRD
Hi Friends,
I have to add two new fields as primary key fields in a Z TABLE, which is existing in Quality and Production Systems with Data.
If I incorporate these two new fields (Primary Key fields) in development and if the TR is moved to Quality and Production sytems, is there any possibility of loss in data will happen at Quality and Prd systems?
At present, Table is having 20 fields with 2 primary key fields and i have to add 2 more primary key fields.
I have checked in SCN and not find relevant threads.
Please help me out.
Regards,
Suresh.NO . It wont be a problem .
For ex :
If you have VBELN , POSNR are key fields now , you have an unique reord with that combination .
If you add one other field in it fo ex VKBUR then records will be like this
VBELN POSNR VKBUR MATERIAL QTY
10020 10 abcxyz 1
10020 10 1234 abcxyz 1
So your previous records before adding new primary keys , will have new fields balnk , and the new records will have data filled up in all primary key fields .
However , if you try to update the existing records that will be in existing PRIMARYKEY combination only .
for example if you try update record 1 above with VKBUR value 85858 , it creates a new record , it wont be updated in existing record.
Hope this helps , Pls let me know if u have any more doubts. -
How to know primary key column name form a table name in sql query
Suppose I only know the table name. How to get its primary key column name from the table name?
ThanksViews don't have primary keys though their underlying tables might. You'd need to pick apart the view to determine where it's columns are coming from.
You can select the text of the view in question from user_views. -
Function Based Indexes in Primary Key Definition
Hello.
I have been looking into function based indexes. I have been able to create them using unique indexes. However, when I try to create them on primary keys, the engine complains.
Any ideas? Does anyone know if you can create function based indexes on primary keys?
An example would be great!
Thanks,
BradHi,
You can create a function-based Index by going to the Properties dialog for the Index in the Relational model (not the Physical model). If you select the Index Expression tick box, you can then enter the expression into the Expression text area.
David -
Knowing the primary key columns of the given table
Hi,
How can I get the primary key columns of the given table?
Regards,
Sachin R.K.You can find the constraint_name from all_constraints/user_constraints for constraint_type = 'P' (which is the primary key constraint).
And then see which columns are in for the constriant_name
in all_cons_columns/user_cons_columns view.
Below is the example
select acc.column_name from
all_cons_columns acc, all_constraints ac
where acc.constraint_name = ac.constraint_name
and acc.table_name = 'DEPT' AND acc.owner = 'SCOTT'
and ac.constraint_type = 'P'
Hope this helps
Srinivasa Medam -
How add primary key constraint to already existing table with data
I want apply primary key constraint to already existing table with data
is there any command or way to doAlternatively, assuming you want to ensure uniqueness in your primary key column you can do this:
alter table <table name> add constraint <cons name> primary key (col1,col2) exceptions into <exception_table>
If the altter table statement fails this will populate the EXCEPTIONS table with the rows that contain duplicate values for (col1,col2).
You will need to run (or get a DBA to run) a script called UTLEXCPT.SQL (which will be in the $ORACLE_HOME/rdbms/admin directory) if you don't already have an EXCEPTIONS table.
Cheers, APC -
Hi Friends,
I am confused about primary keys.
What is the purpose of this key again? I know it is used for unique constraints.
Supposing I have a table with two (2) columns which are each indexed as unique.
Then they can me both candidate at primary key right?
So why do I need a primary key again? when I have 2 columns which are uniquely index?
Thanks a lotA UNIQUE index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. This constraint does not apply to NULL values except for the BDB storage engine. For other engines, a UNIQUE index allows multiple NULL values for columns that can contain NULL
The differences between the two are:
1. Column(s) that make the Primary Key of a table cannot be NULL since by definition; the Primary Key cannot be NULL since it helps uniquely identify the record in the table. The column(s) that make up the unique index can be nullable. A note worth mentioning over here is that different RDBMS treat this differently –> while SQL Server and DB2 do not allow more than one NULL value in a unique index column, Oracle allows multiple NULL values. That is one of the things to look out for when designing/developing/porting applications across RDBMS.
2. There can be only one Primary Key defined on the table where as you can have many unique indexes defined on the table (if needed).
3. Also, in the case of SQL Server, if you go with the default options then a Primary Key is created as a clustered index while the unique index (constraint) is created as a non-clustered index. This is just the default behavior though and can be changed at creation time, if needed.
So, if the unique index is defined on not null column(s), then it is essentially the same as the Primary Key and can be treated as an alternate key meaning it can also serve the purpose of identifying a record uniquely in the table. -
Key column in an internal table?
Hi all,
Can somebody tell me, if it is possible to create an internal table with a key-column?
I think it is, but I do not know how? So, How can I make it? Does this keys work exactly like DB table keys?
Thanks,
MPMI'm not sure if it is correct that sorted tables can only have unique keys.
The table keys are important for access:
1. Every table with character fields have got a key by (SAP) definition. An unsorted standard table's key consists of all non-numeric fields. In this case the access will be sequential except for a binary search.
2. Sorted tables may have one unique or non-unique key consisting odf one or more fields. If the key is not unique, the entry with the lowest index is accessed.
3. Only Hash tables require a unique key
4. Only one key per internal table is allowed (no secondary keys as in other programming languages or database definitions).
5. See also:
http://help.sap.com/saphelp_47x200/helpdata/en/90/8d7325b1af11d194f600a0c929b3c3/frameset.htm
regards,
Clemens -
Performance: index vs. primary key
For reasons beyond my control, I have an Oracle database with no primary keys. Fields that would likely be primary keys, however, are indexed.
Is there any reason to believe that database performance will improve if I do add primary keys?
If yes, why?
Thank you!
-BrentPrimary keys provide unique values and an in built index on thecolumn. Since the fields are indexed converting them to primary keys shall not affect the performance.Yes, if u want unique values they can be defined as Primary Keys.
However if these fields are VARCHAR2 with large size then an index will not improve the performance. You can look at creating new PK as numeric fields with a Sequence.
<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by Brent Christensen ([email protected]):
For reasons beyond my control, I have an Oracle database with no primary keys. Fields that would likely be primary keys, however, are indexed.
Is there any reason to believe that database performance will improve if I do add primary keys?
If yes, why?
Thank you!
-Brent<HR></BLOCKQUOTE>
null -
Create a materized view without primary key constraint on the base table?
Hi
I tried to create a materized view but I got this error:
SQL> CREATE MATERIALIZED VIEW TABLE1_MV REFRESH FAST
START WITH
to_date('04-25-2009 03:00:13','MM-dd-yyyy hh24:mi:ss')
NEXT
sysdate + 1
AS
select * from TABLE1@remote_db
SQL> /
CREATE MATERIALIZED VIEW TABLE1_MV REFRESH FAST
ERROR at line 1:
ORA-12014: table 'TABLE1' does not contain a primary key constraint.
TABLE1 in remote_db doesn't have a primary key constraint. Is there anyway that I can create a materized view on a base table which doesn't have a primary key constraint?
Thanks
LizHi,
Thanks for your helpful info. I created a materialized view in the source db with rowid:
SQL> CREATE MATERIALIZED VIEW log on TABLE1 with rowid;
Materialized view log created.
Then I created a MV on the target DB:
CREATE MATERIALIZED VIEW my_schema.TABLE1_MV
REFRESH FAST
with rowid
START WITH
to_date('04-25-2009 03:00:13','MM-dd-yyyy hh24:mi:ss')
NEXT
sysdate + 1
AS
select * from TABLE1@remote_db
SQL> /
CREATE MATERIALIZED VIEW my_schema.TABLE1_MV
ERROR at line 1:
ORA-12018: following error encountered during code generation for
"my_schema"."TABLE1_MV"
ORA-00942: table or view does not exist
TABLE1 exists in remote_db:
SQL> select count(*) from TABLE1@remote_db;
COUNT(*)
9034459
Any clue what is wrong?
Thanks
Liz -
How to find out the primary key column of a database table?
Hi
Given the following scenario :
Given an inputfield, the user can enter a table name. The code behind will base on the table name given and extract out the fieldname of the primary key and concatenate the two field to become a unique string.
Eg. Order ID and Product ID make out a primary key.
How do i achieve that? Any code sample?
Regards,
RaydenCheck the below code :
REPORT zTest no standard page heading.
data : i_DD03P like DD03P occurs 0 with header line.
data v_table like DCOBJDEF-NAME.
parameters : p_table like dd02l-tabname.
start-of-selection.
v_table = p_table.
CALL FUNCTION 'BDL_DDIF_TABL_GET'
EXPORTING
NAME = v_table
STATE = 'A'
LANGU = 'E'
IMPORTING
GOTSTATE =
DD02V_WA =
DD09L_WA =
TABLES
DD03P_TAB = i_DD03P
DD05M_TAB =
DD08V_TAB =
DD12V_TAB =
DD17V_TAB =
EXCEPTIONS
ILLEGAL_INPUT = 1
OTHERS = 2
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
loop at i_DD03P.
if i_dd03p-KEYFLAG = 'X'.
write:/ 'Key fields', i_dd03p-FIELDNAME.
endif.
endloop.
Thanks
Seshu -
Dynamic SQL Joining between tables and Primary keys being configured within master tables
Team , Thanks for your help in advance !
I'm looking out to code a dynamic SQL which should refer Master tables for table names and Primary keys and then Join for insertion into target tables .
EG:
INSERT INTO HUB.dbo.lp_order
SELECT *
FROM del.dbo.lp_order t1
where not exists ( select *
from hub.dbo.lp_order tw
where t1.order_id = t2.order_id )
SET @rows = @@ROWCOUNT
PRINT 'Table: lp_order; Inserted Records: '+ Cast(@rows AS VARCHAR)
-- Please note Databse names are going to remain the same but table names and join conditions on keys
-- should vary for each table(s) being configured in master tables
Sample of Master configuration tables with table info and PK Info :
Table Info
Table_info_ID Table_Name
1 lp_order
7 lp__transition_record
Table_PK_Info
Table_PK_Info_ID Table_info_ID PK_Column_Name
2 1 order_id
8 7 transition_record_id
There can be more than one join condition for each table
Thanks you !
Rajkumar YeluguHi Rajkumar,
It is glad to hear that you figured the question out by yourself.
There's a flaw with your while loop in your sample code, just in case you hadn't noticed that, please see below.
--In this case, it goes to infinite loop
DECLARE @T TABLE(ID INT)
INSERT INTO @T VALUES(1),(3),(2)
DECLARE @ID INT
SELECT @ID = MIN(ID) FROM @T
WHILE @ID IS NOT NULL
PRINT @ID
SELECT @ID =ID FROM @T WHERE ID > @ID
So a cursor would be the appropriate option in your case, please reference below.
DECLARE @Table_Info TABLE
Table_info_ID INT,
Table_Name VARCHAR(99)
INSERT INTO @Table_Info VALUES(1,'lp_order'),(7,'lp__transition_record');
DECLARE @Table_PK_Info TABLE
Table_PK_Info_ID INT,
Table_info_ID INT,
PK_Column_Name VARCHAR(99)
INSERT INTO @Table_PK_Info VALUES(2,1,'order_id'),(8,7,'transition_record_id'),(3,1,'order_id2')
DECLARE @SQL NVarchar(MAX),
@ID INT,
@Table_Name VARCHAR(20),
@whereCondition VARCHAR(99)
DECLARE cur_Tabel_Info CURSOR
FOR SELECT Table_info_ID,Table_Name FROM @Table_Info
OPEN cur_Tabel_Info
FETCH NEXT FROM cur_Tabel_Info
INTO @ID, @Table_Name
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @whereCondition =ISNULL(@whereCondition+' AND ','') +'t1.'+PK_Column_Name+'='+'t2.'+PK_Column_Name FROM @Table_PK_Info WHERE Table_info_ID=@ID
SET @SQL = 'INSERT INTO hub.dbo.'+@Table_Name+'
SELECT * FROM del.dbo.'+@Table_Name+' AS T1
WHERE NOT EXISTS (
SELECT *
FROM hub.dbo.'+@Table_Name+' AS T2
WHERE '+@whereCondition+')'
SELECT @SQL
--EXEC(@SQL)
SET @whereCondition = NULL
FETCH NEXT FROM cur_Tabel_Info
INTO @ID, @Table_Name
END
Supposing you had noticed and fixed the flaw, your answer sharing is always welcome.
If you have any question, feel free to let me know.
Eric Zhang
TechNet Community Support -
Primary key in fact/Dimention/SID table?
Hello sap gurus,
2 Questions....
Which is primary key in
-Fact table (probably answer is .... combination of DIM_ID keys)
-Dimention table ( probably answer is .... DIM_ID key)
-SID table ( probably answer is .... SID_ID key)
Thnx in advanceGaurav,
You already have the answers. A very simple way to figure out the keys is to use the transaction LISTSCHEMA, it will list the tables in an Infocube. Once you have the tables , you can use se11/se12 to check the keys.
For ODS information in BI 7, check the link below.
http://help.sap.com/saphelp_nw70/helpdata/en/80/1a60cae07211d2acb80000e829fbfe/frameset.htm
For ODS information in BW 3.5 , check the link below
http://help.sap.com/saphelp_nw04/helpdata/en/c0/99663b3e916a78e10000000a11402f/frameset.htm
-Saket -
Primary key deletion in custom database table
hi,
I've created a ztable.In which i've have created primary key and foreign key.
I've activated the table.Now my requirement is that i want to delete the primary key as i've shoosen wrong field as primary key.
For that first of all i deleted the foreign key relationship than i have unchecked the primary key but it is giving me an error.
even though if i deleted that field having primary key it is also giving me error as "Primary key change not permitted for value table.
Please help me out if possible.
waiting for ur response.
Thanks,
DheerajMake sure u dont have any data in the table..u have to delete all the data..Also make sure you are not using that table any where else.do a where used list in the table.
Maybe you are looking for
-
Hello a couple of weeks ago i renewed my icloud 15gb but yesterday i increased it to 28gb and I need to apply for a refund of the difference. My account is based in the UK. Please advise how i can go about doing this. Thanks
-
Regarding purchase order number
Hi Just basic question in Sales document. While creating Sales order, we are giving purchase order number in header. why its required. Can somebody explain me... Regards Chandra
-
I am moving house in 3 weeks and sorted out my BT move online last week. I was a bit annoyed at having to go into a new 12 month contract with the phone line and even more annoyed to find that recontracting my BB to the same period made me lose the d
-
Add page on logon page from third party system and from KM
Hi All, I have one requirement where i need to embade other html file in logon page of Portal. Where the pages will come from othere server as well as from KM. VS
-
Anyone else checked their financial reports yet? Mine are all wrong?!
Hi everyone... the first finacial reports are coming in and I had a quick look at them. In my reports the total units sold just don't add up to the units previously reported. There is a gap of 10-30% missing units How can this be? Furthermore, the re