Home  >  Article  >  Database  >  Understand the main data types of MySQL: Be familiar with what are the commonly used data types

Understand the main data types of MySQL: Be familiar with what are the commonly used data types

WBOY
WBOYOriginal
2024-01-04 13:04:401174browse

Understand the main data types of MySQL: Be familiar with what are the commonly used data types

Overview of MySQL basic data types: To understand what the commonly used data types are, specific code examples are required

MySQL is a commonly used relational database management system that supports Multiple data types. Understanding these data types is critical to proper database design and data storage. This article will introduce the commonly used data types in MySQL and provide specific code examples.

  1. Integer type (INT)

Integer type is one of the most commonly used data types, used to store integers. MySQL provides a variety of integer data types, including the following: TINYINT (-128 to 127), SMALLINT (-32768 to 32767), MEDIUMINT (-8388608 to 8388607), INT (-2147483648 to 2147483647) and BIGINT (- 9223372036854775808 to 9223372036854775807). The following is sample code to create a table named "users" that contains an integer id field:

CREATE TABLE users (
  id INT PRIMARY KEY,
  username VARCHAR(255),
  password VARCHAR(255)
);
  1. Float (FLOAT and DOUBLE)

Floating point type is used to store numbers with decimal points. MySQL provides two floating-point data types: FLOAT and DOUBLE. FLOAT is used to store single-precision floating-point numbers, while DOUBLE is used to store double-precision floating-point numbers. The following is sample code to create a table named "products" that contains a floating-point price field:

CREATE TABLE products (
  id INT PRIMARY KEY,
  name VARCHAR(255),
  price FLOAT(10, 2),
  description TEXT
);
  1. String type (VARCHAR and TEXT)

String type is used to store text data. MySQL provides two commonly used string data types: VARCHAR and TEXT. VARCHAR is used to store variable length strings, while TEXT is used to store larger text data. The following is sample code to create a table named "comments", which contains a name field of type VARCHAR and a content field of type TEXT:

CREATE TABLE comments (
  id INT PRIMARY KEY,
  name VARCHAR(255),
  content TEXT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
  1. Date and time types (DATE, TIME and DATETIME)

Date and time types are used to store date and time data. MySQL provides a variety of date and time data types, including DATE, TIME and DATETIME. DATE is used to store dates, TIME is used to store time, and DATETIME is used to store a combination of date and time. The following is a sample code to create a table named "orders", which contains an order_date field of type DATE and an order_time field of type TIME:

CREATE TABLE orders (
  id INT PRIMARY KEY,
  order_date DATE,
  order_time TIME
);
  1. Enumeration type (ENUM)

Enumeration type is used to store a predefined list of values. The ENUM data type in MySQL allows you to specify a fixed set of values ​​for a column. The following is sample code to create a table named "colors" that contains a color field of type ENUM:

CREATE TABLE colors (
  id INT PRIMARY KEY,
  color ENUM('red', 'green', 'blue')
);

Summary:

This article provides an overview of commonly used data types in MySQL , including integers, floating point types, string types, date and time types, and enumeration types. Understanding these data types is critical to proper database design. With concrete code examples, you can better understand and use these data types in real-world applications. I hope this article can help you deepen your understanding of MySQL data types and provide guidance for database design and development.

The above is the detailed content of Understand the main data types of MySQL: Be familiar with what are the commonly used data types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn