Welcome to dbFreaks.com!
FAQFAQ   SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

PLS HELP! - show databases and tables and columns

 
   Database Help (Home) -> mySQL RSS
Next:  Insert fail  
Author Message
MuZZy

External


Since: Feb 20, 2005
Posts: 2



(Msg. 1) Posted: Sun Feb 20, 2005 6:34 pm
Post subject: PLS HELP! - show databases and tables and columns
Archived from groups: mailing>database>mysql (more info?)

Hi,

I just wonder if someone can help me with this:
I need to create a sql script which will run when user installs/upgrades my app.
User may already have the database and tables tructure setup on the server, or may not.

The script needs to conditionally create database, conditionally create tables, and conditionally
add columns to the tables if needed.

I could perform this on MS SQL, as the master database has all info about all other
databases/tables/schemas. BUt in MySQL i don't see such an option.

So, i would need this(for description i use a mix of SQL and pseudo-language):

// -------------- CODE START
IF NOT EXISTS( MyDatabase ) CREATE MyDatabase;
USE MyDatabase;
IF NOT EXISTS( MyTable )
  CREATE MyTable (
   Field1 INT NOT NULL,
   Field2 VARCHAR(20),
   Field3 INT
  )TYPE='MyISAM'

IF NOT EXISTS (Field3 IN MyTable)
  ALTER TABLE MyTable ADD Field3 INT
..... etc...
// -------------- CODE END

So if someone could show me the MySQL way to do that, i would really appreciate that!

Thank you,
Andrey<!-- ~MESSAGE_AFTER~ -->

 >> Stay informed about: PLS HELP! - show databases and tables and columns 
Back to top
Login to vote
laitkor

External


Since: Feb 15, 2005
Posts: 5



(Msg. 2) Posted: Sun Feb 20, 2005 11:20 pm
Post subject: Re: PLS HELP! - show databases and tables and columns [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You could do it in a shell script. Your pseudo code is along the path
to implement it.
--
Jay
http://d2t2fish.blogspot.com

 >> Stay informed about: PLS HELP! - show databases and tables and columns 
Back to top
Login to vote
Bill Karwin1

External


Since: Jun 17, 2004
Posts: 42



(Msg. 3) Posted: Tue Feb 22, 2005 6:34 pm
Post subject: Re: PLS HELP! - show databases and tables and columns [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

MuZZy wrote:
 > I need to create a sql script which will run when user installs/upgrades
 > my app.
 > User may already have the database and tables tructure setup on the
 > server, or may not.
  > The script needs to conditionally create database, conditionally create
  > tables, and conditionally add columns to the tables if needed.

You can get partway there using existing MySQL syntax.
See the respect web pages for CREATE DATABASE, DROP TABLE, CREATE TABLE,
etc.

<a style='text-decoration: underline;' href="http://dev.mysql.com/doc/mysql/en/create-database.html" target="_blank">http://dev.mysql.com/doc/mysql/en/create-database.html</a>
CREATE DATABASE IF NOT EXISTS databaseName ...

<a style='text-decoration: underline;' href="http://dev.mysql.com/doc/mysql/en/create-table.html" target="_blank">http://dev.mysql.com/doc/mysql/en/create-table.html</a>
CREATE TABLE IF NOT EXISTS tableName ...

<a style='text-decoration: underline;' href="http://dev.mysql.com/doc/mysql/en/drop-table.html" target="_blank">http://dev.mysql.com/doc/mysql/en/drop-table.html</a>
DROP TABLE IF EXISTS tableName ...

But there is no equivalent mechanism for conditional creation of columns.

At least one widely-used project comes to mind that does what you're
describing; runs a script to bring a database schema up to date with
current table/field definitions: Bugzilla.

The setup procedure for Bugzilla involves running a script
checksetup.pl, which can upgrade previous Bugzilla installations to the
current version's required schema. Basically, it does this with
ordinary ALTER TABLE ADD COLUMN statements, but ignores any error
returned (as would happen if the column already exists).

If you want to know more, download Bugzilla at
<a style='text-decoration: underline;' href="http://www.bugzilla.org/download/#stable," target="_blank">http://www.bugzilla.org/download/#stable,</a> open the distribution, and
read their checksetup.pl script.

Regards,
Bill K.<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: PLS HELP! - show databases and tables and columns 
Back to top
Login to vote
MuZZy

External


Since: Feb 20, 2005
Posts: 2



(Msg. 4) Posted: Tue Feb 22, 2005 8:40 pm
Post subject: Re: PLS HELP! - show databases and tables and columns [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

d@t@ wrote:
 > You could do it in a shell script. Your pseudo code is along the path
 > to implement it.
 > --
 > Jay
<font color=purple> > <a style='text-decoration: underline;' href="http://d2t2fish.blogspot.com</font" target="_blank">http://d2t2fish.blogspot.com</font</a>>
 >

I know that, but i asked how do i translate my pseudo-code to MySQL SQL language?
I don't know how to check if database exists or if table exists or if table's column exists<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: PLS HELP! - show databases and tables and columns 
Back to top
Login to vote
Display posts from previous:   
   Database Help (Home) -> mySQL All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]