-2

The SQL query CREATE TABLE mytable IF NOT EXISTS returns true (in my PHP app) if the query ran correctly, but not if the table was actually created.

How can I tell if the table was created without running another query? I don't want to run double the amount of queries every single time my app runs.

Jared
  • 2,978
  • 4
  • 26
  • 45
  • @Furhad "Give your code"? What code would I possibly need to give? – Jared Oct 07 '13 at 10:26
  • 1
    As far as I know, DDL queries do not return anything. They may trigger a warning or error. Can you please provide more context? – Álvaro González Oct 07 '13 at 10:26
  • @Glavić My question is to check without running another query. That answer says to use another query. – Jared Oct 07 '13 at 10:27
  • @ÁlvaroG.Vicario `CREATE TABLE IF NOT EXISTS` returns `true` if the query ran, not if it actually worked. I've tagged the question with `php`, but it should apply to any language. – Jared Oct 07 '13 at 10:28
  • @Jazza: your original question was also "if not, what query do you advise"... – Glavić Oct 07 '13 at 10:30
  • "More context" means "what are your reasons?" – Your Common Sense Oct 07 '13 at 10:30
  • where is the table structure. how can create a table without table structure – Md. Sahadat Hossain Oct 07 '13 at 10:31
  • Check: http://stackoverflow.com/questions/18738909/mysql-select-if-table-exists/18740546#18740546 – Jhanvi Oct 07 '13 at 10:34
  • If MySQL can't create the table it will return an error. Since you're using the `IF NOT EXISTS` clause the only reason MySQL will not return an error and not create a table is if the table already exists. If you want to know that you'll need to check for its existence before you attempt to create it. I don't really understand the purpose of your question. –  Oct 07 '13 at 10:34
  • In some applications you may want to automatically create a database table such as the very first time you run the app. Who wants to manually create tables every time you deploy? – Jared Oct 07 '13 at 10:36
  • @Glavić True. I removed that secondary question to avoid confusion. – Jared Oct 07 '13 at 10:38
  • @Jazza - Alright, the `returns true` part refers to some undisclosed PHP code but I guess it isn't relevant: your question is how to perform two tasks with a single query. You'll possibly need to write a function. – Álvaro González Oct 07 '13 at 10:39
  • (continued) Also I want to insert some default rows when I create my tables, such as a root user in a user table. – Jared Oct 07 '13 at 10:40
  • @ÁlvaroG.Vicario Indeed. The language shouldn't matter. I'm using PHP (I tagged the question with it) but it should apply to any language that can use SQL. – Jared Oct 07 '13 at 10:40
  • I reckon this question better be closed, only to stop all the pointless answers. – Your Common Sense Oct 07 '13 at 11:16
  • I was going to mark one of the answers as the solution, but they appeared to have deleted it. – Jared Oct 09 '13 at 01:51

1 Answers1

-1

Try this

CREATE TABLE IF NOT EXISTS `table_name` (
`table field defination`
) ENGINE = `engine_name`
Md. Sahadat Hossain
  • 3,210
  • 4
  • 32
  • 55