Getting started with mysql is enough! ! (Continually updated)

Log in to the database

mysql -uroot -p  //回车以后输入密码即可

Show the database directory

show databases;

As shown:
Insert picture description here

information_scheam  //存的是视图
mysql//核心数据,存很多的表文件
performance_schema//对性能提升做操作的数据库   存的是表
//前三个尽量不动

Query the statements needed to create a database and view its character set

show create database mysql;

As shown in the figure:
Insert picture description here
you can see that the character set utf-8 is marked in the comment behind. It explains the encoding format of our database.

Create a database:

create database db1;//db1可以换成你想创建的任何数据库的名字

To avoid mistakes

create database if not exists db1;//我们可以在建表时看一下我们的名称是否重复,避免报错

When creating a new database, you can change its character set

create database db2 character set gbk;

Small exercise:
create a database db4, and check whether it exists in advance and change the character set to gbk

create database  if not exists db4 character set gbk;

Modify the character set of the database

alter database db4 character set utf8mb4;

Delete database

drop database if exists db4;//避免报错加上if语句

Use database

//查看当前使用的数据库
select database();
//使用数据库
use db1;//注意直接使用数据库名

Table operations
View the names of all tables in the database

show tables;

Structure of the lookup table

desc 表名;//desc 是描述的意思

Create table

 create table student(
     id int,
     name varchar(32),
     age int,
     score double(4,1),
     birthday date,
     insert_time timestamp
     );
     //前面是名称后面是类型
     timestamp是系统当前的时间

Copy table

create table 表名 like 基表;

Delete table

drop table if exists student;
或者直接
drop table;


Modify table

修改表名
alter table 表名 rename to 新表名;
修改表的字符集
alter table 表名 character set gbk;
添加一列
alter table 表名 add 列名 数据类型;
修改列名称,类型
alter table 表名 change 原列名 新列名 新类型;
若不改变名称只改变类型
alter table 表名 modify 原列名  新类型;
删除列
alter table stu drop sex;

DML: Add, delete and modify data in the table
1. Add data
insert into table name (column name 1, column name 2, …, column name n) values ​​(value 1, value 2, …, value 3);
or
insert into table name values (Value 1, value 2, ..., value 3);

Guess you like

Origin blog.csdn.net/weixin_45663946/article/details/108637366