473,325 Members | 2,480 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,325 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 1789
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.