educational

An Introduction to MySQL: Part 2

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 this installment, he'll cover the concepts and techniques needed to setup up the database for manipulation.

A database is really nothing more than a hierarchy of increasingly complex data structures, and in MySQL, the acknowledged structure for holding blocks (or records) of information is called the table. These records, in turn, are made up of the smallest object that can be manipulated by the user, objects known as the datatype. Together one or more of these datatypes will form a record. A table holds the collection of records that make up part of the database. We can consider the hierarchy of a database to be that of the following: Database < Table < Record < Datatype

Datatypes come in several forms and sizes, allowing the programmer to create tables suited for the scope of the project. The decisions made in choosing proper datatypes greatly influence the database's performance, so it is wise to have a detailed understanding of these concepts.

MySQL Datatypes
MySQL is capable of many of the datatypes that even the novice programmer has probably been exposed to. Some of the more commonly used include:

• CHAR (M) CHAR's are used to represent fixed length strings. A CHAR string can range from 1-255 characters. In later table creation, an example CHAR datatype would be declared as follows, for example: car_model CHAR(10);

• VARCHAR (M) VARCHAR is a more flexible form of the CHAR data type. It also represents data of type String, yet stores this data in variable length format. Again, VARCHAR can hold 1-255 characters. VARCHAR is usually a wiser choice than CHAR, due to it's variable length format characteristic. Although, keep in mind that CHAR is much faster than VARCHAR, sometimes up to 50%. (A CHAR stores the whole length of the declared variable, regardless of the size of the data contained within, whereas a VARCHAR only stores the length of the data, thus reducing size of the database file.) For example: car_model VARCHAR(10);

• INT (M) [Unsigned] The INT datatype stores integers ranging from -2147483648 to 2147483647. Optionally, "unsigned" can be denoted with the declaration, modifying the range to be 0 to 4294967295, ex.: light_years INT; (Valid integer: '-24567'. Invalid integer: '3000000000').

- or - light_years INT unsigned; (Valid integer: '3000000000'. Invalid integer: '-24567').

• FLOAT [(M,D)] A FLOAT represents small decimal numbers, used when a somewhat more precise representation of a number is required, for example: rainfall FLOAT (4,2); Note: Due to the fact that FLOAT is rounded, those wishing to represent money values would find it wise to use DECIMAL, a datatype found within MySQL that does not round values. Consult the MySQL server's documentation for a complete explanation.

• DATE Stores date related information. The default format is 'YYYY-MM-DD', and ranges from '0000-00-00' to '9999-12-31'. MySQL provides a powerful set of date formatting and manipulation commands, too numerous to be covered within this article. However, one can find these functions covered in detail within the MySQL documentation. Thusly: the_date DATE;

• TEXT / BLOB The text and blob datatypes are used when a string of 255 - 65535 characters is required to be stored. This is useful when one would need to store an article such as the one you are reading. However, there is no end space truncation as with VARCHAR AND CHAR. The only difference between BLOB and TEXT is that TEXT is compared case insensitively, while BLOB is compared case sensitively.

• SET A datatype of type string that allows one to choose from a designated set of values, be it one value or several values. One can designate up to 64 values, for example: transport SET ("truck", "wagon") NOT NULL;

From the above declaration, the following values can be held by transport: "", "truck", "wagon", "truck,wagon"

• ENUM A datatype of type string that has the same characteristics as the SET datatype, but only one set of allowed values may be chosen. Usually only takes up one byte of space, thus saving time and space within a table: transport ENUM ("truck", "wagon") NOT NULL;

From the above declaration, the following values can be held by transport: "", "truck", "wagon"

• Records Together, a group of declared datatypes form what is known as a record. A record can be as small as one data variable, or as many as deemed needed. One or more records form the structure of a table.

The Bigger Picture: Tables
Before we can execute commands on the database, we must first create a table in which data can be stored. This is accomplished in the following manner:

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

Ensuing output:

Query OK, 0 rows affected (0.10 sec)

mysql>

The first table in your database has now been created. Remember that no two tables can have the same name, and that each dataspace is more often referred to as a column. Before we can execute commands on the database, we must first create a table in which data can be stored.

Column Characteristics
A column's name may not be made up of strictly numbers, but may start with a number, and include up to 64 characters. The following options can be placed after any datatype, adding other characteristics and capabilities to them:

• Primary Key: Used to differentiate one record from another. No two records can have the same primary key. This is obviously useful when it is imperative that no two records are mistaken to be the other.

• Auto_Increment: A column with this function is automatically incremented one value (previous + 1) when an insertion is made into the record. The datatype is automatically incremented when 'NULL' is inserted into the column.

• NOT NULL: Signifies that the active column can never be assigned a NULL value, for example:

soc_sec_number INT PRIMARY KEY;

Because no two soc_sec_number records can hold the same value, for example, ID_NUMBER INT AUTO_INCREMENT; can be used to automatically increments in value, starting at '1', with every subsequent insertion.

Table Relevant Commands
We can execute a number of useful commands pertaining to the tables, such as the following:

• Show Tables mysql> show tables;

Result: This will list all tables currently existing within the database.

• Show Columns mysql> show columns from test;

Result: This will return the columns and column information pertaining to the designated table.

Take a minute to execute each one of the above commands after you have created the test table. They will prove very helpful as your database increases in size and complexity.

You should now have a basic understanding of the creation of tables, one of the most important concepts of using the MySQL server. You now know that tables are constructed using datatypes, which when grouped together form a record. In the next section, we will begin learning how to actually manipulate the database.

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

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 ·
opinion

The Taxman Cometh for Every Business

February may be the month of romance, but it is also a time when we need to think about something that inspires very little love: taxes. April is not far away, and the taxman is always waiting. This year, federal and most state income taxes are due Monday, April 15.

Cathy Beardsley ·
Show More