String data type refers to storing text-based data such as "ram", "hello", etc.
When you create a table in MySQL, you define the string data type (like CHAR, VARCHAR, TEXT, etc.) at that time so that string-type data can be inserted into that column.
Syntax:
CREATE TABLE table_name (
column_name VARCHAR(size),
column_name2 CHAR(size),
column_name3 TEXT
);
Example:
Suppose you want to create a product table to store details about products, including the product name, its category, and a description.
To store this information, you can use string data types such as VARCHAR, CHAR, or TEXT.
Query:
CREATE TABLE pro (
product_name VARCHAR(50), -- stores text like 'Laptop', 'Mobile'
category CHAR(20), -- stores short fixed-length text like 'Electronics'
description TEXT -- stores long text like 'A high-performance laptop with 16GB RAM'
);
Insert Values into the Table:
INSERT INTO pro (product_name, category, description)
VALUES ('Laptop', 'Electronics', 'A high-performance laptop with 16GB RAM');
Output:
product_name | category | description
-----------------------------------------------
Laptop | Electronics | A high-performance laptop with 16GB RAM
Explanation:
product_nameusesVARCHARbecause names can vary in length.- The
categoryfield usesCHARbecause its values are short and have a fixed length. descriptionusesTEXTfor long descriptions.
