An identity column contains a unique numeric value for each row in the table. DB2® can automatically generate sequential numeric values for this column as rows are inserted into the table. Thus, identity columns are ideal for primary key values, such as employee numbers or product numbers.

You can define an identity column as either GENERATED BY DEFAULT or GENERATED ALWAYS:

  • If you define the column as GENERATED BY DEFAULT, you can insert a value, and DB2 provides a default value if you do not supply one.
  • If you define the column as GENERATED ALWAYS, DB2 always generates a value for the column, and you cannot insert data into that column. If you want the values to be unique, you must define the identity column with GENERATED ALWAYS and NO CYCLE and define a unique index on that column.

Suppose that table T1 is defined with GENERATED ALWAYS and CYCLE:

  1. CREATE TABLE T1
  2. (CHARCOL1 CHAR(1),
  3. IDENTCOL1 SMALLINT GENERATED ALWAYS AS IDENTITY
  4. (START WITH -1,
  5. INCREMENT BY 1,
  6. CYCLE,
  7. MINVALUE -3,
  8. MAXVALUE 3));
  1. INSERT INTO T1 (CHARCOL1) VALUES ('A');
  1. CHARCOL1 IDENTCOL1
  2. ======== =========
  3. A -1
  4. A 0
  5. A 1
  6. A 2
  7. A 3
  8. A -3
  9. A -2
  10. A -1

The following example adds a column to the table DSN8910.DEPT, which contains a location code for the department. The column name is LOCATION_CODE, and its data type is CHAR (4).

  1. ALTER TABLE DSN8910.DEPT
  2. ADD LOCATION_CODE CHAR (4);

Add hidden column 需要加关键字,参考这里:

http://www.ibm.com/support/knowledgecenter/SSEPEK_10.0.0/com.ibm.db2z10.doc.sqlref/src/tpc/db2z_sql_altertable.dita?lang=en

  1.  

DB2 Add hidden Identity columns的更多相关文章

  1. Oracle列自增实现(2)-Identity Columns in Oracle Database 12c Release 1 (12.1)

    Oracle列自增-Identity Columns in Oracle Database 12c Release 1 (12.1) 在ORACLE 12C以前的版本中,如果要实现列自增长,需要通过序 ...

  2. Oracle 12C -- Identity Columns(标识列)

    Identity Columns很适合数据库中需要"surrogate keys"的场景.依赖sequence产生器,每行的标识列会被赋予一个自增或自减的值.缺省,标识列在创建的时 ...

  3. DevExpress Add ASPxGridView template columns at runtime

    <%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %> <%@ Import Namespace ...

  4. Oracle 12c的自增列Identity Columns

    在Oracle的12c版本中,Oracle实现了类似MySQL中的auto_increment的自增列,下面我们看一起Oracle是怎么实现的. Oracle Database 12c Enterpr ...

  5. asp.net—自定义轻量级ORM

    大型项目中ORM的使用已经是相当的频繁.目前.NET(C#)中比较流行的ORM框架也有很多,比如SqlSugar,Dapper,Entity Framework(EF)等. 相信很多有2年以上工作经验 ...

  6. 连表查询都用Left Join吧 以Windows服务方式运行.NET Core程序 HTTP和HTTPS的区别 ASP.NET SignalR介绍 asp.net—WebApi跨域 asp.net—自定义轻量级ORM C#之23中设计模式

    连表查询都用Left Join吧   最近看同事的代码,SQL连表查询的时候很多时候用的是Inner Join,而我觉得对我们的业务而言,99.9%都应该使用Left Join(还有0.1%我不知道在 ...

  7. DB2 for z: system catalog tables

    http://www.ibm.com/support/knowledgecenter/SSEPEK_10.0.0/com.ibm.db2z10.doc.sqlref/src/tpc/db2z_cata ...

  8. Microsoft.AspNet.Identity.EntityFramework/IdentityDbContext.cs

    using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; ...

  9. DB2 移动数据总结一

    数据移动参考的连接 IMPORT http://www-01.ibm.com/support/knowledgecenter/SSEPGG_9.7.0/com.ibm.db2.luw.admin.cm ...

随机推荐

  1. FineUI第十三天---`列布局

    这是经典的列布局:                  <x:Panel runat=                     <Items>                      ...

  2. IntelliJ Idea 修改编码格式

    Setting→Editor→File Encodings→设置“Project Encoding”为UTF-8,如图:

  3. String与InputStream互转的几种方法

    [java] view plain copy /** * 利用BufferedReader实现Inputstream转换成String <功能详细描述> * * @param in * @ ...

  4. 对xml文件的简单解析

    package com.eprobj.demo; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; impor ...

  5. django中抽象基类的Foreignkey的定义

    class base(models.Model): user = models.ForeignKey(User) class Meta: abstract =True 以上是抽象基类的定义,只有一个公 ...

  6. tomcat 访问软连接

    Linux创建软连接: ln -s 源文件 目标文件 tomcat安装目录 / conf目录下的:context.xml文件在 <Context />; 里面加上 allowLinking ...

  7. ubuntu添加sudo权限

    ubuntu有时候没有开通sudo功能,有些操作只能切换到root进行,很不方便. 1.切换到root su root 2.打开suduers文件 gedit /etc/sudoers 3.找到下面这 ...

  8. 转:TopN推荐系统——推荐的实现与推荐效果的评价指标

    转自:用户推荐系统_python 代码-豆瓣书籍:项亮的<推荐系统实践> import random import math class UserBasedCF: def __init__ ...

  9. 转:JQuery选择器

    选择器是jQuery最基础的东西,本文中列举的选择器基本上囊括了所有的jQuery选择器,也许各位通过这篇文章能够加深对jQuery选择器的理 解,它们本身用法就非常简单,我更希望的是它能够提升个人编 ...

  10. HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth之完全详解

    HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth到底指的哪到哪的距离之完全详解 scrollHeight: 获取对象的滚动高度. scrollLe ...