Archive for December, 2006

Working with (Struts hosting) MySQL SQL 56 mysql> begin; Query

Sunday, December 31st, 2006

Working with MySQL SQL 56 mysql> begin; Query OK, 0 rows affected (0.00 sec) Now execute your SQL. Once you have finished, use the command COMMIT or ROLLBACK, depending on your circumstances. Functions/Operators In several places throughout this chapter, we have used a MySQL function in a query. A function is code written by MySQL that aids in the query being used. For example, suppose you want to determine the largest account ID in the acc table. To do this, use the max() function: mysql> SELECT max(acc_id) FROM account; +————-+ | max(acc_id) | +————-+ | 1034546 | +————-+ 1 row in set (0.00 sec) In this query, the maximum value in the acc_id column is returned from the database. MySQL includes a large number of functions too many to list in this chapter. Refer to the MySQL documentation for a listing of all available functions and examples. Joins One of the harder concepts to grasp in the world of databases is the join. Let s begin our discussion by throwing another table into our current database. Right now we have a database called acc that has the following fields: acc_id int username varchar password varchar ts timestamp act_ts timestamp This table doesn t hold much information about the actual owner of the account ID. We need to add another table called acc_add that will hold address information for the account owner. Here s the table definition: mysql> create table acc_add ( add_id int not NULL, acc_id int, name varchar(64), address1 varchar(64),
Hint: If you are looking for good and high quality web space to host and run your java application check Vision java web hosting services

Introducing MySQL SQL 55 Note that the (Michigan web hosting) DATA

Saturday, December 30th, 2006

Introducing MySQL SQL 55 Note that the DATA DIRECTORY option in the ALTER TABLE as well as in the CREATE TABLE command is available only on MyISAM tables underMySQL 4.0. Deleting Tables If you are absolutely sure that you want to get rid of a table permanently, use the command DROP TABLE
. Here is a simple example of using DROP TABLE: mysql> create table test (id int); Query OK, 0 rows affected (0.05 sec) mysql> insert into test values(1); Query OK, 1 row affected (0.02 sec) mysql> drop table test; Query OK, 0 rows affected (0.03 sec) Notice that the table will be dropped without any reservation by the database server. It is vital that you type in the table name accurately because once a table has been dropped, it is no longer available. Transactions One of the most powerful aspects of MySQL is its ability to use transactions. A transaction is an atomic action that must either succeed or fail. This means that in a transaction consisting of three different queries a SELECT, an INSERT, and an UPDATE if any of these operations fail, the other commands must be rolled back to their original state. The current MySQL system includes two different table types that allow for transactions: InnoDB and BDB. In order for a database table to use transactions, the table must be created using a TYPE clause or the table must be altered with an ALTER TABLE command also using the TYPE clause. Once you create a table to handle transactions, you must inform the MySQL system that you want to use transactions. You can accomplish this by using the autocommit database server variable. By default, this variable is set to a value of 1, meaning that the database server will automatically commit the query once it executes. To start a transaction, the autocommit variable must be set to 0. For example: mysql> set autocommit = 0; Query OK, 0 rows affected (0.00 sec) Now you have the ability to execute SQL statements that will be either committed to the database or rolled back. Start with either the BEGIN or BEGIN WORK statement:

Hint: This post is supported by Gama web hosting php services

Working with (Servlet hosting) MySQL SQL 54 column name, increasing

Friday, December 29th, 2006

Working with MySQL SQL 54 column name, increasing the field size of a particular column, or dropping/adding primary keys. First, let s add a new column to our acc table: mysql> ALTER TABLE account ADD access int; Query OK, 8 rows affected (0.11 sec) Records: 8 Duplicates: 0 Warnings: 0 This query adds a new column called access to the account table and uses a column type of int. The ADD clause of ALTER TABLE has a few options. The full definition is ALTER TABLE
ADD [COLUMN] [FIRST|AFTER ] By using FIRST or AFTER, you ensure that the new column is specifically placed within the table definition. The default placement is at the end of the current table definition. What if you wanted to change a column s data type? For example: mysql> ALTER TABLE account CHANGE access access varchar(15); Query OK, 8 rows affected (0.11 sec) Records: 8 Duplicates: 0 Warnings: 0 This query changes the access column to a varchar(15). Notice how the column name had to be used twice. The CHANGE clause doesn t know if you are changing the name of the column, the type, or both, so it requires that you specify the column name. MySQL includes a clause called MODIFY that assumes the name isn t going to change: mysql> ALTER TABLE account MODIFY access varchar(15); Query OK, 8 rows affected (0.11 sec) Records: 8 Duplicates: 0 Warnings: 0 If you want to remove a primary key currently defined on a table, use the following query: mysql> ALTER TABLE account DROP PRIMARY KEY; Query OK, 8 rows affected (0.11 sec) Records: 8 Duplicates: 0 Warnings: 0 A new primary key can be added with the following query: mysql> ALTER TABLE account ADD primary key(acc_id); Query OK, 8 rows affected (0.11 sec) Records: 8 Duplicates: 0 Warnings: 0 Placing Tables on Specific Drives When you are building a large database system, you probably want to disperse the actual tables across disk drives. This is possible using the DATA DIRECTORY clause of the ALTER TABLE command. For example: ALTER TABLE account DATA DIRECTORY=”/usr/local/databases/account”

Hint: If you are looking for very good and affordable webspace to host and run your tomcat hosting application check Sandzak.com tomcat web hosting provider

Hosting gratis - Introducing MySQL SQL 53 Using the basic command

Friday, December 29th, 2006

Introducing MySQL SQL 53 Using the basic command produces the following: mysql> show processlist; +–+—-+———+——–+——-+—-+—–+—————+ |Id|User|Host |db |Command|Time|State|Info | +–+—-+———+——–+——-+—-+—–+—————+ |1 |joeg|localhost|NULL |Sleep |8900| |NULL | |4 |ODBC|localhost|accounts|Query |0 | NULL|showprocesslist| +–+—-+———+——–+——-+—-+—–+—————+ 2 rows in set (0.00 sec) As you can see, the command tells you a user s name, the host the user is connecting from, what database the user is using, and even the command the user is executing. More on Tables Let s examine the natural progression of database creation and manipulation. First, you design the database and tables; next you add them to the server, populate the tables with data, and finally retrieve and manipulate the data. Now, what happens when you have to change a table? In this section, we look at the various commands available in MySQL for changing the definition of a table. Specifically, we consider renaming a table, altering the columns and their definitions, placing tables, and deleting tables. As you ll see, for the first three tasks you use the ALTER TABLE command. Renaming You rename a table by using the ALTER TABLE command. For example: mysql> ALTER TABLE acc RENAME account; Query OK, 0 rows affected (0.03 sec) mysql> show tables; +——————–+ | Tables_in_accounts | +——————–+ | account | +——————–+ 1 row in set (0.00 sec) Here you use the command to rename the acc table to the accounts table. You can use the SHOW TABLES command to verify that the table name was accurately changed. Altering Column Definitions One of the primary uses for the ALTER TABLE command is changing the schema of a table. The change could be adding a new column, changing the

Hint: If you are looking for very good and affordable webspace to host and run your tomcat hosting application check Sandzak.com tomcat web hosting provider

Godaddy web hosting - Working with MySQL SQL 52 SHOW COLUMNS Once

Thursday, December 28th, 2006

Working with MySQL SQL 52 SHOW COLUMNS Once you create a table, you can obtain information about its columns, how they are defined, and primary key information by using SHOW COLUMNS. The full format of the command is SHOW [FULL] COLUMNS FROM

[FROM ] [LIKE ] If you use the basic format, SHOW COLUMNS FROM
, you see the following: mysql> show columns from acc; +———-+—————+——+—–+———+——-+ | Field | Type | NULL | Key | Default | Extra | +———-+—————+——+—–+———+——-+ | acc_id | int(11) | | PRI | 0 | | | username | varchar(64) | YES | | NULL | | | password | varchar(64) | YES | | NULL | | | ts | timestamp(14) | YES | | NULL | | +———-+—————+——+—–+———+——-+ 4 rows in set (0.00 sec) MySQL provides a shortcut to the basic format by using the DESCRIBE
command. The command assumes you have USEd a database. By using the [FULL] option, you display the privileges the current logged-on user has with the table columns as well. SHOW STATUS You can obtain a great deal more information about a table by using the SHOW STATUS command. The format of the command is SHOW TABLE STATUS [FROM ] [LIKE ] For example: mysql> show table status; This command works on all tables from the current database or from a specified database. If you want to limit the tables the command accesses, use the LIKE option. SHOW PROCESSLIST The last command we cover in our introduction section is SHOW PROCESSLIST. This command is useful for determining access to the database server both current access and access in the recent past. The format of the command is SHOW [FULL] PROCESSLIST

Hint: This post is supported by Gama web hosting php services

Secure web hosting - Introducing MySQL SQL 51 Using SHOW MySQL includes

Wednesday, December 27th, 2006

Introducing MySQL SQL 51 Using SHOW MySQL includes a command called SHOW, which allows a developer or administrator to see details about databases, tables, and the database system itself. In this section, we look at the various SHOW commands and explain what information they provide. Note that in some of the commands an optional LIKE can be used to filter the information provided by the command. The % wildcard is used just like as you do in the SELECT command use of LIKE. We cover the most popular SHOW commands here and save some of them for Chapter 13, Database Administration. SHOW DATABASES The SHOW DATABASES command shows all of the databases available on the current database server. You use the LIKE command to limit the output. For example: mysql> show databases; +———-+ | Database | +———-+ | accounts | | ca | | mysql | | test | +———-+ 4 rows in set (0.03 sec) SHOW TABLES The SHOW TABLES command displays all of the tables within a particular database. The full format of the command is: SHOW [OPEN] TABLES [FROM databaseName] [LIKE wildcardString] Notice that there are a number of optional components to the command. The [OPEN] option will show only those databases that are currently being accessed by a client. If SHOW TABLES is executed, it requires that a database currently be active by executing the USE command. You can use the FROM databaseName option to query the tables available in any database on the system. For example: mysql> SHOW TABLES FROM mysql; +—————–+ | Tables_in_mysql | +—————–+ | columns_priv | | db | | func | | host | | tables_priv | | user | +—————–+ 6 rows in set (0.03 sec)
Note: If you are looking for inexpensive but high quality provider to host and run your serlvet application check Astra servlet hosting services

Working with (Easy web hosting) MySQL SQL 50 mysql> SELECT *

Tuesday, December 26th, 2006

Working with MySQL SQL 50 mysql> SELECT * FROM acc WHERE acc_id = ‘1034055′; +——–+——–+———-+—————-+—————+ |acc_id |username| password | ts | act_ts | +——–+——–+———-+—————-+—————+ |1034055 | jdoe | ime | 20021014212553 | 20021014212444| +——–+——–+———-+—————-+—————+ 1 row in set (0.01 sec) Now we can insert the new row: mysql> INSERT INTO acc VALUES(1034055, ‘jdoe’, ‘newpass’, 0, @time); Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 A final SELECT will show both of the rows and how they relate through the act_ts column of the new row and the ts of the old row: mysql> SELECT * FROM acc WHERE acc_id = ‘1034055′; +——-+——–+———-+—————-+—————-+ |acc_id |username| password | ts | act_ts | +——-+——–+———-+—————-+—————-+ |1034055| jdoe | newpass | 00000000000000 | 20021014212553 | |1034055| jdoe | ime | 20021014212553 | 20021014212444 | +——-+——–+———-+—————-+—————-+ 2 rows in set (0.01 sec) We can always know the active row by including ts=0 in our queries. Deletes When data is no longer needed in a database, you can use the DELETE command to remove a row. However, if you want to maintain the history of the rows in the database, you should instead make the row inactive. First, let s show the removal of a row. The query looks like this: DELETE FROM acc WHERE acc_id = ‘1034154′; The query will select the appropriate row based on the WHERE clause. Another use of the DELETE command is: DELETE FROM acc; This query doesn t include a WHERE clause and thus will remove all rows from the specified database table. To maintain the history of the rows in the database, you shouldn t use the DELETE command because the row will be permanently removed. In that case, the best way to delete the row is to make the row inactive by setting the ts of the row to a timestamp other than 0. In most cases, you want to update the current row to a current timestamp value so that the row has a record of when it was made inactive.
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

Jsp hosting - Introducing MySQL SQL 49 To support this type

Monday, December 25th, 2006

Introducing MySQL SQL 49 To support this type of update, we ve changed the table acc a bit. The new table definition looks like this: mysql> describe acc; +———-+—————+——+—–+———+——-+ | Field | Type | NULL | Key | Default | Extra | +———-+—————+——+—–+———+——-+ | acc_id | int(11) | | PRI | 0 | | | username | varchar(64) | | PRI | | | | password | varchar(64) | YES | | NULL | | | ts | timestamp(14) | YES | PRI | NULL | | | act_ts | timestamp(14) | YES | | NULL | | +———-+—————+——+—–+———+——-+ 5 rows in set (0.00 sec) As you can see, we ve added an act_ts column defined as a timestamp; defined the username, acc_id, and ts not to be NULL; and defined the primary key as a combination of acc_id, username, and ts. To show the process of doing the update, consider the row with an acc_id of 1034055. When the initial row was placed in the database, the ts column was set to 0, and the act_ts was set to the actual time the row was inserted. Here s the output of a SELECT showing the row: mysql> SELECT * FROM acc WHERE acc_id = ‘1034055′; +——+——–+———-+—————-+————+ | acc_id |username| password | ts | act_ts | +——+——–+———-+—————-+————+ |1034055 | jdoe | ime | 00000000000000 |20021014212444| +——–+——–+———-+—————-+————–+ 1 row in set (0.00 sec) Next, we need to insert a new row into the database. In order for the database to remain consistent, we need to relate the old row to the new row using a timestamp. The timestamp needs to be the same, so the first step is to obtain the current time and place it in a temporary variable. We accomplish this by using a SET command and local system variable. For example: mysql> set @time=now(); Query OK, 0 rows affected (0.03 sec) The @time variable now holds a timestamp, and it can be used to insert the new row and change the old row. First, the old row is updated and the ts column is set to the current time: mysql> UPDATE acc SET ts=@time WHERE acc_id = 1034055; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 Here s the query to show the new ts value in the old row:
Quick Hint: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

Hosting gratis - Working with MySQL SQL 48 UPDATE [LOW_PRIORITY] [IGNORE]

Sunday, December 24th, 2006

Working with MySQL SQL 48 UPDATE [LOW_PRIORITY] [IGNORE] tbl_name SET col_name1=expr1 [, col_name2=expr2, …] [WHERE where_definition] [LIMIT #] If you have a user who changes his or her password, you can use the UPDATE command to make the change in the database. Consider the following SELECT, UPDATE, SELECT combination: mysql> SELECT * FROM acc WHERE username=’jime’; +———+———-+———-+—————-+ | acc_id | username | password | ts | +———+———-+———-+—————-+ | 1034034 | jime | NULL | 20021014165845 | +———+———-+———-+—————-+ 1 row in set (0.00 sec) mysql> UPDATE acc SET password=’ime’ WHERE username=’jime’; Query OK, 1 rows affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> SELECT * FROM acc WHERE username=’jime’; +———+———-+———-+—————-+ | acc_id | username | password | ts | +———+———-+———-+—————-+ | 1034034 | jime | ime | 20021014204947 | +———+———-+———-+—————-+ 1 row in set (0.00 sec) In this combination of SQL commands, we display the row where the username is jime. The password is shown to be NULL. We use the UPDATE command to change the password to ime. Notice that the UPDATE command instructs a specific table to be updated; then the column that needs to be changed is indicated by SET. If we have to change numerous columns, we can use multiple SETs and separate them by commas. Finally, we can use a condition to limit the rows changed. The last SELECT command shows that the row was updated correctly. The second way to update a database is to never change a row in the database but instead to inactivate one row and insert a new one. In order to do this type of update, you must include two timestamp fields in each row. The first is called an active timestamp, and the second is just the timestamp. The most active row in the database for a particular key has a timestamp of 0. The active timestamp will be the time when the row was inserted. Once the row is inserted, the active timestamp of the current row is copied to the timestamp (ts field) of the inactive row.

Hint: This post is supported by Gama web hosting php services

Introducing MySQL SQL 47 mysql> (Easy web hosting) SELECT * FROM

Saturday, December 23rd, 2006

Introducing MySQL SQL 47 mysql> SELECT * FROM acc INTO OUTFILE ‘test.outfile’; Query OK, 8 rows affected (0.00 sec) This query results in a text file with all of the rows, as shown here: 1034033 jsmith smithy 20021014165845 1034034 jime N 20021014165845 1034067 jthompson james2 20021014165845 1034089 sstanford stanford 20021014165845 1034123 blewis lewis 20021014165845 1034154 ysheets sheets 20021014165845 1034546 jjmyers N 20021014165845 1034055 jdoe doey 20021014165908 Notice that the NULL values are converted to N and line terminations are provided. If you need the data sorted, you can add the appropriate clauses to the query. MySQL also includes the clause INTO DUMPFILE, which basically dumps a single row into a file without any sort of special processing. The DUMPFILE is typically used to output a BLOB to a file. Our sample database doesn t include a BLOB, but the query might look like the following: SELECT pic_blob FROM images INTO DUMPFILE ‘world.jpg’ WHERE pic_name = ‘World’; Counting If you consider the various SELECT queries we ve created in this section, you will note that they all output some number of result rows. What if we want a query that counts the total number of rows in a result? The total count can be returned using the count(*) option. For example: mysql> SELECT count(*) FROM acc; +———-+ | count(*) | +———-+ | 8 | +———-+ 1 row in set (0.00 sec) Here we execute a SELECT to return the total number of rows in the acc table. Notice that the count value is returned as a column in the result. The column heading value can be changed, as we explained earlier in this section. Updates The first major SQL statement we covered was INSERT, which you use to place data into your database. This was followed by the SELECT statement, which you use to pull the data from your database. What do you do if you want to change the data within a row? You have two options. The first is to just make the change. You can do this with the UPDATE command:

Hint: If you are looking for very good and affordable webspace to host and run your tomcat hosting application check Sandzak.com tomcat web hosting provider