MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
MySQL provides several data types for storing strings:
CHAR: The CHAR type is used to store fixed-length character strings. The length can be any value from 0 to 255. You must specify the length of the CHAR column upon creation. Right-padding of spaces is applied for values shorter than the specified length.
Example:
CREATE TABLE example ( char_column CHAR(10) );
To insert data:
INSERT INTO example (char_column) VALUES ('abc');
This will actually store 'abc ', as CHAR(10) reserves 10 characters for every entry.
VARCHAR: The VARCHAR type is used to store variable-length character strings. The length can be any value from 0 to 65535. You must specify the maximum length of the VARCHAR column upon creation.
Example:
CREATE TABLE example ( varchar_column VARCHAR(10) );
To insert data:
INSERT INTO example (varchar_column) VALUES ('abc');
Unlike CHAR, VARCHAR does not pad values with spaces. The above example will store 'abc' as is.
TEXT: The TEXT type is used to store long strings of character data. The maximum length is 65535 characters.
Example:
CREATE TABLE example ( text_column TEXT );
To insert data:
INSERT INTO example (text_column) VALUES ('This is a long string of text.');
ENUM: The ENUM type is used to store one of a predefined set of strings. It's very efficient and compact.
Example:
CREATE TABLE example ( enum_column ENUM('Small', 'Medium', 'Large') );
To insert data:
INSERT INTO example (enum_column) VALUES ('Medium');
SET: Similar to ENUM, the SET data type allows you to input one or more values from a predefined list, up to 64 total.
Example:
CREATE TABLE example ( set_column SET('Red', 'Green', 'Blue') );
To insert data:
INSERT INTO example (set_column) VALUES ('Red,Green');
Remember to choose the most appropriate string type based on the nature of data you need to store. VARCHAR is generally good for most purposes, but CHAR can be more efficient for very short strings, TEXT for very long ones, and ENUM and SET for cases where you have a known set of values.
MySQL String Data Types Comparison:
-- Example using VARCHAR CREATE TABLE example_table ( column_varchar VARCHAR(255), column_char CHAR(255), column_text TEXT );
When to Use MySQL TEXT Data Type:
-- Example using TEXT CREATE TABLE text_table ( document TEXT );
Working with MySQL ENUM and SET Data Types:
-- Example using ENUM and SET CREATE TABLE enum_set_table ( status ENUM('active', 'inactive', 'pending'), tags SET('red', 'green', 'blue') );
Character Set and Collation in MySQL String Types:
-- Example with character set and collation CREATE TABLE charset_collation_table ( data VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci );
Working with MySQL ENUM in Queries:
-- Example query with ENUM SELECT * FROM enum_set_table WHERE status = 'active';