Oracle Create Table:

CREATE TABLE TAB(
ID NUMBER(10) NOT NULL PRIMARY KEY,
NAME VARCHAR(19) NOT NULL
);

Drop table when exists:

BEGIN
EXECUTE IMMEDIATE 'DROP TABLE TAB';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -942 THEN
RAISE;
END IF;
END;

Create sequence as id:

CREATE SEQUENCE ID_SEQ_TAB
START WITH 1
INCREMENT BY 1
MINVALUE 1
NOMAXVALUE
NOCACHE;

Drop sequence when exists:

BEGIN
EXECUTE IMMEDIATE 'DROP SEQUENCE ID_SEQ_TAB';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -2289 THEN
RAISE;
END IF;
END;

Create trigger:

CREATE OR REPLACE TRIGGER ID_TRI_TAB
BEFORE INSERT
ON TAB
FOR EACH ROW
WHEN(NEW.ID IS NULL)
BEGIN
SELECT ID_SEQ_TAB.NEXTVAL INTO :NEW.ID FROM DUAL;
END;

Insert into table:

Insert into table_name (column_name...) values(values...)

Select column names of a table:

select column_name from user_tab_columns
where table_name = 'YOUR TABLE NAME HERE';

ps:

  • dual:

A mystery table in Oracle, You can select many things from this table:

Select a_squence_name.nextval from dual
  • Select into from:

A copy operation on oracle.You can use it like this:

SELECT * INTO new_table_name [IN externaldatabase] FROM old_tablename

Primary Key Increase by Trigger的更多相关文章

  1. 分区实践 A PRIMARY KEY must include all columns in the table's partitioning function

    MySQL :: MySQL 8.0 Reference Manual :: 23 Partitioning https://dev.mysql.com/doc/refman/8.0/en/parti ...

  2. ORA-02429: cannot drop index used for enforcement of unique /primary key

    相信不少人遇到过ORA-02429: cannot drop index used for enforcement of unique /primary key 这个错误,对应的中文提示"O ...

  3. SQL PRIMARY KEY 约束\SQL FOREIGN KEY 约束\SQL CHECK 约束

    SQL PRIMARY KEY 约束 PRIMARY KEY 约束唯一标识数据库表中的每条记录. 主键必须包含唯一的值. 主键列不能包含 NULL 值. 每个表都应该有一个主键,并且每个表只能有一个主 ...

  4. Hibernate Id Generator and Primary Key

    Use automate id by hibernate: If you want the tables' id be created automation. How to do it? When u ...

  5. Database Primary key and Foreign key [From Internet]

    Database Primary key and Foreign key --Create Referenced Table CREATE TABLE Department ( DeptID int ...

  6. SQLite主键自增需要设置为integer PRIMARY KEY

    按照正常的SQL语句,创建一个数据表,并设置主键是这样的语句: ), EventType )) 但使用这种办法,在SQLite中创建的的数据表,如果使用Insert语句插入记录,如下语句: INSER ...

  7. ASP.NET MVC another entity of the same type already has the same primary key value

    ASP.NET MVC项目 Repository层中,Update.Delete总是失败 another entity of the same type already has the same pr ...

  8. 1503 - A PRIMARY KEY must include all columns in the table's partitioning function

    1503 - A PRIMARY KEY must include all columns in the table's partitioning function 错误的原因:表的主键字段必须包含分 ...

  9. (转)Sqlite中INTEGER PRIMARY KEY AUTOINCREMENT和rowid的使用

    原文:http://www.cnblogs.com/peida/archive/2008/11/29/1343832.html Sqlite中INTEGER PRIMARY KEY AUTOINCRE ...

随机推荐

  1. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_02 泛型_4_定义和使用含有泛型的方法

    泛型方法 泛型也可以用在方法上 测试 调用的时候,可以传递多种数据类型 . 泛型的静态方法

  2. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_1_IO概述(概念&分类)

  3. 小程序-web-view嵌入H5页面遇到的bug

    遇到的问题1:ios页面中,内容过多时有下滑真是功能,但是下滑的时候回看到底部的微信自带的灰色背景及H5的域名(ios的webview中上/下拉露出黑灰色背景问题) 解决办法:给body添加样式--- ...

  4. lambda表达式(1)

    转自:http://www.cnblogs.com/knowledgesea/p/3163725.html 简化了匿名委托的使用,让你让代码更加简洁,优雅.据说它是微软自c#1.0后新增的最重要的功能 ...

  5. oracle--表空间处理

    CREATE TABLESPACE命令详解(转) 表空间理解 https://www.cnblogs.com/kerrycode/p/3418694.html 常用操作 https://www.cnb ...

  6. [19/05/18-星期六] HTML_form标签

    一.form标签(一) <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...

  7. Maven-Eclipse使用maven创建HelloWorld Java项目,使用Junit-4.11的注解

    1.针对前面创建的mavenTest项目,我们做一些修改,包括pom.xml.App.java.AppTest.java 说明:其中的scope属性,如果是test,表示该依赖只对测试有效,如果不声明 ...

  8. 解决MarkDown打开出现:awesomium web-brower framework This view has crashed

    当在windows 8 以上操作系统安装markdown 的时候,可能会出现这样的错误 解决方法: 官网链接:http://markdownpad.com/faq.html#livepreview-d ...

  9. CodeForces.1174D.ArraySplitting.(后缀和数组 + 贪心)

    题目链接 参考代码: #include <cstdio> #include <algorithm> using namespace std; typedef long long ...

  10. [AHOI2013]作业 (莫队+分块)

    [AHOI2013]作业 (莫队+分块) 题面 给定了一个长度为n的数列和若干个询问,每个询问是关于数列的区间[l,r],首先你要统计该区间内大于等于a,小于等于b的数的个数,其次是所有大于等于a,小 ...