在编写程序时,数据库结构会经常变化,所以经常需要编写一些数据库脚本,编写完成后需发往现场执行,如果已经存在或者重复执行,有些脚本会报错,所以需要判断其是否存在,现在我就把经常用到的一些判断方法和大家分享下:

一。判断Oracle表是否存在的方法

declare tableExistedCount number;   --声明变量存储要查询的表是否存在
begin
select count(1) into tableExistedCount from user_tables t where t.table_name = upper('Test'); --从系统表中查询当表是否存在
if tableExistedCount = 0 then --如果不存在,使用快速执行语句创建新表
execute immediate
'create table Test --创建测试表
(ID number not null,Name = varchar2(20) not null)';
end if;
end;

二。判断Oracle表中的列是否存在的方法

declare columnExistedCount number;   --声明变量存储要查询的表中的列是否存在
begin
--从系统表中查询表中的列是否存在
select count(1) into columnExistedCount from user_tab_columns t where t.table_name = upper('Test') and t.column_name = upper('Age');
--如果不存在,使用快速执行语句添加Age列
if columnExistedCount = 0 then
execute immediate
'alter table Test add age number not null';
end if;
end;
DECLARE
num NUMBER;
BEGIN
SELECT COUNT(1)
INTO num
from cols
where table_name = upper('tableName')
and column_name = upper('columnName');
IF num > 0 THEN
execute immediate 'alter table tableName drop column columnName';
END IF;
END;

三。判断Oracle表是否存在主键的方法

declare primaryKeyExistedCount number;   --声明变量存储要查询的表中的列是否存在
begin
--从系统表中查询表是否存在主键(因一个表只可能有一个主键,所以只需判断约束类型即可)
select count(1) into primaryKeyExistedCount from user_constraints t where t.table_name = upper('Test') and t.constraint_type = 'P';
--如果不存在,使用快速执行语句添加主键约束
if primaryKeyExistedCount = 0 then
execute immediate
'alter table Test add constraint PK_Test_ID primary key(id)';
end if;
end;

四。判断Oracle表是否存在外键的方法

declare foreignKeyExistedCount number;   --声明变量存储要查询的表中的列是否存在
begin
--从系统表中查询表是否存在主键(因一个表只可能有一个主键,所以只需判断约束类型即可)
select count(1) into foreignKeyExistedCount from user_constraints t where t.table_name = upper('Test') and t.constraint_type = 'R' and t.constraint_name = '外键约束名称';
--如果不存在,使用快速执行语句添加主键约束
if foreignKeyExistedCount = 0 then
execute immediate
'alter table Test add constraint 外键约束名称 foreign key references 外键引用表(列)';
end if;
end;

Oracle判断表、列、主键是否存在的方法的更多相关文章

  1. Oracle 获取表的主键、外键以及唯一约束条件

    Oracle 获取表的主键.外键以及唯一约束条件 Select a.Owner 主键拥有者, a.table_name 主键表, b.Column_Name 主键列, b.Constraint_Nam ...

  2. Oracle heap 表的主键 dump 分析

    1. 创建heap 表: create table t1 (id char(10) primary key,a1 char(10),a2 char(10),a3 char(10)); SQL> ...

  3. Oracle 给表添加主键和使ID自增、触发器、创建结构一样的表

    1.关于主键:在建表时指定primary key字句即可:create table test( id number(6) primary key, name varchar2(30));如果是对于已经 ...

  4. Oracle建表时主键自增

    1.创建表 /*第一步:创建表格*/ create table t_user( id int primary key, --主键,自增长 username ), password ), type ) ...

  5. oracle建表设置主键自增

    首先创建一张表 create table member( memberId number primary key, memberMail )not null, memberName ) not nul ...

  6. Oracle使用游标为所有用户表添加主键语句

    应用场合:数据表新增自增一主键能加快数据表的访问速度,而且是整形的索引速度最快.本程序适合在导入Oracle数据库时删除不存在主键的情况下运行. 代码说明:所有的表主键字段名都设置为ID,如果已存在I ...

  7. SQL 数据库 学习 007 通过一个示例简单介绍什么是字段、属性、列、元组、记录、表、主键、外键 (上)

    SQL 数据库 学习 007 通过一个示例简单介绍什么是字段.属性.列.元组.记录.表.主键.外键 (上) 我们来介绍一下:数据库是如何存储数据的. 数据库是如何存储数据的 来看一个小例子 scott ...

  8. oracle数据库创建表且主键自增

    唠叨几句:几年前的知识忘却了,整理一下笔记,提供一下方便 1.创建数据库表 设置主键 create table users( userid number(10) primary key, /*主键,自 ...

  9. Oracle表添加主键、外键

    1.创建表的同时创建主键约束 (1)无命名 create table student ( studentid int primary key not null, studentname varchar ...

  10. mysql insert插入时实现如果数据表中主键重复则更新,没有重复则插入的四种方法

    [CSDN下载] Powerdesigner 设计主键code不能重复等问题 [CSDN博客] Oracle中用一个序列给两个表创建主键自增功能的后果 [CSDN博客] MySQL自增主键删除后重复问 ...

随机推荐

  1. 通过spark-sql快速读取hive中的数据

    1 配置并启动 1.1 创建并配置hive-site.xml 在运行Spark SQL CLI中需要使用到Hive Metastore,故需要在Spark中添加其uris.具体方法是将HIVE_CON ...

  2. 熟悉一下oncontextmenu事件的知识

    定义和使用 只要点击鼠标右键,就触发oncontextmenu事件并打开上下文菜单. 需要注意的是:所有主流浏览器都支持oncontextmenu事件,但其中的contextmenu元素只有FireB ...

  3. 哪个先执行:@PostConstruct和@Bean的initMethod?

    结论: /** * step1:执行构造函数 * step2:执行使用@PostConstruct注解修饰的方法[如果有多个,则执行顺序不确定] * step3:执行@Bean注解中initMetho ...

  4. api.closeFrame

    关闭frame closeFrame({params}) params name: 类型:字符串 默认值:无 描述:(可选项)frame 名字,不传时关闭当前 frame 示例代码 api.close ...

  5. vs中插件影响代码自动创建后台事件问题

    CSS Tools 启用之后会影响代码自动创建后台事件,禁用之后解决.禁用之后鼠标悬浮不能看图片,颜色也不能展示

  6. 未能加载“xxx”程序集

    找到程序集名称,去项目文件中查找是否拥有.

  7. Dubbo下载-从missing artifactId说起

    项目pom文件引入dubbo 报 missing artifactId https://github.com/dangdangdotcom/dubbox 从GitHub上直接下载解压包, 最好下载分支 ...

  8. js 实现复制到粘贴板功能

    前言:js 或者 jquery 都可以实现的复制到粘贴板功能,有时还想要有换行等格式(同 textarea) 网站地址:我的个人vue+element ui demo网站 github地址:yuleG ...

  9. java单例模式的心得

    由于设计模式对于java高级开发人员来说是非常重要的,网上也有很多关于设计模式的文章,博客等.所以,首先我对相对简单的单例模式做一个简单的总结. 一.实现方式 单例模式的实现方式有3种,分别是饿汉式, ...

  10. 如何灵活利用免费开源图标字体-IcoMoon篇——张鑫旭

    一.温故知新 之前有专门介绍过如何使用类似fontforge软件制作自定义字符字体以及如何在web中实际应用. 不过,文中提到的是利用系统自带的一些特殊字体,如WINGDNG3.ttf字体. 显然,系 ...