Basic operations of databases and tables in MySQL

1. The operation of the database:

1. Display database:

 show databases;

2. Create a database:

create database [if not exists] database name ;
([] is optional)

Give a chestnut:
① (Create a database of students)

create database students;

② (If there is no students database in the system, create it; if there is, don't create it)

create database if not exists students;

③(If you want to have Chinese in the database, you have to add charecter set utf8)

create database  if not exists students character set utf8;

3. Use the database:

use database name ;

use students;

4. Delete the database:

drop database [if exists] database name ;
([] is optional)

drop database students;

Two, the operation of the table:

Note :
We operate a database table , you need to use the database (use the name of the database you want to use)

1. View the table structure:

desc table name;

desc users;

Give a chestnut:
Insert picture description here

2. Create a table:

format:

CREATE TABLE table_name (
    field1 datatype,
    field2 datatype,
    field3 datatype
);

For example: (comment is used to annotate attributes)

create table test (
    id int,
    name varchar(20) comment '姓名',
    birthday timestamp,
    amout decimal(13,2),
    resume text
);

3. Delete the table:

drop table [if exists] table_name;

-- 删除 test 表
drop table test;
-- 如果存在 test 表,则删除 test 表
drop table if exists test;

4. View the tables in the database:

show tables;

3. Commonly used data types:

int: integer
decimal(M,D): floating-point number type
varchar(size): string type
timestamp: date type

Guess you like

Origin blog.csdn.net/qq_45658339/article/details/109705922