Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
160 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.
  • 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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...