Generating Primary Key Column Sql

-->

Value generation patterns

Jan 07, 2020  Auto increment columns widely used for auto-generating values for primary keys in Database tables.Most of the databases like SQL server etc have existing features to create auto increment columns. In Oracle 12c they introduced IDENTITY columns which allows users to create auto increment columns. Because SQL Server doesn't support pseudo columns for identifiers, updates that have to use the auto-generated key feature must operate against a table that contains an IDENTITY column. SQL Server allows only a single IDENTITY column per table. The result set that is returned by getGeneratedKeys method of the SQLServerStatement class will have.

There are three value generation patterns that can be used for properties:

  • No value generation
  • Value generated on add
  • Value generated on add or update

No value generation

No value generation means that you will always supply a valid value to be saved to the database. This valid value must be assigned to new entities before they are added to the context.

Value generated on add

Value generated on add means that a value is generated for new entities.

Depending on the database provider being used, values may be generated client side by EF or in the database. If the value is generated by the database, then EF may assign a temporary value when you add the entity to the context. This temporary value will then be replaced by the database generated value during SaveChanges().

If you add an entity to the context that has a value assigned to the property, then EF will attempt to insert that value rather than generating a new one. A property is considered to have a value assigned if it is not assigned the CLR default value (null for string, 0 for int, Guid.Empty for Guid, etc.). For more information, see Explicit values for generated properties.

Warning

How the value is generated for added entities will depend on the database provider being used. Database providers may automatically setup value generation for some property types, but others may require you to manually setup how the value is generated.

For example, when using SQL Server, values will be automatically generated for GUID properties (using the SQL Server sequential GUID algorithm). However, if you specify that a DateTime property is generated on add, then you must setup a way for the values to be generated. One way to do this, is to configure a default value of GETDATE(), see Default Values.

Value generated on add or update

Value generated on add or update means that a new value is generated every time the record is saved (insert or update).

Like value generated on add, if you specify a value for the property on a newly added instance of an entity, that value will be inserted rather than a value being generated. It is also possible to set an explicit value when updating. For more information, see Explicit values for generated properties.

Warning

How the value is generated for added and updated entities will depend on the database provider being used. Database providers may automatically setup value generation for some property types, while others will require you to manually setup how the value is generated.

For example, when using SQL Server, byte[] properties that are set as generated on add or update and marked as concurrency tokens, will be setup with the rowversion data type - so that values will be generated in the database. However, if you specify that a DateTime property is generated on add or update, then you must setup a way for the values to be generated. One way to do this, is to configure a default value of GETDATE() (see Default Values) to generate values for new rows. You could then use a database trigger to generate values during updates (such as the following example trigger).

Value generated on add

By convention, non-composite primary keys of type short, int, long, or Guid are set up to have values generated for inserted entities, if a value isn't provided by the application. Your database provider typically takes care of the necessary configuration; for example, a numeric primary key in SQL Server is automatically set up to be an IDENTITY column.

You can configure any property to have its value generated for inserted entities as follows:

Primary

Warning

This just lets EF know that values are generated for added entities, it does not guarantee that EF will setup the actual mechanism to generate values. See Value generated on add section for more details.

Default values

On relational databases, a column can be configured with a default value; if a row is inserted without a value for that column, the default value will be used.

You can configure a default value on a property:

You can also specify a SQL fragment that is used to calculate the default value:

Sql Insert Primary Key

Specifying a default value will implicitly configure the property as value generated on add.

Value generated on add or update

Warning

This just lets EF know that values are generated for added or updated entities, it does not guarantee that EF will setup the actual mechanism to generate values. See Value generated on add or update section for more details.

Computed columns

On some relational databases, a column can be configured to have its value computed in the database, typically with an expression referring to other columns:

Note

In some cases the column's value is computed every time it is fetched (sometimes called virtual columns), and in others it is computed on every update of the row and stored (sometimes called stored or persisted columns). This varies across database providers.

No value generation

Disabling value generation on a property is typically necessary if a convention configures it for value generation. For example, if you have a primary key of type int, it will be implicitly set configured as value generated on add; you can disable this via the following:

Summary: in this tutorial, you will learn how to use the MySQL generated columns to store data computed from an expression or other columns.

Primary Key Adalah

Introduction to MySQL generated column

When you create a new table, you specify the table columns in the CREATE TABLE statement. Then, you use the INSERT, UPDATE, and DELETE statements to modify directly the data in the table columns.

MySQL 5.7 introduced a new feature called the generated column. Columns are generated because the data in these columns are computed based on predefined expressions.

For example, you have the contacts with the following structure:

To get the full name of a contact, you use the CONCAT() function as follows:

This is not the most beautiful query yet.

By using the MySQL generated column, you can recreate the contacts table as follows:

The GENERATED ALWAYS as (expression) is the syntax for creating a generated column.

Tsql Primary Key Columns

To test the fullname column, you insert a row into the contacts table.

Now, you can query data from the contacts table.

The values in the fullname column are computed on the fly when you query data from the contacts table.

MySQL provides two types of generated columns: stored and virtual. The virtual columns are calculated on the fly each time data is read whereas the stored column are calculated and stored physically when the data is updated.

Based on this definition, the fullname column that in the example above is a virtual column.

MySQL generated column’s syntax

The syntax for defining a generated column is as follows:

First, specify the column name and its data type.

Next, add the GENERATED ALWAYS clause to indicate that the column is a generated column.

Then, indicate whether the type of the generated column by using the corresponding option: VIRTUAL or STORED. By default, MySQL uses VIRTUAL if you don’t specify explicitly the type of the generated column.

After that, specify the expression within the braces after the AS keyword. The expression can contain literals, built-in functions with no parameters, operators, or references to any column within the same table. If you use a function, it must be scalar and deterministic.

Finally, if the generated column is stored, you can define a unique constraint for it.

MySQL stored column example

Let’s look at the products table in the sample database.

The data from quantityInStock and buyPrice columns allow us to calculate the stock’s value per SKU using the following expression:

However, we can add a stored generated column named stock_value to the products table using the following ALTER TABLE ...ADD COLUMN statement:

Typically, the ALTER TABLE statement requires a full table rebuild, therefore, it is time-consuming if you change the big tables. However, this is not the case for the virtual column.

Now, we can query the stock value directly from the products table.

Unique Key

In this tutorial, you have learned how to use the MySQL generated column to store data computed from an expression or other columns.