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

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

WIA Profile: Patricia Ucros

Born in Bogota, Colombia, Ucros graduated from college with a degree in education. She spent three years teaching third grade, which she enjoyed a lot, before heeding her father’s advice and moving to South Florida.

Women In Adult ·
opinion

Creating Payment Redundancies to Maximize Payout Uptime

During the global CrowdStrike outage that took place toward the end of July, a flawed software update brought air travel and electronic commerce to a grinding halt worldwide. This dramatically underscores the importance of having a backup plan in place for critical infrastructure.

Jonathan Corona ·
opinion

The Need for Minimal Friction in Age Verification Technology

In the adult sector, robust age assurance, comprised of age verification and age estimation methods, is critical to ensuring legal compliance with ever-evolving regulations, safeguarding minors from inappropriate content and protecting the privacy of adults wishing to view adult content.

Gavin Worrall ·
opinion

Account-to-Account Payments: The New Banking Disruptor?

So much of our industry relies upon Visa and Mastercard to support consumer payments — and with that reliance comes increased scrutiny by both brands. From a compliance perspective, the bar keeps getting raised until it feels like we end up spending half our time making sure we are compliant rather than growing our business.

Cathy Beardsley ·
profile

WIA Profile: Samantha Beatrice

Beatrice credits the sex positivity of Montreal for ultimately inspiring her to pursue work in adult entertainment. She had many friends working in the industry, from sex workers to production teams, so it felt like a natural fit and offered an opportunity to apply her marketing and social media savvy to support people she truly believes in and wants to see succeed.

Women In Adult ·
opinion

Understanding the Latest Server Processors

Over the last decade, we mostly stopped talking about CPU performance. Recently, however, there has been a seismic and exciting change in the CPU landscape, due to innovation by a chip company called Advanced Micro Devices (AMD).

Brad Mitchell ·
Show More