sql: Oracle 11g create table, function,trigger, sequence

时间:2023-02-22 15:54:52
--书藉位置Place目录
drop table BookPlaceList; create table BookPlaceList
(
BookPlaceID INT PRIMARY KEY, --NUMBER
BookPlaceName nvarchar2(500) not null,
BookPlaceCode varchar(100) null, --位置編碼
BookPlaceParent INT null
--BookPlaceKindId nvarchar(500) null --放置目录範圍ID
); select * from BookPlaceList; ---自动增长ID
--序列创建
drop SEQUENCE BookPlaceList_SEQ; CREATE SEQUENCE BookPlaceList_SEQ
INCREMENT BY 1 -- 每次加几个
START WITH 1 -- 从1开始计数
NOMAXVALUE -- 不设置最大值
NOCYCLE -- 一直累加,不循环
NOCACHE; --设置缓存cache个序列,如果系统down掉了或者其它情况将会导致序列不连续,也可以设置为---------NOCACHE SELECT BookPlaceList_SEQ.Currval FROM DUAL; SELECT BookPlaceList_SEQ.Nextval FROM DUAL; --自增长触发器
drop TRIGGER BookPlaceList_ID_AUTO; CREATE OR REPLACE TRIGGER BookPlaceList_ID_AUTO
BEFORE INSERT ON BookPlaceList FOR EACH ROW
BEGIN
SELECT BookPlaceList_SEQ.NEXTVAL INTO :NEW.BookPlaceID FROM DUAL;
END; --自增长触发器
create or replace trigger BookPlaceList_ID_AUTO
before insert on BookPlaceList --BookPlaceList 是表名
for each row
declare
nextid number;
begin
IF :new.BookPlaceID IS NULL or :new.BookPlaceID=0 THEN --BookPlaceID是列名
select BookPlaceList_SEQ.Nextval --BookPlaceList_SEQ正是刚才创建的
into nextid
from dual;
:new.BookPlaceID:=nextid;
end if;
end; -- BookPlaceList_ID_AUTO --添加
insert into BookPlaceList(BookPlaceName,BookPlaceCode,BookPlaceParent) values('图书位置目录','',0); insert into BookPlaceList(BookPlaceName,BookPlaceCode,BookPlaceParent) values('第一柜','',1);
insert into BookPlaceList(BookPlaceName,BookPlaceCode,BookPlaceParent) values('第二柜','',1); insert into BookPlaceList(BookPlaceName,BookPlaceCode,BookPlaceParent) values('第三柜','',1); select * from BookPlaceList; drop table StaffReaderList;
--职员信息Reader staff member IC卡号(卡换了,卡号不一样),员工号,职位,部门,如果职员换岗或离职了,这个问题如何解决记录关联问题
create table StaffReaderList
(
StaffReaderID INT PRIMARY KEY,
StaffReaderIC varchar(100) not null, --员工工牌IC号
StaffReaderNO varchar(20) not null, --员工编号
StaffReaderName nvarchar2(500) not null, --员工姓名
StaffReaderImage BFILE null,
StaffReaderDepartment int,
CONSTRAINT fky_StaffReaderDepartment
FOREIGN KEY(StaffReaderDepartment) REFERENCES DepartmentList(DepartmentID),--员工所属部门(外键) ON DELETE SET NULL ON DELETE CASCADE
StaffReaderPosition int,
CONSTRAINT fky_StaffReaderPosition
FOREIGN KEY(StaffReaderPosition) REFERENCES PositionList(PositionID), --职位Position(外键)
StaffReaderMobile varchar(50) null, --手机
StaffReaderTel varchar(200) null, --电话,
StaffReaderSkype varchar(50) null, ---
StaffReaderQQ varchar(50) null, --
StaffReaderEmail varchar(100) null, --电子邮件
StaffReaderIsJob char check (StaffReaderIsJob in ('N','Y')), --是否離職
StaffReaderOperatorID int,
CONSTRAINT fky_StaffReaderOperatorID
FOREIGN KEY(StaffReaderOperatorID) REFERENCES BookAdministratorList(BookAdminID),--操作人员ID(添加记录的人员)(外键)
StaffReaderDatetime TIMESTAMP --
); --判断表是否存在
SELECT COUNT(*) FROM User_Tables t WHERE t.table_name = upper('AuthorList');
create or replace FUNCTION f_BookPlacename(kid in number) RETURN nvarchar2 IS
tmpVar nvarchar2(100);
/******************************************************************************
NAME: f_BookPlacename
PURPOSE: REVISIONS:
Ver Date Author Description
--------- ---------- --------------- ------------------------------------
1.0 2015/5/21 geovindu 1. Created this function. NOTES: Automatically available Auto Replace Keywords:
Object Name: f_BookPlacename
Sysdate: 2015/5/21
Date and Time: 2015/5/21, 12:02:38, and 2015/5/21 12:02:38
Username: geovindu (set in TOAD Options, Procedure Editor)
Table Name: BookPlaceList (set in the "New PL/SQL Object" dialog) ******************************************************************************/
BEGIN
--tmpVar := "";
select BookPlaceName into tmpVar from BookPlaceList where BookPlaceID=kid;
RETURN tmpVar;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
WHEN OTHERS THEN
--tmpVar := "";
-- Consider logging the error and then re-raise
RAISE;
END f_BookPlacename;
--测试 涂聚文 20150522
select f_BookPlacename(1) FROM dual;
declare tableCount number;
begin
select count(1) into tableCount from user_tables t where t.table_name = upper('TestDu'); --从系统表中查询当表是否存在
if tableCount = 0 then --如果不存在,使用快速执行语句创建新表
execute immediate
'create table TestDu --创建测试表
(
TestID number not null,
TestName varchar2(20) not null
)';
end if;
end;
declare --在PL/SQL 匿名块中定义变量
vName nvarchar2(200):='齐白石水彩画系列';
vEname nvarchar2(200):='丰子恺油画系列';
vId number:=2;
--set serveroutput on size 5000;
begin
update BookSeriesList set BookSeriesName=vName where BookSeriesID=vId;
DBMS_OUTPUT.PUT_LINE('书系列更新成功!');
IF SQL%NOTFOUND THEN --判断,如果未更新数据,则向表中插入记录
insert into BookSeriesList(BookSeriesName) values(vName);
DBMS_OUTPUT.PUT_LINE('书系列插入成功!');
END IF;
---异常处理
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('插入书系列记录错误!');
end;

 

Toad for Oracle 启动DBMS输出. Oracle SQL developer 和 SQL Plus 用Script启动:set serveroutput on size 5000;

见图:

sql: Oracle 11g create table, function,trigger, sequence 

  

sql: Oracle 11g create table, function,trigger, sequence的更多相关文章

  1. sql: Oracle 11g create procedure

    CREATE OR REPLACE PROCEDURE proc_Insert_BookKindList ( temTypeName nvarchar2, temParent int ) AS nco ...

  2. sql: Oracle simple example table

    --Oracle 9i 实例数据脚本地址:$oracle_home/rdbms/admin/utlsampl.sql CREATE TABLE DEPT (DEPTNO NUMBER(2) CONST ...

  3. Oracle Demo ->> CREATE TABLE

    Demo One CREATE TABLE employees_demo ( employee_id ) , first_name ) , last_name ) CONSTRAINT emp_las ...

  4. oracle job create table insert into

    create or replace procedure proc_tzyj is begin insert into t_trade_activity@dw3_link.regress.rdbms.d ...

  5. 转换sql文件的create table语句为drop table语句

    package com.csii.pweb.query.action; import java.io.BufferedReader; import java.io.FileNotFoundExcept ...

  6. PLS-00357: Table,View Or Sequence reference 'SEQ_TRADE_RECODE.NEXTVAL' not allowed in this context

    oracle数据库: 为了使ID自增,建了序列后,创建触发器: create or replace TRIGGER TRIG_INSERT_TRADE_RECODE BEFORE INSERT ON ...

  7. MongoDB 实现 create table tab2 as select

    1. var result = db.foo.aggregate(...);db.bar.insert(result.result); 2. var temp1 = db.mtb1.find(name ...

  8. SQL SERVER 2008向ORACLE 11G迁移示例

    来源于:http://www.cnblogs.com/hiizsk/ 由SQL SERVER 2008向ORACLE 11G迁移过程记录之一-表 使用Oracle Sql Developer将SQL ...

  9. 使用Oracle Sql Developer将SQL SERVER 2008数据库移植到Oracle 11g

    ORACLE官方提供的Sql Developer自带的Oracle Migration Workbench. 什么是Oracle SQL Developer?在官方页面上,是这样介绍它的: Oracl ...

随机推荐

  1. ListView具有多种item布局——实现微信对话列

    这篇文章的效果也是大家常见的,各种通讯应用的对话列表都是这种方式,像微信.whatsapp.易信.米聊等.我们这篇文章也权当为回忆,形成简单的笔记.这篇文章参考了2009年Google IO中的&lt ...

  2. android.os.DeadObjectException memory near r0: 异常处理 Consumer closed input channel or an error occurred. events=0x9

    原地址:http://www.cnblogs.com/wanqieddy/p/3495338.html android.os.DeadObjectException memory near r0: 异 ...

  3. Merkle Tree算法详解

    转载自:http://blog.csdn.net/yuanrxdu/article/details/22474697Merkle Tree是Dynamo中用来同步数据一致性的算法,Merkle Tre ...

  4. poj 1426 Find The Multiple( bfs )

    题目:http://poj.org/problem?id=1426 题意:输入一个数,输出这个数的整数 倍,且只有0和1组成 程序里写错了一个数,结果一直MLE.…… #include <ios ...

  5. Android TextView &colon; &OpenCurlyDoubleQuote;Do not concatenate text displayed with setText”

    参考:http://*.com/questions/33164886/android-textview-do-not-concatenate-text-displayed-wi ...

  6. wpa&lowbar;cli 关联无线网络

    fq关联无线网络,不同的无线网络认证方式不同设置:    1)open(开放式认证方式,分为):         wpa_cli -iwlan0 set_network 0 ssid '"w ...

  7. Python爬虫入门教程 32-100 B站博人传评论数据抓取 scrapy

    1. B站博人传评论数据爬取简介 今天想了半天不知道抓啥,去B站看跳舞的小姐姐,忽然看到了评论,那就抓取一下B站的评论数据,视频动画那么多,也不知道抓取哪个,选了一个博人传跟火影相关的,抓取看看.网址 ...

  8. 【问题记录】MyBatis查询数据库返回多个不同类型参数的结果集的接收方式

    其实是个非常简单的问题,但是这玩意儿弄得我很难受,又浪费了一个下午的时间,简直了…… 问题大概是,我在查询数据库时,查询的结果有两个,一个是varchar格式的字段,一个int格式字段,例如: sel ...

  9. asp&period;net core mvc发布后显示异常错误信息的方法

    在发布的项目文件夹中找到web.config文件,修改: <aspNetCore processPath="dotnet" arguments=".\Cloud.B ...

  10. noip第16课资料