Mysql basic operation instructions [Summary]

1. Open the client to connect to Mysql:window+r cmd

  • Execute in the terminal: mysql -uroot -pEnter
  • Check the installation path of Mysql:show variables like '%dir%';

2. Database related operations:

  • Query all databases:show databases;
  • Create a database:create database 数据库名称;
  • View database details:show create database 数据库名称
  • Create a database (specify the character set):create database 数据库名称 character set gbk/utf8;
  • Delete the database:drop database 数据库名称;
  • Use database:use 数据库名称;

3. Related to the storage engine of the database: https://www.cnblogs.com/y-rong/p/8110596.html

  • Check the storage engines supported by the database: show engines;orshow engines \G;
  • View the storage engine used by the current database: show variables like 'storage_engine';orshow variables like '%storage_engine%';
  • You can also show create table 表名check the engine used by a table
  • If you did not change it yourself when you created it, the default engine used by the table should be consistent with the database;
  • There is no necessary connection between the storage engine of the database and the storage engine of the table, but the priority is different. If the character set of the table is not set, the default character set of the database will be used, and nothing more.

4. Related operations on database tables:

  • Create table:create table 表名(字段名 类型);
  • Modify the engine and character set of the table when creating the table: create table 表名(字段名 类型) engine = myisam/innodb(默认) charset = gbk;
  • View table:show tables;
  • View the details of a table:show create table 表名;
  • View the fields of a table:desc 表名;
  • Delete table:drop table 表名;

5. Operations related to database table fields:

  • Rename the table:rename table 表名 to 新表名;
  • Modify engine and character set:alter table 表名 engine=myisam/innodb charset=gbk;
  • Add fields:alter table 表名 add 字段名 类型 first/after XXX;
  • Modify the name of the field:alter table 表名 change 字段名 新字段名 类型;
  • Modify the position of the field:alter table 表名 modify 字段名 类型 first/after XXX;
  • Delete field:alter table 表名 drop 字段名;
  • Delete the primary key in a table:alter table 表名 drop primary key;
  • Add a primary key for the inserted field:alter table 表名 add primary key (主键);

6. Data related:

  1. Insert data:
    • First create an emp table:create table emp(id int ,name varchar(10),age int,sal int);
    • Insert data in the entire table (the order can not be wrong, nor can you add less attributes):insert into emp values(1,'tom',18,3000);
  • Specify the field to insert data:
    • insert into emp(name,age)values('jerry',19);
    • insert into emp(name)values('李白');
  • Insert data in bulk:
    • insert into emp values(3,'刘备',28,60000),(4,'张飞',30,'90000');
    • insert into emp(name,age)values('悟空',500),('八戒',400);
  1. Query data:

    • Query all field information of all data:select * from 表名;
    • Example: Query the names and ages of all employees:select name,age from emp;
  2. change the data:

    • grammar:update 表名 set 字段名=值;
    • Example: Modify TOM's salary to 3333:update emp set sal = 3333 where name ='TOM';
  3. delete data:

    • grammar:delete from 表名 where 字段名=值;
    • Example: Delete people under the age of 25 in the emp table:delete from emp where age < 25;

7. Chinese character set issues:set names gkb;

Guess you like

Origin blog.csdn.net/weixin_44296929/article/details/108572458