DevShed - Beginning MySQL Tutorial
★ wanayoo — archive 1999 http://www.devshed.com/Server_Side/MySQL/Intro/page5.htmlNouvelle recherche | Portail wanayoo

Developer Shed
Wednesday, 10.13.99  

HOME > SERVER SIDE > MYSQL > ARTICLE    SOURCE

Power Search
Free Newsletter:


Client Side
DHTML
Graphics
JavaScript

Server Side
Administration
ASP
JServ
MySQL
PHP
Python
Roxen
Zend
Zope
Web Hosting

Tools
HTML Editors
Graphic Editors
FTP Programs

Dev Talk
Forums
Mailing List
Brain Dump
Feedback

ClipScripts
Add script
Edit script


Advertise
on DevShed
JUMP TO:
> The Database
> Part 1: At First Glance
> Part 2: Datatypes and Tables
> Part 3: Manipulating the Database
> Part 4: Advanced MySQL Commands
Beginning MySQL Tutorial
By W.J. Gilmore
President, 3.14Interactive

April 03, 1999

Part 4: Advanced MySQL Commands
What we have covered so far is but a small part of what MySQL is capable of. Let's delve a little deeper into the language, exploring some of the more advanced commands of the language.

Logical Operations

MySQL includes full support of all basic logical operations.

AND (&&)


mysql> SELECT * FROM test WHERE mysql> (name = "Bugs Bunny") AND mysql> (phone_number = 5554321);

Result:
All records containing the name "Bugs Bunny" AND the phone number '5554321' will be displayed to the screen.

OR ( || )


mysql> SELECT * FROM test WHERE mysql> (name = "Bugs Bunny") OR mysql> (phone_number = 5554321);

Result:
All records containing the name "Bugs Bunny" OR the phone number '5554321' will be displayed to the screen.

NOT ( ! )


mysql> SELECT * FROM test WHERE mysql> (name != "Bugs Bunny");

Result:
All records NOT containing the name "Bugs Bunny" will be displayed to the screen.

Order By


mysql> SELECT * FROM test WHERE mysql> (name = "Bugs Bunny") ORDER BY mysql> phone_number;

Result:
All records containing the name "Bugs Bunny" will be displayed to the screen, ordered in respect to the phone_number.

Search functions

MySQL offers the user the ability to perform both general and specific searches on data.


mysql> SELECT * FROM test WHERE mysql> (name LIKE "%gs Bunny");

Result:
All records containing the partial string "gs Bunny" will be displayed to the screen. This would include such names as: "Bugs Bunny", "ags Bunny", "gs Bunny", and "234rtgs Bunny".

Notice that "LIKE" has been used instead of the equals sign (=). "LIKE" signifies that one is searching for an estimate of the data requested, and not necessarily an exact copy.

The '%' sign could be placed anywhere within the string. The method in which the server searches for a string is dependent upon where one places the '%' sign.


mysql> SELECT * FROM test WHERE mysql> (name LIKE "Bugs Bunny%");

Result:
All records containing the partial string "Bugs Bunny" will be displayed to the screen. This would include such names as: "Bugs Bunnys", "Bugs Bunnyyyy453", "Bugs Bunnytrtrtrtrtr", but not "gs Bunny".

Focused Search Results

One can also perform searches and display only certain columns.


mysql> SELECT name FROM test WHERE mysql> (name = "Bugs Bunny");

Result:
name
Bugs Bunny

Alter table

Another very important function of MySQL is the ability to modify previously created tables. This is accomplished via the ALTER statement. This function allows one to add, modify, and delete columns, as well as rename the table, among other functions.

Example: Rename the table


mysql> ALTER table test RENAME mytest;

Example: Add a column


mysql> ALTER table mytest ADD birthday DATE;

Example: Modify a column


mysql> ALTER table mytest CHANGE mysql> name newname VARCHAR (25);

Example: Delete a column


mysql> ALTER table mytest DROP newname;

Executing the above four functions would modify test, creating the following table:


mysql> TABLE mytest ( > email VARCHAR (25), > phone_number INT, > ID INT AUTO_INCREMENT, > birthday DATE );

The topics covered within this article are but a short introduction of the capabilities of MySQL. However, these functions form the basis of almost all advanced commands to be found in the language. Above all, the most important lesson that one can remember is to practice, study the documentation, and become an active member of the mailing list archives (And obviously read DevShed frequently!). Only by taking an enthusiastic, even "aggressive" approach to the language can one successfully master it.

Click here to discuss this article
with other readers
. 158 comments.


Click here to receive updates on the latest articles.


W.J. Gilmore is President of 3.14Interactive (http://www.314interactive.com), a web-applications development and consulting firm.



Now Available:

Click to Order!

Search for books on MySQL

Printer friendly versionPrinter friendly version

Tech News:
• Ameritrade To Offer Wireless Trading
• VA Linux Announces Deal to Resell Debian Linux Distribution
• E Ink and Lucent Team Up on Printable Organic Transistors
• Troubles in 2000 Not Limited to Y2K
•  More news..

Hot Forum Topics
forms--redirecting to different pages
Load Data Infile Problem
IP recognition
Help --- error
MySQL passwords in PHP scripts



Copyright © 1997-99 ngenuity. All rights reserved. Privacy Policy.