473,231 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

The prefered way to update a database, (MySQL)

Hi,

We have a small utility that uses a database.
We are happy with the structure of the database but we cannot predict any
updates.

What we are particularly concerned about are updates vs. new installs.

We have an install that does something like...

.....
CREATE DATABASE "my_db" DEFAULT CHARACTER SET latin1 COLLATE
latin1_swedish_ci;
USE my_db;

CREATE TABLE IF NOT EXISTS "my_table" (
"id" int(11) NOT NULL default '0'
)
....
if we create a new field we would update our install script

....
CREATE TABLE IF NOT EXISTS "my_table" (
"id" int(11) NOT NULL default '0',
"anotherid" int(11) NOT NULL default '0'
)
....

But what about users that already have the table? how can I check if the
field needs to be added?

ALTER TABLE "my_table" ADD "anotherid" INT( 11 ) NOT NULL default '0'

Ideally the script would be self sufficient so we don't have versions to
compare. I would check if the field exists, and if it doesn't alter the
table.

Many thanks in advance.

Simon

Jul 17 '05 #1
2 1787
NC
Simon wrote:

We have an install that does something like...

....
CREATE DATABASE "my_db" DEFAULT CHARACTER SET latin1 COLLATE
latin1_swedish_ci;
USE my_db;

CREATE TABLE IF NOT EXISTS "my_table" (
"id" int(11) NOT NULL default '0'
)
...

if we create a new field we would update our install script

...
CREATE TABLE IF NOT EXISTS "my_table" (
"id" int(11) NOT NULL default '0',
"anotherid" int(11) NOT NULL default '0'
)
...

But what about users that already have the table? how can I
check if the field needs to be added?


Use any of the following:

SHOW COLUNMS;
http://dev.mysql.com/doc/mysql/en/show-columns.html

SHOW CREATE TABLE;
http://dev.mysql.com/doc/mysql/en/sh...ate-table.html

DESCRIBE my_table;
http://dev.mysql.com/doc/mysql/en/describe.html

The output will differ, but the names of columns will be listed
anyway.

For a more PHP-oriented solution, see documentation for
mysql_list_fields():

http://www.php.net/mysql_list_fields

Cheers,
NC

Jul 17 '05 #2
>We have a small utility that uses a database.
We are happy with the structure of the database but we cannot predict any
updates.

What we are particularly concerned about are updates vs. new installs.
You can't do what you want in SQL alone. Or at least not easily.
With a better programming language that interfaces to SQL (and
this includes PHP), you can.
But what about users that already have the table? how can I check if the
field needs to be added?
You can do things like:
- Query "show tables" and see if the table is there.
- Query "describe table_name" or "show columns from
table_name like 'column_name'" and see what columns are there.
- Query "select field_in_question from table_name" and see if it
gets an error (due to field_in_question not being present).
- In MySQL 5, query the table information_schema.COLUMNS.

This requires that you get a result set and then manipulate it
in PHP, rather than doing it all in SQL.

And if the field isn't there, run something like:ALTER TABLE "my_table" ADD "anotherid" INT( 11 ) NOT NULL default '0' Ideally the script would be self sufficient so we don't have versions to
compare. I would check if the field exists, and if it doesn't alter the
table.


Gordon L. Burditt
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark | last post by:
A beginner in this area, I have been able to read a record from a MySQL database and populate an HTML form (wow!). Now, my goal is to allow the user to edit the contents of the form and then...
19
by: Westcoast Sheri | last post by:
To keep track of how many fruits my visitors buy, I use a mySQL database (2 columns: "fruit" and "quantity")....so can we make these following mySQL queries work somehow? (visitor buys 5...
0
by: Jim S. | last post by:
I'm having a horrible time simply inserting the date in a MySQL database through a Visual Basic program. I have a table, called "ORDERS"; "ID" is the primary key. I'm trying the insert the date,...
5
by: jayson_13 | last post by:
Hi, I need to implement a counter and i face problem of locking so hope that u guys can help me. I try to do test like this : 1st connection SELECT * FROM nextkey WHERE tblname = 'PLCN'...
2
by: Carlos Eduardo Peralta | last post by:
Hello: Can i update a MySQL database with just copy the files MYI MYD and FRM in the right dir? I know this work. The question is how MySQL manage the user that are usig that database in that...
4
by: Stephen | last post by:
Hello People, Using MS Access 2003 VBA I get the error 3020 Update or CancelUpdate without AddNew or Edit when I run through the following code. Can anyone help suggest anything to try? Thanks....
4
by: whitemoss | last post by:
Hi, I've made some changes to my coding..but unfortunately, there were errors when compiling it..dunno how to solve it..hope anyone can help me...the errors: client.c: In function senddata:...
1
tolkienarda
by: tolkienarda | last post by:
i need to update a database table using variables in unusual places here are the update statements mysql_query("UPDATE 'grades' SET '$class' = '$grade' WHERE student='$student'");...
11
by: stantron | last post by:
Setup: I only have one database with one table in it. The first page has a form that adds a record (w/ 6 fields in it) to the mySQL database's lone table via PHP. This works fine. I also have a PHP...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.