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

--书藉位置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;

  

时间: 2024-10-12 00:00:51

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

sql: Oracle 11g create procedure

CREATE OR REPLACE PROCEDURE proc_Insert_BookKindList ( temTypeName nvarchar2, temParent int ) AS ncount number; begin --SELECT COUNT (*) INTO ncount FROM BookKindList fm1 where EXISTS (SELECT BookKindName from BookKindList fm2 where fm2.BookKindName=

Oracle Demo ->> CREATE TABLE

CREATE TABLE employees_demo ( employee_id NUMBER(6) , first_name VARCHAR2(20) , last_name VARCHAR2(25) CONSTRAINT emp_last_name_nn_demo NOT NULL , email VARCHAR2(25) CONSTRAINT emp_email_nn_demo NOT NULL , phone_number VARCHAR2(20) , hire_date DATE D

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

1 package com.csii.pweb.query.action; 2 3 import java.io.BufferedReader; 4 import java.io.FileNotFoundException; 5 import java.io.FileReader; 6 import java.io.IOException; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 11 public class Ja

oracle 11g drop table 后闪回

--初始化数据 drop table test purge; create table test as select * from dba_objects; delete from test where object_id is null; alter table test add constraint pk_test_object_id primary key(object_id); create index ind_t_object_name on test(object_name); --

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 TRADE_RECODE FOR EACH ROW BEGIN  :NEW.ID:=SEQ_TRADE_RECODE.NEXTVAL;END; 报错: PL/SQL: Statement ignored PLS-00357: Table,View Or Sequence reference 'SEQ

oracle 11g ocp 笔记(6)-- oracle安全

1.创建和管理账号 用户.用户帐户.模式术语 用户.用户帐户.模式常替换使用,但这些术语并不完全相同. 用户通过建立到实例的会话连接到用户帐户,并基于用户帐户名登录. 模式(schema)是用户帐户拥有的一组对象. 用户账户的属性:   用户名 身份认证方式 默认表空间 表空间配额 临时表空间 用户配置文件 状态 1)用户名: 字母开头.只能包含字母.数字.美元符$.下划线_,不能是保留字,字母自动转化成大写,不按规则则需要双引号指定,不能超过30个字符.一旦创建不能修改. ·身份验证方法: 2

IOS-sqlite3数据库: create table数据库表及对数据库表的增删改查(create/insert/delete)

1.步骤是: 先加入sqlite开发库libsqlite3.dylib, 新建或打开数据库, 创建数据表, 插入数据, 查询数据并打印 2.sqlite 的方法 sqlite3          *db, 数据库句柄,跟文件句柄FILE很类似 sqlite3_stmt *stmt, 这个相当于ODBC的Command对象,用于保存编译好的SQL语句 sqlite3_open(),    打开数据库,没有数据库时创建.sqlite3_exec(),    执行非查询的sql语句Sqlite3_st

Oracle 11g安装GI后,运行roothas.pl脚本报错libcap.so.1找不到

环境:RHEL6.4 + Oracle 11.2.0.3问题:需求是文件系统迁移到ASM,在安装GI后,运行roothas.pl脚本报错 1.运行root.sh后,按提示运行roothas.pl报错: [[email protected] mnt]# /u01/app/11.2.0/grid/crs/install/roothas.pl Using configuration parameter file: /u01/app/11.2.0/grid/crs/install/crsconfig_p

MongoDB 实现 create table tab2 as select

1. var result = db.foo.aggregate(...);db.bar.insert(result.result); 2. var temp1 = db.mtb1.find(name:"joe");while(temp1.hasNext()) db.tab2.insert(temp1.next()); 相当于sql中:create table tab2 as select * from mtb1 where name='joe'; 原文地址:https://www.c