PDA

View Full Version : Hypothetical Question


Alli
7-7-05, 09:41 PM
Let's say I have two tables in a database:

Table 1
- values (enum)

Table 2
- value (varchar)
- primary (binary)

Is there a way to make it so that if I put 1/true/yes/etc in the "primary" field in table 2, it will add the value in "value" in table 2 to the enum list for "values" in table 1? I'm sure I could come up with a php script to do this, but I'm wondering if there's a more automatic way. I'm using phpmyadmin.

P.S. Sorry if my terminology is incorrect. I'll be taking a course on databases this fall, but until then I'm just a beginner trying to feel my way around. :(

RTH10260
7-8-05, 01:21 AM
Let's say I have two tables in a database:

Table 1
- values (enum)

Table 2
- value (varchar)
- primary (binary)

Is there a way to make it so that if I put 1/true/yes/etc in the "primary" field in table 2, it will add the value in "value" in table 2 to the enum list for "values" in table 1? I'm sure I could come up with a php script to do this, but I'm wondering if there's a more automatic way. I'm using phpmyadmin.(When you insert into two tables, write two INSERT statements. You can add both commands together in the same .sql script, separatesd by a ';'.INSERT INTO table2 VALUES ('xyz',3);INSERT INTO table1 VALUES ('xyz');and assuming that 'xyz' is listed as valid value for the ENUM.

Note that we assume that nothing bad will happen to interrupt the completion of the second statement.

If the atomicity of the combined operation must be guaranteed (either both statement complete or no modification shows in the database), then auto-commit must be disabled, a start transaction should be given before the first statment and be followed by a commit statement after the second insert. For this to work, you will also need to chose a database engine 'BSD' and not MYISAM for both tables.

I don't know what your intent is with Table 1 with this single column, but if you are interested in knowing what values have ever been inserted into Table 2, make a query on Table 2 using UNIQUE on column 'value'. and if you need to make subtotals on those values, then GROUP BY will help you along.

Alli
7-8-05, 02:09 AM
But is there a way to do this in phpmyadmin, so I don't have to write out the INSERT statements? If not, next time I'll take the time to make a user interface that can do it all at once for me. I'm not too concerned about the atomicity stuff since I'm the only one modifying the database.

RTH10260
7-9-05, 09:56 AM
But is there a way to do this in phpmyadmin, so I don't have to write out the INSERT statements? If not, next time I'll take the time to make a user interface that can do it all at once for me. I'm not too concerned about the atomicity stuff since I'm the only one modifying the database.Interactive use:
For simple operations like adding a few rows to tables, click on the Structures tab and see the list of tables in the database. Use the Insert text link to open a webpage that allows you to add records. Use any Browse link or Query to select some rows of a table, then you can modify them in the displayed list. phpmyadmin wil display the associated sql command. C&p to save the text as sample code, phpmyadmin will also prepare a code snippet for PHP use if you like to take it that way.

Use with sql scripts:
Just create plain text files on your computer with any sequence of sql commands (comment lines with leading #), save it. Convention suggests that one uses '.sql' file extention, but for use with phpmyadmin that is irrelevant. As you may need to edit the files often, just use the regular '.txt' so that you can double-click to open them.

Then in phpmyadmin click on the database name in the left margin to position yourself, then on the SQL button in the heading.
Use the Browse button to locate the script file on your computer, click Go to upload and execute it. If the script is short, just c&p the whole thing into the sql window and click Go.

To efficiently run sql scripts, the MySql implementation contains a row of SET statements to handle values local to the script file, and to control some aspects of the script execution.
Ref: http://dev.mysql.com/doc/mysql/en/set-option.html

Also in interacive more, use the SHOW command (although most of this information can be displayed using the phpmyadmin GUI).
Ref: http://dev.mysql.com/doc/mysql/en/show.html

SQL Statement Syntax: http://dev.mysql.com/doc/mysql/en/sql-syntax.html

Note that as a customer you cannot execute a series of DBA related commands. Remember that a few commands need to be run done using in OPS (eg manage databases, users, user privileges).

If you haven't alreay located it, use the MySql online documentation, the link is in phpmyadmin as Help. Read chapter 3, and 9 thru 14, to get you up to speed.
Other reading: chapters on installation and management as you wish (but you cannot use it at Powweb), have a look at the optimization chapter to get a feels what one should or shouldn't do.
Note that the documentation is sometimes a bit tricky to read, it references all versions up the modern but not yet released 5.x. You need to pick up the feel of how to filter very old and irrelevant stuff and filter things that will only become reality in the near future.
Use the search to locate content quickly.
MySql Online Doc: http://dev.mysql.com/doc/mysql/en/index.html