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 © 2025 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

WIA: Alexis Fawx Levels Up as Multifaceted Entrepreneur

As more performers look to diversify, expanding their range of revenue streams and promotional vehicles, some are spreading their entrepreneurial wings to create new businesses — including Alexis Fawx.

Women In Adult ·
opinion

Navigating Age-Related Regulations in Europe

Age verification measures are rapidly gaining momentum across Europe, with regulators stepping up efforts to protect children online. Recently, the U.K.’s communications regulator, Ofcom, updated its timeline for implementing the Online Safety Act, while France’s ARCOM has released technical guidance detailing age verification standards.

Gavin Worrall ·
opinion

Why Cyber Insurance Is Crucial for Adult Businesses

From streaming services and interactive platforms to ecommerce and virtual reality experiences, the adult industry has long stood at the forefront of online innovation. However, the same technology-forward approach that has enabled adult businesses to deliver unique and personalized content to consumers worldwide also exposes them to myriad risks.

Corey D. Silverstein ·
opinion

Best Practices for Payment Gateway Security

Securing digital payment transactions is critical for all businesses, but especially those in high-risk industries. Payment gateways are a core component of the digital payment ecosystem, and therefore must follow best practices to keep customer data safe.

Jonathan Corona ·
opinion

Ready for New Visa Acquirer Changes?

Next spring, Visa will roll out the U.S. version of its new Visa Acquirer Monitoring Program (VAMP), which goes into effect April 1, 2025. This follows Visa Europe, which rolled out VAMP back in June. VAMP charts a new path for acquirers to manage fraud and chargeback ratios.

Cathy Beardsley ·
opinion

How to Halt Hackers as Fraud Attacks Rise

For hackers, it’s often a game of trial and error. Bad actors will perform enumeration and account testing, repeating the same test on a system to look for vulnerabilities — and if you are not equipped with the proper tools, your merchant account could be the next target.

Cathy Beardsley ·
profile

VerifyMy Seeks to Provide Frictionless Online Safety, Compliance Solutions

Before founding VerifyMy, Ryan Shaw was simply looking for an age verification solution for his previous business. The ones he found, however, were too expensive, too difficult to integrate with, or failed to take into account the needs of either the businesses implementing them or the end users who would be required to interact with them.

Alejandro Freixes ·
opinion

How Adult Website Operators Can Cash in on the 'Interchange' Class Action

The Payment Card Interchange Fee Settlement resulted from a landmark antitrust lawsuit involving Visa, Mastercard and several major banks. The case centered around the interchange fees charged to merchants for processing credit and debit card transactions. These fees are set by card networks and are paid by merchants to the banks that issue the cards.

Jonathan Corona ·
opinion

It's Time to Rock the Vote and Make Your Voice Heard

When I worked to defeat California’s Proposition 60 in 2016, our opposition campaign was outspent nearly 10 to 1. Nevertheless, our community came together and garnered enough support and awareness to defeat that harmful, misguided piece of proposed legislation — by more than a million votes.

Siouxsie Q ·
opinion

Staying Compliant to Avoid the Takedown Shakedown

Dealing with complaints is an everyday part of doing business — and a crucial one, since not dealing with them properly can haunt your business in multiple ways. Card brand regulations require every merchant doing business online to have in place a complaint process for reporting content that may be illegal or that violates the card brand rules.

Cathy Beardsley ·
Show More