educational

An Introduction to MySQL: Part 3

In Part 1 of his MySQL series, Cracker provided us with a basic understanding of how to connect to the server, select the database, and perform some basic commands. In Part 2, he covered the concepts and techniques needed to setup up the database for manipulation. In today's final installment, he will discuss how to actually manipulate the database.

Manipulating the Database
A MySQL database can be manipulated in four possible ways: addition, deletion, modification, and search. These topics will all be briefly covered in the following two sections. However, before we begin, I would like to highlight the fact that SQL, like many computer languages, is particular about command syntax. The slightest error in placement of a parentheses, comma, or semicolon will almost surely end in error. As a result, take care to be attentive of command syntax.

• Insertion of records
Note: The originally created table, test, created in the last section will be used to illustrate the examples in this section. Here it is again, for quick reference:

mysql> CREATE TABLE test (
> name VARCHAR (15),
> email VARCHAR (25),
> phone_number INT,
> ID INT NOT NULL AUTO_INCREMENT,
> PRIMARY KEY (ID));

Insertion of data into the table is accomplished, logically enough, using the INSERT command.

mysql> INSERT INTO test VALUES
mysql> ('Bugs Bunny', 'carrots@crackersoft.ru',
mysql> 5554321, NULL);

Result, assuming the command was correctly entered:

Query OK, 1 row affected (0.02 sec)
mysql>

So what really happened here? Single quotations were placed around the datatypes VARCHAR. All datatypes of type STRING (i.e. char, varchar, text, blob, etc.) must be surrounded in single quotes, or an error will occur. There were no single quotes surrounding the phone number. Datatypes of type INT do not require single quotes.

NULL? A NULL allows any datatype with the characteristic AUTO_INCREMENT to be automatically assigned a value. If it is the first record inserted into the database, it is assigned the value '1'. Otherwise, it is assigned the previously inserted value + 1 (i.e. if the previously inserted value was '2', then the next would be '3'). In addition, the insertion of NULL into a variable of type TIMESTAMP causes that variable to be given the value of the current date.

Note: It is of importance to remember that the same number of values must be inserted as datatypes are contained within a record. In the above example, if one attempted to insert only three values instead of four, the insertion would fail. The same result applies if one attempted to insert five values. Example:

mysql> insert into test values('doggy');
ERROR 1058: Column count doesn't match value count
mysql>

Note: One of the advantageous aspects of MySQL is it's ability to convert without any trouble between datatypes. MySQL automatically converts between integers, strings, and dates.

• Selection
A database would not be much use if one was not able to search and extract data from it. In MySQL terms, this is accomplished through the SELECT statement.

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

Result:

name email phone ID
Bugs Bunny carrots@crackersoft.ru 5554321 1

Let's assume we have inserted four differing records, all bearing the same name "Bugs Bunny", yet having different email addresses and phone numbers. The table test, would look somewhat like the following:

name email phone ID
Bugs Bunny carrots@crackersoft.ru 5554321 1
Bugs Bunny peppers@crackersoft.ru 5554331 2
Bugs Bunny lettuce@crackersoft.ru 5554341 3
Bugs Bunny celery@crackersoft.ru 5554351 4

• Deletion
One can also delete records previously inserted into the table.
This is accomplished through the use of the DELETE command.

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

Result: This would result in the deletion of all records within the table test containing the name "Bugs Bunny". Another example:

mysql> DELETE FROM test
mysql> WHERE (phone_number = 5554321);

Result: (Using the previously illustrated example)

name email phone ID
Bugs Bunny peppers@crackersoft.ru 5554331 2
Bugs Bunny lettuce@crackersoft.ru 5554341 3
Bugs Bunny celery@crackersoft.ru 5554351 4

• Modification
MySQL has the capability of modifying data already entered into the table. This is accomplished through the UPDATE command.

mysql> UPDATE test SET name = 'Daffy Duck'
mysql> WHERE name = "Bugs Bunny";

Result: (Using the previously illustrated example)

name email phone ID
Daffy Duck peppers@crackersoft.ru 5554331 2
Daffy Duck lettuce@crackersoft.ru 5554341 3
Daffy Duck celery@crackersoft.ru 5554351 4

In this section, we covered the core MySQL database manipulation functions, basic insertion, deletion, modification, and search. The next section will elaborate on these capabilities, providing extended functioning and flexibility when manipulating the database.

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. The slightest error in placement of a parentheses, comma, or semicolon will almost surely end in error.

• 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 all 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 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 );

Simple, yes? Don't worry; all it takes is practice!

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 learn as much as possible on your own, or through formal training. Only by taking an enthusiastic, and even an "aggressive" approach to the language can one master it.

Copyright © 2024 Adnet Media. All Rights Reserved. XBIZ is a trademark of Adnet Media.
Reproduction in whole or in part in any form or medium without express written permission is prohibited.

More Articles

opinion

Strategic Upscaling of Non-4K Content

If content is king in adult, then technical quality is the throne upon which it sits. Technical quality drives customer acquisition and new sales, while cementing retention and long-term loyalty.

Brad Mitchell ·
profile

'Traffic Captain' Andy Wullmer Braves the High Seas as Spirited Exec

Wullmer networked and hobnobbed, gaining expertise in everything from ecommerce to SEO and traffic, making connections and over time rising through the ranks of several companies to become CEO of the mobile business arm of TrafficPartner.

Alejandro Freixes ·
opinion

To Cloud or Not to Cloud, That Is the Question

Let’s be honest. It just sounds way cooler to say your business is “in the cloud,” right? Buzzwords make everything sound chic and relevant. In fact, someone uninformed might even assume that any hosting that is not in the cloud is inferior. So what’s the truth?

Brad Mitchell ·
opinion

Upcoming Visa Price Changes to Registration, Transaction Fees

Visa is updating its fee structure. Effective April 1, both the card brand’s initial nonrefundable application fee and annual renewal fee will increase from $500 to $950. Visa is also introducing a fee of 10 cents for each settled transaction, and 10 basis points — 0.1% — on the payment volume of certain merchant accounts.

Jonathan Corona ·
opinion

Unpacking the New Digital Services Act

Do you hear the word “regulation” and get nervous? When it comes to the EU’s Digital Services Act (DSA), you shouldn’t worry. If you’re complying with the most up-to-date card brand regulations, you can breathe a sigh of relief.

Cathy Beardsley ·
opinion

The Perils of Relying on ChatGPT for Legal Advice

It surprised me how many people admitted that they had used ChatGPT or similar services either to draft legal documents or to provide legal advice. “Surprised” is probably an understatement of my reaction to learning about this, as “horrified” more accurately describes my emotional response.

Corey D. Silverstein ·
profile

WIA Profile: Holly Randall

If you’re one of the many regular listeners to Holly Randall’s celebrated podcast, you are already familiar with her charming intro spiel: “Hi, I’m Holly Randall and welcome to my podcast, ‘Holly Randall Unfiltered.’ This is the show about sex, the adult industry and the people in it.

Women In Adult ·
trends

What's Hot Now: Leading Content Players on Trending Genres, Monetization Strategies

The juggernaut creator economy hurtles along, fueled by ever-ascendant demand for personality-based authenticity and intimacy — yet any reports of the demise of the traditional paysite are greatly exaggerated.

Alejandro Freixes ·
opinion

An Ethical Approach to Global Tech Staffing

One thing my 24-year career as a technologist working to support the online adult entertainment industry has taught me about is the power of global staffing. Without a doubt, I have achieved significantly more business success as a direct result of hiring abroad.

Brad Mitchell ·
opinion

Finding the Right Payment Partner

Whenever I am talking with businesses that are just getting started, one particular question comes up a lot: “How do I get a merchant account?” It’s a simple question, but it has a complicated answer.

Jonathan Corona ·
Show More