Seele

Seele

MySQL Basics

Create MySQL Database#

  1. Login and create database
  # mysql -u root -p
  mysql> create database test;
  1. Login with created database
  # mysqladmin -u root -p create test

Data Types#

  • Numeric: Integer, Float, Boolean, String
  • Date/Time: Date, Time, DateTime
  • String: String, Binary, Character Set

Numeric Types#

TypeSizeRange (Signed)Range (Unsigned)Usage
tinyint1-128~1270~255Integer
smallint2-32768~327670~65535Integer
mediumint3-8388608~83886070~16777215Integer
int4-2147483648~21474836470~4294967295Integer
bigint8-9223372036854775808~92233720368547758070~18446744073709551615Integer
float4-3.402823466E+38~3.402823466E+38-1.79E+308~1.79E+308Float
double8-1.7976931348623157E+308~1.7976931348623157E+308-2.22E-308~2.22E-308Float
decimalThe larger of M+2 and D+2 in DECIMAL(M,D)Depends on the values of M and DDepends on the values of M and DDecimal Value

Date and Time Types#

TypeSizeRangeFormatUsage
date41000-01-01~9999-12-31yyyy-mm-ddDate
time8-838:59:59~838:59:59hh:mmTime
datetime81000-01-01 00:00:00~9999-12-31 23:59:59yyyy-mm-dd hh:mmDateTime
timestamp81970-01-01 00:00:00~2038-01-19 03:14:07yyyy-mm-dd hh:mmTimestamp

String Types#

TypeSizeUsage
char0~255Fixed-length string
varchar0~65535Variable-length string
tinytext0~255Short text string
text0~65535Long text string
mediumtext0~16777215Medium-length text string
langtext0~65535Very long text string
tinyblob0-255Binary string with a maximum length of 255 characters
blob0~65535Binary long text string
mediumblob0~16777215Medium-length binary string
longblob0~4294967295Long-length binary string

Tips:

  1. What is the difference between varchar and char
    Screenshot_20220524_194438

  2. Difference between binary and varbinary

    Similar to char and varchar, the difference is that they contain binary strings instead of non-binary strings. Binary strings are strings composed of 0s and 1s, and can be used to store binary files such as images, videos, and audios. They do not have character sets, and sorting and comparison are based on byte values.

  3. Difference between Blob and Text

    Blob is a binary string, while Text is a string.

Create Table#

Example:

```sql
# create table
CREATE TABLE `test` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(255) NOT NULL,
    `age` int(11) NOT NULL,
    `created_at` datetime NOT NULL,
    `updated_at` datetime NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
  1. Not null: Must provide input value

  2. Auto increment: Auto-incrementing

  3. Default: Default value

    If you want to automatically write the time, you should use the TIMESTAMP type.

    Set DEFAULT to CURRENT_TIMESTAMP. It is best to specify it as NULL.

  4. Primary key: Primary key, can define multiple columns as primary key

  5. Engine: Storage engine, default is InnoDB

  6. Charset: Character set, default is utf8mb4

Example:

root@seele # mysql -u root -p

Enter password:******

mysql> Use test;

Database changed

mysql> CREATE TABLE `test` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(255) NOT NULL,
    `age` int(11) NOT NULL,
    `created_at` datetime NOT NULL,
    `updated_at` datetime NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Query OK, 0 rows affected (0.00 sec)

Delete Table & Database#

  1. Delete table

     DROP TABLE `test`;
    
  2. Delete database

    DROP DATABASE `test`;
    
    mysqladmin -u root -p drop test
    
  3. Delete data and reset auto-increment

    ALTER TABLE `test` AUTO_INCREMENT = 1;
    // Set the auto-increment to 1, which can solve the auto-increment issue in some cases
    
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.