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 > How to know if a table or column exists

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-24-04, 13:06
july9th july9th is offline
Registered User
 
Join Date: Mar 2004
Posts: 5
How to know if a table or column exists

I am wanting to create rerunnable database structure scripts that will create a table, add a column, etc... I want to find out how to determine the presence of a database object before I try to create it.

In MS*SQL, I'd do something like this:

if NOT exists (SELECT sysobjects.name AS TableName, syscolumns.name AS ColumnName
FROM syscolumns INNER JOIN
sysobjects ON syscolumns.id = sysobjects.id
WHERE sysobjects.name = 'party'
AND syscolumns.name = 'email')
BEGIN
ALTER TABLE dbo.party ADD email varchar(255) NULL
END
GO

Is there some equivalent way to do this in MySQL?

Thanks immensely for any help here.
Reply With Quote
  #2 (permalink)  
Old 03-24-04, 15:36
ika ika is offline
Registered User
 
Join Date: Oct 2003
Location: Slovakia
Posts: 482
Re: How to know if a table or column exists

Quote:
Originally posted by july9th
I am wanting to create rerunnable database structure scripts that will create a table, add a column, etc... I want to find out how to determine the presence of a database object before I try to create it.

In MS*SQL, I'd do something like this:

if NOT exists (SELECT sysobjects.name AS TableName, syscolumns.name AS ColumnName
FROM syscolumns INNER JOIN
sysobjects ON syscolumns.id = sysobjects.id
WHERE sysobjects.name = 'party'
AND syscolumns.name = 'email')
BEGIN
ALTER TABLE dbo.party ADD email varchar(255) NULL
END
GO

Is there some equivalent way to do this in MySQL?

Thanks immensely for any help here.
show databases - this shows all databases in MySQL
show tables - this shows all tables in specific database
describe <tablename> - this shows all columns (names) in specific table
Reply With Quote
  #3 (permalink)  
Old 03-24-04, 15:44
july9th july9th is offline
Registered User
 
Join Date: Mar 2004
Posts: 5
Re: How to know if a table or column exists

Quote:
Originally posted by ika
show databases - this shows all databases in MySQL
show tables - this shows all tables in specific database
describe <tablename> - this shows all columns (names) in specific table
thanks... this is a decent start... but how would I weave any one of these into a script I can run on various servers?

For example, how might Describe mytable output be used in some form of conditional structure to either decide to add or not add the column? the output of this function appears to be a query - how do I intercept the output and make use of it?
Reply With Quote
  #4 (permalink)  
Old 03-24-04, 17:07
willi.st willi.st is offline
Registered User
 
Join Date: Feb 2004
Location: Vienna/Austria
Posts: 7
the MySQL Documentation says:

CREATE DATABASE [IF NOT EXISTS] dbname

http://www.mysql.com/documentation/m...REATE_DATABASE


CREATE TABLE [IF NOT EXISTS] tablename .... columns

http://www.mysql.com/documentation/m...l#CREATE_TABLE


hope this helps,

:-) Willi
Reply With Quote
  #5 (permalink)  
Old 03-24-04, 17:28
july9th july9th is offline
Registered User
 
Join Date: Mar 2004
Posts: 5
Quote:
Originally posted by willi.st
the MySQL Documentation says:

CREATE DATABASE [IF NOT EXISTS] dbname

http://www.mysql.com/documentation/m...REATE_DATABASE


CREATE TABLE [IF NOT EXISTS] tablename .... columns

http://www.mysql.com/documentation/m...l#CREATE_TABLE


hope this helps,

:-) Willi

Well, it does help but still doesn't quite hit the nail on the head, since I am wanting to know if my COLUMN already exists in an existing table...

ALTER [IGNORE] TABLE tbl_name
alter_specification [, alter_specification] ...

alter_specification:
ADD [COLUMN] create_definition [FIRST | AFTER col_name ]

and unfortunately, ADD COLUMN doesn't seem to support the IF NOT EXISTS control... so... so far, I'm still stuck.
Reply With Quote
  #6 (permalink)  
Old 03-25-04, 03:16
willi.st willi.st is offline
Registered User
 
Join Date: Feb 2004
Location: Vienna/Austria
Posts: 7
hm, sorry for my misunderstanding ....

as far as i can see, there is no way to resolve the problem with extensions (or functions) i MySQL itself. I would embed the solution in a script (shell-script/awk, php, etc.) or a programming language (java, c, etc.) to extract the relevant information from DESCRIBE tablename or SHOW COLUMS FROM table [FROM database] and to modify the database dynamically.

ok, sorry, that's all i know for now ....

Willi
Reply With Quote
  #7 (permalink)  
Old 03-25-04, 10:58
july9th july9th is offline
Registered User
 
Join Date: Mar 2004
Posts: 5
Red face CALL TO ALL SMARTIES

Quote:
Originally posted by willi.st
hm, sorry for my misunderstanding ....

as far as i can see, there is no way to resolve the problem with extensions (or functions) i MySQL itself. I would embed the solution in a script (shell-script/awk, php, etc.) or a programming language (java, c, etc.) to extract the relevant information from DESCRIBE tablename or SHOW COLUMS FROM table [FROM database] and to modify the database dynamically.

ok, sorry, that's all i know for now ....

Willi

grrrr.... I can't imagine there not being an easy way to know if a column exists.... come on, you smarties... someone come forward with the answer. PLEASE?!?!?
Reply With Quote
  #8 (permalink)  
Old 03-26-04, 12:08
ika ika is offline
Registered User
 
Join Date: Oct 2003
Location: Slovakia
Posts: 482
Re: CALL TO ALL SMARTIES

Quote:
Originally posted by july9th
grrrr.... I can't imagine there not being an easy way to know if a column exists.... come on, you smarties... someone come forward with the answer. PLEASE?!?!?
MySQL has not a data dictionary as Oracle or MSSQL.
Reply With Quote
  #9 (permalink)  
Old 03-30-04, 04:36
ajandris ajandris is offline
Registered User
 
Join Date: Mar 2004
Posts: 8
Re: CALL TO ALL SMARTIES

Quote:
Originally posted by july9th
grrrr.... I can't imagine there not being an easy way to know if a column exists.... come on, you smarties... someone come forward with the answer. PLEASE?!?!?
Use SELECT * FROM .... LIMIT 0 ... and check what column names exists ...
Reply With Quote
  #10 (permalink)  
Old 03-30-04, 10:04
july9th july9th is offline
Registered User
 
Join Date: Mar 2004
Posts: 5
Re: CALL TO ALL SMARTIES

Quote:
Originally posted by ajandris
Use SELECT * FROM .... LIMIT 0 ... and check what column names exists ...
"... and check what column names exist ...", HOW?

I need this to operate in a script... what conditional commands can I place around this SELECT that will let me inquire as to the presence of a certain column? Can you post an example of how to script your suggetion?
Reply With Quote
  #11 (permalink)  
Old 03-30-04, 18:13
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,606
Re: CALL TO ALL SMARTIES

Quote:
Originally posted by july9th
"... and check what column names exist ...", HOW?

I need this to operate in a script... what conditional commands can I place around this SELECT that will let me inquire as to the presence of a certain column? Can you post an example of how to script your suggetion?
I don't think this can be done using just MySQL.

If you can use a scripting language (like Perl or PHP), then it is easy to see the MySQL table structure. Do a SHOW TABLE and examine the output to your heart's content!

-PatP
Reply With Quote
  #12 (permalink)  
Old 03-31-04, 07:23
ajandris ajandris is offline
Registered User
 
Join Date: Mar 2004
Posts: 8
Re: CALL TO ALL SMARTIES

Quote:
Originally posted by july9th
"... and check what column names exist ...", HOW?

I need this to operate in a script... what conditional commands can I place around this SELECT that will let me inquire as to the presence of a certain column? Can you post an example of how to script your suggetion?
It depends on language U are using. For example, Java:

interface java.sql.DatabaseMetaData has method

public ResultSet getColumns(String catalog,
String schemaPattern,
String tableNamePattern,
String columnNamePattern)
throws SQLException

More info:
http://java.sun.com/j2se/1.4.2/docs/api/index.html
see java.sql

For PHP useful function is function mysql_list_fields().

Last edited by ajandris; 03-31-04 at 07:26.
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