If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Check the existence of a table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-08-07, 18:04
Genom Genom is offline
Registered User
 
Join Date: Jul 2007
Posts: 22
Check the existence of a table

Hi;
I made a lot of research but I couldnt find what I want. Is there any function in MySQL which detects is there any table called "x" or not? I wnat to detect it and if it doesnt exist a SQL sentence will create a table. How can I do that? (I think a stored procedure can help me but how can I detect the existence of a table?)
Reply With Quote
  #2 (permalink)  
Old 07-08-07, 18:10
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
select count(*)
  from information_schema.tables
 where table_schema = 'db_name'
   and table_name = 'x'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 07-08-07, 18:19
Genom Genom is offline
Registered User
 
Join Date: Jul 2007
Posts: 22
Thanks Im tring it...
Reply With Quote
  #4 (permalink)  
Old 07-08-07, 18:30
Genom Genom is offline
Registered User
 
Join Date: Jul 2007
Posts: 22
How can use it with SQL commands?
I tried something like this:
IF((select count(*)
from information_schema.tables
where table_schema = 'db_name'
and table_name = 'x')=0,CREATE TABLE....)
But it didnt work. I dont want to use a stored procedure. Is there any other way?
Reply With Quote
  #5 (permalink)  
Old 07-08-07, 18:48
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
other ways?

1. run the query, look at the count, and if it's zero, run the CREATE

2. embed the query in a language like php which has IF statements

3. use a stored proc
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 07-08-07, 20:13
dbmab dbmab is offline
Registered User
 
Join Date: Apr 2006
Location: Denver, Co. USA
Posts: 240
Use -
Code:
CREATE TABLE IF NOT EXISTS tbl_name ...
Reply With Quote
  #7 (permalink)  
Old 07-09-07, 04:09
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
Of course if you just want to find out if the table exists (i.e. not create it but produce a list of available tables?) then using the last option is not valid.

If you have MySQL 5+ use the information_schema tables. Definitely the way to go...
Reply With Quote
  #8 (permalink)  
Old 07-09-07, 07:11
Genom Genom is offline
Registered User
 
Join Date: Jul 2007
Posts: 22
thanks very much...
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On