I. Database

1, create database:

CREATE DATABASE db_name;
Copy the code

Mysql > select * from database where id = 1;

SHOW DATABASES;
Copy the code

3. Use database:

USE db_name;
Copy the code

Second, the table

1. Create table

CREATEA TABLE tb_name ( col1 dataType(dataLength)... ) ;Copy the code

Create a table for a person:

CREATE TABLE person(
    id INT(10),
    name VARCHAR(30), 
    age INT(4),
    sex CHAR(1),
):
Copy the code

Mysql > select * from table_name where table_name = ‘table_name’;

SHOW TABLES;
Copy the code

Iii. Data Type:

The difference between CHAR and VARCHAR is that: CHAR is fixed length, VARCHAR is variable length, for example, the string “ABC”, CHAR(10) means that the stored character occupies 10 bytes (including 7 null characters); Using VARCHAR(10) to store “ABC” takes 3 bytes. 10 is the maximum available length. When you store less than 10 characters, the actual length will be installed.

The difference between an ENUM and a SET is that the value of an ENUM must be one of the enumerated values at the time of definition. The value of a SET can be multiple values.

Insert data

Create a table for employees:

CREATE TABLE employee(
    id INT(10),
    name VARCHAR(30),
    phone CHAR(11)
);
Copy the code

2. Insert data:

INSERT INTO tb_name [(col1, col2...)]  VALUES (value1, value2...) ;Copy the code

Insert three values:

INSERT INTO employee(id,name,phone) VALUES(01,'Tom',110110110);

INSERT INTO employee VALUES(02,'Jack',119119119);

INSERT INTO employee(id,name) VALUES(03,'Rose');
Copy the code

As you have noticed, some data needs to be enclosed in single quotes, such as the names of Tom, Jack, and Rose, because their data type is CHAR. In addition a VARCHAR, TEXT, DATE, TIME, types of data such as ENUM also need single quotation marks, and INT, FLOAT, DOUBLE, etc. You don’t need to.

The first statement has more than the second statement :(id,name,phone). The parentheses are the columns in the table for each value to be added (01, ‘Tom’,110110110). The third statement adds only two columns (ID,name), so Rose’s phone in the table is NULL.