Oracle Create Table如果不存在 [英] Oracle Create Table if it does not exist

查看:822
本文介绍了Oracle Create Table如果不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以指定正确的语法来创建一个表,只有当它不存在于数据库中吗?

Can anyone point me to the right syntax to use in order to create a table only if it does not currently exist in the database?

我目前正在编程一个Java GUI,以便连接到Oracle并在我的数据库上执行语句,我想知道是否将其实现为Java约束,一个SQLPlus约束。

I'm currently programming a Java GUI in order to connect to Oracle and execute statements on my database and I'm wondering if I would implement this as a Java constraint or a SQLPlus constraint.

推荐答案

通常,检查表是否存在并没有什么意义,因为对象不应在运行时创建,应用程序应该知道在安装时创建的对象。如果这是安装的一部分,你应该知道在进程的任何时候存在什么对象,所以你不需要检查表是否已经存在。

Normally, it doesn't make a lot of sense to check whether a table exists or not because objects shouldn't be created at runtime and the application should know what objects were created at install time. If this is part of the installation, you should know what objects exist at any point in the process so you shouldn't need to check whether a table already exists.

如果你真的需要


  • 您可以尝试创建表并捕获`ORA-00955:name已被 c> ALL_TABLES 或可以查询 USER_TABLES DBA_TABLES ,具体取决于您是否正在创建其他用户拥有的对象和您在数据库中的权限)来检查表是否已存在。

  • 您可以尝试在创建之前删除该表,如果没有则捕获ORA-00942:表或视图不存在异常。

  • You can attempt to create the table and catch the `ORA-00955: name is already used by an existing object" exception.
  • You can query USER_TABLES (or ALL_TABLES or DBA_TABLES depending on whether you are creating objects owned by other users and your privileges in the database) to check to see whether the table already exists.
  • You can try to drop the table before creating it and catch the `ORA-00942: table or view does not exist" exception if it doesn't.

这篇关于Oracle Create Table如果不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆