MySQL supports a wide variety of data types, which can be used to store different types of data in your database. The data type of a column determines what type of data can be stored in the column and how the data is stored.
Here are some of the most common MySQL data types:
- Integer: Used to store whole numbers, such as 1, 100, and -1000.
- Decimal: Used to store numbers with decimal places, such as 3.14159 and -1234.56.
- Float: Used to store approximate numbers, such as 3.14159 and -1234.56.
- String: Used to store text, such as "Hello, world!" and "This is a string.".
- Date: Used to store date values, such as "2023-10-13".
- Time: Used to store time values, such as "12:00:00".
- Timestamp: Used to store date and time values, such as "2023-10-13 12:00:00".
- Binary: Used to store binary data, such as images and videos.
You can also specify the size of a column when you create it. This is useful for limiting the amount of data that can be stored in the column and for improving the performance of your database.
For example, the following statement creates a column named name
that can store strings up to 255 characters long:
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);
You can also specify a default value for a column. This is the value that will be inserted into the column if no value is specified when a new row is inserted into the table.
For example, the following statement creates a column named created_at
that stores the current date and time whenever a new row is inserted into the table:
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
MySQL data types are a powerful tool for storing and managing data in your database. By choosing the right data types for your columns, you can improve the performance and integrity of your database.
No comments:
Post a Comment