Difference between alter table statements to add primary keys
Hi,
Can someone explain what the difference between these 2 statements are and if & when one should be used over the other?
Also, are the brackets around the column name necessary?
Thanks!
ALTER TABLE xyz ADD CONSTRAINT id_pk PRIMARY KEY (id);
ALTER TABLE xyz ADD PRIMARY KEY (id);
Hi,
As every one has explained that there is no difference in the actual functioning of the two constraints except that the first statement will create Primary Key constraint with user defined name as id_pk whereas in the second statement Primary key will be created with system generated name like SYS....
Normally name for the constraints are needed when you are working on constraints like need to enable or disable the constraints or need to drop the constraints.
We can say that names for primary key are least required when it comes to the usage except better recognization.
Because we can drop primary key, disable primary key,enable primary key without giving the name for the primary key as there can only be one primary key in the table. So not much an issue with the name.
But as earlier post say its better than nothing,, i will add to it that its almost same as that with names constrained in case of Primary Keys taking into account the usage of the named primary key.
Regards
Similar Messages
-
Difference between DESCRIBE TABLE..... and SY-DBCNT
Hi,
i want to know the difference between DESCRIBE TABLE.. command and SY-DBCNT.hi
good
SY-DBCNT->
SY-DBCNT gives the number of records in database table
After an open SQL statement, the system field sy-dbcnt contains the number of database lines processed.
REPORT ychatest.
DATA : v_matnr LIKE mara-matnr.
SELECT matnr FROM mara INTO v_matnr UP TO 1 ROWS.
ENDSELECT.
WRITE : sy-dbcnt.
DESCRIBE->
DESCRIBE LIST
The DESCRIBE LIST statement allows you to import certain list attributes, such as the number of lines or pages, into program variables.
you need to know the attributes of list levels that were not stored in system variables during list creation, you can use the DESCRIBE LISTstatement.
To retrieve the number of lines or pages of a list, use:
DESCRIBE LIST NUMBER OF LINES|PAGES n ...
To get the page number of a particular line number, use:
DESCRIBE LIST LINE lin PAGE pag ...
To get the properties of a particular page, use:
DESCRIBE LIST PAGE pag ...
Use DESCRIBE LIST for completed lists only, since for lists in creation (index is sy-lsind) some properties are not up to date.
http://help.sap.com/saphelp_nw70/helpdata/en/9f/dba42335c111d1829f0000e829fbfe/content.htm
thanks
mrutyun^ -
Find Differences between two tables at column level
Hi,
I have 2 tables one live table and the other History table..
If i have to find differences between live table and the latest version in the History table and also find which column got chaanged
How would i do that for a table which has many columns and i need each column for which the value has changed for a id
for ex:
Table 1 (LIve) Table 2 (History)
ID col1 col2 Version ID col1 col2 Version
1 ABC 123 V1 1 ABCD 123 v2
2 NBS 1234 V1 2 NBS 123 V2
Result set should be
Result Set:
ID col which changed
1 col1
2 col2
Because the values for that column had been changed
Except gives me all the differences not just the column level ..The dynamic version using schema views... :D
--Build a coulple OF testing tables to play with
CREATE TABLE dbo.Table1 (
ID INT IDENTITY(1,1) PRIMARY KEY,
Col1 INT,
Col2 INT,
Col3 INT
CREATE TABLE dbo.Table2 (
ID INT IDENTITY(1,1) PRIMARY KEY,
Col1 INT,
Col2 INT,
Col3 INT
INSERT dbo.Table1 (Col1,Col2,Col3) VALUES
(123,456,789),
(111,222,333),
(444,555,666),
(777,888,999),
(321,345,769),
(179,753,758),
(362,362,236),
(856,874,896),
(821,729,324)
INSERT dbo.Table2 (Col1,Col2,Col3) VALUES
(123,456,789),
(111,999,333), --col2 diff
(444,555,666),
(777,888,999),
(321,345,123), --col3 diff
(179,753,758),
(362,362,236),
(234,874,896), --col1 diff
(821,729,324)
And then the actual solution...
DECLARE
@t1 VARCHAR(10) = 'Table1',
@t2 VARCHAR(10) = 'Table2'
IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp
SELECT
c.TABLE_SCHEMA,
c.TABLE_NAME,
c.COLUMN_NAME,
c.ORDINAL_POSITION,
CASE WHEN u.COLUMN_NAME IS NOT NULL THEN 1 ELSE 0 END AS PK
INTO #temp
FROM
INFORMATION_SCHEMA.COLUMNS c
JOIN INFORMATION_SCHEMA.TABLES t
ON c.TABLE_NAME = t.TABLE_NAME
AND c.TABLE_SCHEMA = t.TABLE_SCHEMA
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE u
ON c.COLUMN_NAME = u.COLUMN_NAME
AND c.TABLE_NAME = u.TABLE_NAME
AND c.TABLE_SCHEMA = u.TABLE_SCHEMA
AND OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1
WHERE 1 = 1
AND t.TABLE_TYPE = 'BASE TABLE'
AND c.TABLE_NAME IN (@t1,@t2)
ORDER BY
c.TABLE_SCHEMA, c.TABLE_NAME, c.ORDINAL_POSITION
DECLARE @select VARCHAR(MAX)
SELECT @select = COALESCE(@select + ', ', '') + t.TABLE_SCHEMA + '.' + t.TABLE_NAME + '.' + t.COLUMN_NAME
FROM #temp AS t
ORDER BY t.TABLE_NAME, t.ORDINAL_POSITION
DECLARE @from VARCHAR(MAX)
SELECT @from = COALESCE(@from + ' FULL JOIN ', '') + t.TABLE_SCHEMA + '.' + t.TABLE_NAME
FROM #temp AS t
WHERE t.PK = 1
ORDER BY t.TABLE_NAME
DECLARE @on VARCHAR(MAX)
SELECT @on = COALESCE(@on + ' = ', '') + t.TABLE_SCHEMA + '.' + t.TABLE_NAME + '.' + t.COLUMN_NAME
FROM #temp AS t
WHERE t.PK = 1
ORDER BY t.TABLE_NAME, t.ORDINAL_POSITION
DECLARE @where VARCHAR(MAX)
SELECT @where = COALESCE(@where + CASE WHEN t.TABLE_NAME = @t1 THEN ' OR ' ELSE ' <> ' END, '') + t.TABLE_SCHEMA + '.' + t.TABLE_NAME + '.' + t.COLUMN_NAME
FROM #temp AS t
WHERE t.PK = 0
ORDER BY t.ORDINAL_POSITION, t.TABLE_NAME
DECLARE @sql VARCHAR(MAX) = '
SELECT ' + @select + '
FROM ' + @from + '
ON ' + @on + '
WHERE ' + @where
EXEC (@sql)
HTH,
Jason -
SAP HANA - How to run alter table statement in HANA procedure?
I am trying to run alter table statement in a procedure. HANA gives error saying
SAP DBTech JDBC: [257] (at 1338): sql syntax error: ALTER TABLE is not allowed in SQLScript: line 36 col 8 (at pos 1338)
How to run alter table statements in procedure?
Thanks,
Suren.Hi Rich Heilman,
Thanks for your response. I have tried with dynamic SQL. I am trying to add partitions to a non portioned table.
EXECUTE IMMEDIATE 'ALTER TABLE ' || :SCHEMA_NAME || '.TARGET_TABLE PARTITION BY RANGE (TARGET_TYPE_ID) (PARTITION VALUE = 1, PARTITION VALUE = 2, PARTITION VALUE = 3, PARTITION VALUE = 4, PARTITION OTHERS)';
Execution fails with error
Could not execute 'CALL PARTITION_TARGET_TABLE('SUREN_TEST')' in 1.160 seconds .
[129]: transaction rolled back by an internal error: [129] "SUREN_TEST"."PARTITION_TARGET_TABLE": line 53 col 3 (at pos 2173): [129] (range 3)
Any reasons for this error?
Thanks,
Suren. -
Performance for ALTER TABLE statements
Hi,
I'd like to improve performance for scripts running several ALTER TABLE statements. I have two questions regarding this.
This is the original code:
ALTER TABLE CUSTOMER_ORDER_DELIVERY_TAB ADD (
QTY_TO_INVOICE NUMBER NULL );
ALTER TABLE CUSTOMER_ORDER_DELIVERY_TAB ADD (
QTY_INVOICED NUMBER NULL );
1. Would I gain any performance by making the following changes?
ALTER TABLE CUSTOMER_ORDER_DELIVERY_TAB ADD (
QTY_TO_INVOICE NUMBER NULL,
QTY_INVOICED NUMBER NULL );
These columns are later on filled with values and then made NOT NULL.
2. Would I gain anything by make these columns NOT NULL with a DEFAULT value in the first statement and then later on insert the values?
/Roland Bali
null1. It wud definitely increase the performance.
2. You can only have NOT NULL columns added to an existing table if the table is empty.
<BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by Roland Bali ([email protected]):
Hi,
I'd like to improve performance for scripts running several ALTER TABLE statements. I have two questions regarding this.
This is the original code:
ALTER TABLE CUSTOMER_ORDER_DELIVERY_TAB ADD (
QTY_TO_INVOICE NUMBER NULL );
ALTER TABLE CUSTOMER_ORDER_DELIVERY_TAB ADD (
QTY_INVOICED NUMBER NULL );
1. Would I gain any performance by making the following changes?
ALTER TABLE CUSTOMER_ORDER_DELIVERY_TAB ADD (
QTY_TO_INVOICE NUMBER NULL,
QTY_INVOICED NUMBER NULL );
These columns are later on filled with values and then made NOT NULL.
2. Would I gain anything by make these columns NOT NULL with a DEFAULT value in the first statement and then later on insert the values?
/Roland Bali<HR></BLOCKQUOTE>
Naveen -
Wat is the exact differences between clustered table and pooled table
hi,
can you tell me ravi...wat is the exact differences between clustered table and pooled table
with regards//
anilreddygHi Anil Reddy
Pooled Tables, Table Pools, Cluster Tables, and Table Clusters
These types of tables are not transparent in the sense that they are not legible or manageable directly using the underlying database system tools. They are managed from within the R/3 environment from the ABAP dictionary and also at runtime when they are loaded into application memory.Pool and cluster tables are logical tables. Physically, these logical tables are arranged as records of transparent tables. The pool and cluster tables are grouped together in other tables, which are of the transparent type. The tables that group together pool tables are known as table pools, or just pools; similarly, table clusters, or just
clusters, are the tables which group cluster tables.Not all operations that can be performed over transparent tables can be executed over pool or cluster tables.
For instance, you can manage these tables using Open SQL calls from ABAP, but not Native SQL.These tables are meant to be buffered and loaded in memory, because they are commonly used for storing internal control information and other types of data with no external (business) relevance. SAP recommends that tables of pool or cluster type be used exclusively for control information such as
program parameters, documentation, and so on. Transaction and application data should be stored in transparent tables.
Table Pools
From the point of view of the underlying DBMS as from the point of view of the ABAP dictionary, a table pool is a transparent table containing a group of pooled tables which, when created, were assigned to this table pool.
Field Type Description
TABNAME CHAR(10) Table name
VARKEY CHAR(n) Maximum key length n =< 110
DATALN INT2(5) Length of the VARDATA record returned
VARDATA RAW(m) Maximum length of the data varies according to DBMS
Table Clusters
Similarly to pooled tables, cluster tables are logical tables which, when created, are assigned to a table cluster. Therefore, a table cluster, or just cluster, groups together several tables of type clusters.Several logical rows from different cluster tables are brought together in a single physical record. The records
from the cluster tables assigned to a cluster are thus stored in a single common table in the database.A cluster contains a transparent cluster key which must be located at the start of the key of all logical cluster tables to be included in the cluster. As well, a cluster contains a long field (VARDATA), which contains the
data of the cluster tables for this key. If the data does not fit into a field, continuation records are created.
Field Type Description
CLKEY1 CHAR(*) First key fields
CLKEY2 CHAR(*) Second key field
CLKEYN CHAR(*) nth key field
PAGENO INT2(5) Number of the next page
TIMESTMP CHAR(14) Time stamp
PAGELG INT2(5) Length of the VARDATA record returned
VARDATA RAW(*) Maximum length of the data section; varies according to database system
Working with Tables
The dictionary includes many functions for working with tables. There are five basic operations you can perform on tables: display, create, delete, modify, copy. Please do not confuse displaying a table with displaying the table entries (table contents). In order to display a table, it must previously exist; otherwise the system will display an error message in the status bar. For the following example, the table TABNA is used. To display this table, from the main dictionary screen, enter the table name in the Object name
input field with the radio button selected next to Tables. Then, click on the Display button at the bottom of the screen, or press the F7 function key, or, alternatively,
select Dictionary object Display from the menu.
In this screen, you can see table information such as
¨ Table type, shown next to the name of the object. In the example, it is a transparent table.
¨ Short text description.
¨ Name of the user who made the last change, and the date of the change.
¨ Master language.
¨ Table status. On the screen, you can see this table is saved and active.
¨ Development class. For information on development classes, refer to Chap. 6.
Delivery class, which sets the maintenance group for the table. It controls how tables will behave during client copy procedures, upgrades, and so forth.¨
Tab. Maint. Allowed flag, which indicates whether you can generate a screen for maintaining table entries.
¨Then, on the lower part of the screen, you can see the table fields with all associated characteristics such as:
¨ Field name.
¨ Key indicator. When set, this field is the primary key, or part of it.
¨ Data element.
¨ Basic data type.
¨ Length.
¨ Check table.
¨ Short text, describing the field.
Additional information about the table can be displayed by selecting the corresponding functions from the menu or directly from the application toolbar, such as keys, indexes, or technical settings
Standard table:
The key access to a standard table uses a sequential search. The time required for an access is linearly dependent on the number of entries in the internal table.
You should usually access a standard table with index operations.
Sorted table:
The table is always stored internally sorted by its key. Key access to a sorted table can therefore use a binary search. If the key is not unique, the entry with the lowest index is accessed. The time required for an access is logarithmically dependent on the number of entries in the internal table.
Index accesses to sorted tables are also allowed. You should usually access a sorted table using its key.
Hash table:
The table is internally managed with a hash procedure. All the entries must have a unique key. The time required for a key access is constant, that is it does not depend on the number of entries in the internal table.
You cannot access a hash table with an index. Accesses must use generic key operations (SORT, LOOP, etc.).
Index table:
The table can be a standard table or a sorted table.
Index access is allowed to such an index table. Index tables can be used to define the type of generic parameters of a FORM (subroutine) or a function module.
Just have a look at these links:
http://help.sap.com/saphelp_nw04/helpdata/en/90/8d7304b1af11d194f600a0c929b3c3/frameset.htm
http://help.sap.com/saphelp_nw04/helpdata/en/74/83015785d811d295a800a0c929b3c3/frameset.htm
Regards
Sreeni -
What is basic difference between alter any & bebug any procedure sys privs?
Hi
I would like to know what is the basic difference between alter any & debug any procedure sys privs? Under which circustances, these will be granted?
Regards
ArizALTER ANY is the privilege to recompile. The DEBUG privilege displays information on all SQL statements executed by the application generated by debug mode.
I have never found any value in creating a procedure in debug mode though I have found a lot of procs compiled in debug mode that needed to be recompiled. -
Difference between Temp table and Variable table and which one is better performance wise?
Hello,
Anyone could you explain What is difference between Temp Table (#, ##) and Variable table (DECLARE @V TABLE (EMP_ID INT)) ?
Which one is recommended to use for better performance?
also Is it possible to create CLUSTER and NONCLUSTER Index on Variable table?
In my case: 1-2 days transactional data are more than 3-4 Millions. I tried using both # and table variable and found table variable is faster.
Is that Table variable using Memory or Disk space?
Thanks Shiven:) If Answer is Helpful, Please VoteCheck following link to see differences b/w TempTable & TableVariable: http://sqlwithmanoj.com/2010/05/15/temporary-tables-vs-table-variables/
TempTables & TableVariables both use memory & tempDB in similar manner, check this blog post: http://sqlwithmanoj.com/2010/07/20/table-variables-are-not-stored-in-memory-but-in-tempdb/
Performance wise if you are dealing with millions of records then TempTable is ideal, as you can create explicit indexes on top of them. But if there are less records then TableVariables are good suited.
On Tables Variable explicit index are not allowed, if you define a PK column, then a Clustered Index will be created automatically.
But it also depends upon specific scenarios you are dealing with , can you share it?
~manoj | email: http://scr.im/m22g
http://sqlwithmanoj.wordpress.com
MCCA 2011 | My FB Page -
What's the difference between "PreparedStatement" and "Statement"?
What's the difference between "PreparedStatement" and "Statement"?
Which is better??????Read the docs for the two classes. The differences are apparent there.
Which one is better depends on your needs. I think that constructing and executing a PreparedStatement can be a bit less performant than just a Statement, if you're only executing the query one time. But I don't think that difference will normally be noticable in the context of a given application.
Additionally, if you have to pass any dates or strings to the query, you'll want PreparedStatement's parameters, rather than trying to format and escape things in the query string. -
Need to find the Difference between two table
Hello ,
I have stucked in program as below scenario:-
I have two tables of huge data of same structure in a same schema.I need to find the difference exact values in tables.
By using MINUS we can find the difference between two table ,i need to find the what exact difference in the there values with colunm and value at that column.
Example TableA
Col1 col2 col3 col4 col5.... col50
10 ABC 2001 EE 444 TT
40 XYZ 3002 RR 445 TT3
80 DEF 6005 YY 446 YY8
TableB
Col1 col2 col3 col4 col5.... col50
10 ABC 2001 EE 444 TT
40 XYZ 3002 RR 445 TT3
81 DEF 6005 Yu 447 YY8
I need to the out put like this :-
The Diffence between two table is
TableA.COL1=80 TableB.Col1=81, Different
TableA.Col4=YY TableB.col4=Yu,Different
TableA.Col5=446TableB.col5=447,Different
Please suggest me to write the pl/sql program for the same
thanx in advance
KKThanx friends for all your efforts
I have a sample code for the same,this will compare the two tables for single row in each table .
what r the modification needed for the multiple rows of values in the two tables??
Please suggest!!
CREATE OR REPLACE PROCEDURE test_compare
IS
TYPE t_col
IS
TABLE OF VARCHAR2 (30)
INDEX BY PLS_INTEGER;
l_col t_col;
j NUMBER := 0;
l_sql VARCHAR2 (2000);
col1 VARCHAR2 (30);
col2 VARCHAR2 (30);
val1 NUMBER;
val2 NUMBER;
status VARCHAR2 (30);
CURSOR c1
IS
SELECT column_id, column_name
FROM all_tab_columns
WHERE table_name = 'TEST1';
BEGIN
FOR i IN c1
LOOP
j := j + 1;
l_col (j) := i.column_name;
END LOOP;
FOR k IN 1 .. j
LOOP
l_sql :=
'SELECT '
|| ''''
|| l_col (k)
|| ''''
|| ', '
|| 'TEST2.'
|| l_col (k)
|| ', '
|| ''''
|| l_col (k)
|| ''''
|| ', '
|| 'TEST1.'
|| l_col (k )
|| ', '
|| 'DECODE(TEST2.'
|| l_col (k)
|| ' -TEST1.'
|| l_col (k)
|| ', 0, ''NO CHANGE'', ''CHANGED'') FROM TEST2, TEST1';
EXECUTE IMMEDIATE l_sql INTO col1, val1,col2, val2, status;
IF status = 'CHANGED'
THEN
DBMS_OUTPUT.put_line( 'TEST2.'
|| col1
|| '='
|| val1
|| ', TEST1.'
|| col2
|| '='
|| val2
|| ', '
|| status);
END IF;
END LOOP;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ('Error:- ' || SQLERRM);
END;
/ -
How to find the structural difference between two tables
Hi all,
How to find the structural difference between two tables .
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Production
PL/SQL Release 11.1.0.7.0 - Production
CORE 11.1.0.7.0 Production
TNS for 32-bit Windows: Version 11.1.0.7.0 - Production
NLSRTL Version 11.1.0.7.0 - Production
Thanks,
P Prakashyou could try something similar to this, for each table pair that you want to compare:
SELECT 'TABLE_A has these columns that are not in TABLE_B', DIFF.*
FROM (
SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH
FROM all_tab_columns
WHERE table_name = 'TABLE_A'
MINUS
SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH
FROM all_tab_columns
WHERE table_name = 'TABLE_B'
) DIFF
UNION
SELECT 'TABLE_B has these columns that are not in TABLE_A', DIFF.*
FROM (
SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH
FROM all_tab_columns
WHERE table_name = 'TABLE_B'
MINUS
SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH
FROM all_tab_columns
WHERE table_name = 'TABLE_A'
) DIFF;that's assuming, column_name, data_type and data_length are all you want to compare on. -
Difference between AS Java and Java Add-in
Hi Guys
Can anybody explain me the exact difference between AS Java and Java Add-in? How can I identify a SAP ABAP system is installed with Java Add-in or AS java?
Regards
DharmendraHi,
>and if usage is ECC/CRM etc then you go for dual stack installation.
Wrong ! The official SAP advice is now to use dual stack only for the systems where it is not possible to do otherwise : PI and SOLMAN. Check the new ERP master guide.
Personnally I would advice strongly to avoid dual stack systems for ECC/CRM or BI.
We have a dual stack BI system (because at the time, SAP adviced it) but 2 years later we regret it a lot.
Dual stack system are a mess to manage, administrate and patch.
This is exactly what I call a "false good idea" !
Regards,
Olivier -
Issue Differences between the table of totals (ZFXX00T) and detail (ZFXX00
Hi,
Currently I have a issue in the FI-SL module with Differences between the table of totals (ZFXX00T) and detail (ZFXX00A). And I do not Know why. Please your help or comments.did you try ERP Financials forum yet? a lot of FI-SL has been replaced with SAP Ledger FAGLFLEXA and T tables.
@greg_not_so -
Alter Table Statements in Designer
Hi Guys,
Just wondering if anyone knows if it's possible to store Alter Table statements in Designer 9i?
AntonHi,
The designer generate alter statements for you.
e.g You have a table emp with empno number(8) in designer and in your application database. You cahnged empno to number(12) in designer. Generate ddl pointing to your application database. Designer generate an ALTER table script for you. -
Difference between Qualified tables and tuples.
What is he difference between Qualified tables and tuples.
Does MDM 7.1 also support qualified tables? which is better out of 2.Hi Shekhar
Qualified tables are special tables which have Non qualifiers and Qualifiers and for a single record we can have multiple values based on NQ. To explain further- Material can have different prices based on Region of sale, Lot size. This can be modeled using a qualified table where Price varies based on combination of Sales Area and Lot size(NQs).
To read more please refer to link- https://weblogs.sdn.sap.com/pub/wlg/3417?page=last&x-showcontent=off&x-maxdepth=0 [original link is broken] [original link is broken] [original link is broken]
Tuple is new data type introduced in MDM 7.1, it has many plus points over Qualified table. It supports multiple nesting and can contain many different data type fields. To read more please read- Difference between TUPLE and a look up table
Thanks
Ravi
Maybe you are looking for
-
In need of a macro to transpose data from horizontal to vertical
I need help creating a macro that would transpose some data from horizontal to vertical.
-
New iPad Discolored/Tinted Display
Got the new iPad Firday. The resolution is phenomenal. However, I've noticed that holding the tablet in portrait mode, the display graduates subtly in color from left to right, a cooler blue to a warmer red tint. This is uniform from top to bottom.
-
Display database tables in generic tables in a browser
Hi, Iam relativelly new in Web programming and Jsp/Servlet. I am developing a web database application to learn Jsp/Servlet. My question is the following. I wrote some general classes in which i wrote general methods for accessing tables. Lets say My
-
Standard XML schema for Vendor data exchange between SAP and other system
Is there a SAP standard way of XML schema that we exchange between SAP and other system? Please let me know. Thanks.
-
Is there any chance that Lightroom 2 might be able to read the proprietary changes that Capture NX2 writes into the Nikon NEF files? These two programs greatly substitute each other.