Oracle Create Table如果不存在

时间:2021-06-06 08:47:06

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?

任何人都可以指出我使用正确的语法,只有在数据库中当前不存在时才能创建表吗?

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.

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

4 个解决方案

#1


7  

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.

通常,检查表是否存在没有多大意义,因为不应在运行时创建对象,应用程序应该知道在安装时创建了哪些对象。如果这是安装的一部分,您应该知道过程中任何位置存在哪些对象,因此您不需要检查表是否已存在。

If you really need to, however,

但是,如果你真的需要,

  • You can attempt to create the table and catch the `ORA-00955: name is already used by an existing object" exception.
  • 您可以尝试创建表并捕获`ORA-00955:名称已被现有对象使用“异常。
  • 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.
  • 您可以查询USER_TABLES(或ALL_TABLES或DBA_TABLES,具体取决于您是否正在创建其他用户拥有的对象以及您在数据库中的权限)以检查该表是否已存在。
  • 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.
  • 您可以尝试在创建表之前删除该表,并捕获“ORA-00942:表或视图不存在”异常(如果不存在)。

#2


1  

You can do this with the Following Procedure -

您可以使用以下程序执行此操作 -

BEGIN
    BEGIN
         EXECUTE IMMEDIATE 'DROP TABLE <<Your Table Name>>';
    EXCEPTION
         WHEN OTHERS THEN
                IF SQLCODE != -942 THEN
                     RAISE;
                END IF;
    END;

    EXECUTE IMMEDIATE '<<Your table creation Statement>>';

END;

Hope this may help you.

希望这可以帮到你。

#3


1  

@Archie I would like to answer your question. @Piyas De Sorry for stealing your code :).

@Archie我想回答你的问题。 @Piyas De很抱歉偷你的代码:)。

Just a little update of @Piyas De answer.

只是@Piyas De回复的一点点更新。

BEGIN
    BEGIN
        EXECUTE IMMEDIATE '<<Your table creation Statement>>';
    EXCEPTION
        WHEN OTHERS THEN
            IF SQLCODE == -955 THEN
                RAISE;
            END IF;
    END;
END;

#4


0  

try
{
    // insert query for insert new record in your table 
}
catch(Exception Ex)
{
    //if it throw exception then catch it

     int s=Ex.getErrorCode(); // check it for 903 error

     //903 is table not existing error in oracle11g

 // then create your new table here otherwise if table present then record get stored in database
}

#1


7  

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.

通常,检查表是否存在没有多大意义,因为不应在运行时创建对象,应用程序应该知道在安装时创建了哪些对象。如果这是安装的一部分,您应该知道过程中任何位置存在哪些对象,因此您不需要检查表是否已存在。

If you really need to, however,

但是,如果你真的需要,

  • You can attempt to create the table and catch the `ORA-00955: name is already used by an existing object" exception.
  • 您可以尝试创建表并捕获`ORA-00955:名称已被现有对象使用“异常。
  • 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.
  • 您可以查询USER_TABLES(或ALL_TABLES或DBA_TABLES,具体取决于您是否正在创建其他用户拥有的对象以及您在数据库中的权限)以检查该表是否已存在。
  • 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.
  • 您可以尝试在创建表之前删除该表,并捕获“ORA-00942:表或视图不存在”异常(如果不存在)。

#2


1  

You can do this with the Following Procedure -

您可以使用以下程序执行此操作 -

BEGIN
    BEGIN
         EXECUTE IMMEDIATE 'DROP TABLE <<Your Table Name>>';
    EXCEPTION
         WHEN OTHERS THEN
                IF SQLCODE != -942 THEN
                     RAISE;
                END IF;
    END;

    EXECUTE IMMEDIATE '<<Your table creation Statement>>';

END;

Hope this may help you.

希望这可以帮到你。

#3


1  

@Archie I would like to answer your question. @Piyas De Sorry for stealing your code :).

@Archie我想回答你的问题。 @Piyas De很抱歉偷你的代码:)。

Just a little update of @Piyas De answer.

只是@Piyas De回复的一点点更新。

BEGIN
    BEGIN
        EXECUTE IMMEDIATE '<<Your table creation Statement>>';
    EXCEPTION
        WHEN OTHERS THEN
            IF SQLCODE == -955 THEN
                RAISE;
            END IF;
    END;
END;

#4


0  

try
{
    // insert query for insert new record in your table 
}
catch(Exception Ex)
{
    //if it throw exception then catch it

     int s=Ex.getErrorCode(); // check it for 903 error

     //903 is table not existing error in oracle11g

 // then create your new table here otherwise if table present then record get stored in database
}