Tuesday 1 August 2017

Primary Key in SAP HANA

v  For some applications, it is useful to uniquely identify the rows in a table by one or more columns so that you can process them in a specified sequence, for example. You can do this by assigning a primary key. The column names that are to create the table key are represented by the keywords PRIMARY KEY. The input values of the key columns defined in this way must not be a NULL value.
v  You can use the primary key to insert rows in a table in the same way as you insert rows in a base table when no primary key is defined. However, the system outputs an error message if you attempt to insert an existing value into the primary key column a second time since the uniqueness of the column is ensured by defining the primary key.
v  A primary key can consist of multiple columns. However, it is unusual for a key to be constructed from more than five columns, since this makes it difficult for users to enter unique values. The arrangement of the columns behind the keywords PRIMARY KEY defines the key sequence.
v  You can specify a primary key when you define the table or add it to an existing table at a later stage.
Examples:
Code:
create column table "KABIL_PRACTICE"."PRODUCT_PRIMARY_KEY"
 (
"Prod_Id" integer primary key,
"Prod_Name" varchar(25), 
"Stock_Qty" integer,
"Each_Price" integer
 );

(Or)

create column table "KABIL_PRACTICE"."PRODUCT_PRIMARY_KEY"
 (
"Prod_Id" integer,
"Prod_Name" varchar(25), 
"Stock_Qty" integer,
"Each_Price" integer,
primary key ("Prod_Id")  
 );

Both statements allow to uniquely identify the rows in the PRODUCT_PRIMARY_KEY table on the basis of the product id “Prod_Id”.

And insert some records into the table.

Code:

insert into "KABIL_PRACTICE"."PRODUCT_PRIMARY_KEY" values
(1,'fan',100,1500);

Results:

Product Table
When I try to insert another record with the same “Prod_Id” It through an error like this.

Unique Constraint Violated
And also it won’t allow any Null values.

Thank You for Visting my Blog...
Please Subscribe for Updates and Share Your Comments...

1 comment:

  1. If we need to insert null values in primary key column,what needs to be done??

    ReplyDelete