A column or columns is called primary key (PK) that uniquely identifies each row in the table.
I am using org.hibernate.id.IdentifierGenerator to generate a primary key column as follows. In the following given example, currently it just increments the key of type INT(11) (MySQL) sequentiall. We can create a unique ID in java by using the UUID and call the method like randomUUID on UUID. String uniqueID = UUID.randomUUID.toString; This will generate the random uniqueID whose return type will be String.
If you want to create a primary key, you should define a PRIMARY KEY constraint when you create or modify a table.
When multiple columns are used as a primary key, it is known as composite primary key.
In designing the composite primary key, you should use as few columns as possible. It is good for storage and performance both, the more columns you use for primary key the more storage space you require.
Inn terms of performance, less data means the database can process faster.
When we specify a primary key constraint for a table, database engine automatically creates a unique index for the primary key column.
The main advantage of this uniqueness is that we get fast access.
In oracle, it is not allowed for a primary key to contain more than 32 columns.
The following SQL command creates a PRIMARY KEY on the 'S_Id' column when the 'students' table is created.
MySQL:
SQL Server, Oracle, MS Access:
MySQL, SQL Server, Oracle, MS Access:
Note:you should note that in the above example there is only one PRIMARY KEY (pk_StudentID). However it is made up of two columns (S_Id and LastName).
When table is already created and you want to create a PRIMARY KEY constraint on the 'S_Id' column you should use the following SQL:
Primary key on one column:
Primary key on multiple column:
When you use ALTER TABLE statement to add a primary key, the primary key columns must not contain NULL values (when the table was first created).
If you want to DROP (remove) a primary key constraint, you should use following syntax:
MySQL:
SQL Server / Oracle / MS Access:
This annotation defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue
annotation. A table generator may be specified on the entity class or on the primary key field or property. The scope of the generator name is global to the persistence unit (across all generator types).
Required Element Summary | |
---|---|
String | name (Required) A unique generator name that can be referenced by one or more classes to be the generator for id values. |
Optional Element Summary | |
---|---|
int | allocationSize (Optional) The amount to increment by when allocating id numbers from the generator. |
String | catalog (Optional) The catalog of the table. |
int | initialValue (Optional) The initial value to be used when allocating id numbers from the generator. |
String | pkColumnName (Optional) Name of the primary key column in the table. |
String | pkColumnValue (Optional) The primary key value in the generator table that distinguishes this set of generated values from others that may be stored in the table. |
String | schema (Optional) The schema of the table. |
String | table (Optional) Name of table that stores the generated id values. |
UniqueConstraint[] | uniqueConstraints (Optional) Unique constraints that are to be placed on the table. |
String | valueColumnName (Optional) Name of the column that stores the last value generated. |
Element Detail |
---|
Defaults to a name chosen by persistence provider.
Defaults to the default catalog.
Defaults to the default schema for user.
Defaults to a provider-chosen name.
/microsoft-word-activate-free-product-key-generator.html. Defaults to a provider-chosen name.
Defaults to a provider-chosen value to store in the primary key column of the generator table
Defaults to no additional constraints.
Overview | Package | Class | Tree | Deprecated | Index | Help |
Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.
Scripting on this page tracks web page traffic,but does not change the content in any way.