Add, delete, modify and check mysql database

One, log in to the mysql database

The basic structure of the database

  • Structure: The default administrator of the mysql database is root库——表——数据

Login format:

  • When there is no password: mysql -u root
    Login without password
  • Set the password for the first time: mysqladmin -u root -p password'password value' (输入后,下一步直接回车即可)
    Set a password for the first time
  • Log in with a password as: mysql -u root -p (按照提示输入密码)or mysql -u root -p password value(直接登录)
    mysql -u root -p
    mysql -u root -p123.com
  • Not changing the password for the first time: mysql -u root -p password(按照提示先输入旧密码,然后根据提示输入两次新密码)
    Not changing the password for the first time
    Not changing the password for the first time

2. Exit and view the database and related rules

Basically, all mysql operation statements need to end with ';', but some do not need it, but it is better to develop a habit of (;与 , 要使用英文的)inputting case-insensitively. Generally speaking, after executing the command, it will prompt "OK" of

  • View the basic information of the database service:
    msyql>status;
    status
    exit the database: exit; or quit;
    exit
    quit

Three, view and create libraries and tables in the database

Command format:

***查看数据库中有那些库
mysql>show databases; 

***切换到指定的库中
mysql>use 库名;  

***查看当前库中有哪些表
mysql>show tables;  

***查看mysql库中指定表的数据结构
mysql>describe 库名.表名;  

***创建表
mysql>create table 库名.表名 (字段1名称 类型,字段2名称 类型,... primary key (主键名)); 

***删除指定表
mysql>drop table 库名.表名;

***删除指定库
mysql>drop database 库名; 

Example:

  • View which libraries exist in the current database
    mysql>show databases;
    Check out those libraries
  • Switch to the specified library
    mysql>use mysql;
    Switch to the specified library
  • View which tables are in the current library
    mysql>show tables;
    See which tables are in the library
  • View the structure of the table
    mysql>describe user; (得先切换到指定库之后才能直接查看)
    mysql>describe mysql.user;(这个不需要切换到指定库,前面跟上表所在的库就可以)
    describe user;
    describe mysql.user;
  • Create a new library
    mysql>create database aaa; (create a library called aaa)
    Create a new library
  • Create a new table
    mysql>create table aaa.abc (xm char(16) not null,nl char(12) default '',sx char(10),yy char(10),primary key (xm));
    here The char(16) in the brackets means that you can enter up to 16 characters.
    Not null means that it is not allowed to be empty.
    Default '' means that the default is empty, but if you specify how much is to insert data, then it is how much, only when inserting data, not Write, it will become empty by default. (如果'20'那么就是默认为20,这里的什么也没有,所以是空)
    Primary key (xm) This means that the primary key is xm, and the primary key is unique and cannot be empty. That is to say, the data of xm cannot be repeated. After

    (写的过程中我的'老是变成’这个,所有的命令只要有'都是',不是‘这个,并且所有的标点符号都是英文的)
    Create a new table
    creating, you can use describe to view the structure of the table. Display basic information of the table
    View the structure of the table
  • Delete the specified table
    mysql>drop table aaa.abc;
    Delete the specified table
  • Delete the library
    mysql>drop database aaa;
    Delete the specified library
    after deleting, you will use show databases; if you look at it, you will find that the created library is gone
    View

Four, manage the data in the table

Command format:

往表中插入数据
mysql>insert into 库名.表名 (字段1名称,字段2名称,...) values('字段1的值','字段2的值',...;
简化版:insert into 库名.表名 values('字段1的值','字段2的值',...);  
(这个是把字段隐藏了,写字段值的时候按表的数据结构的顺序写就行)(每个字段值都要加''
如:insert aaa.bbb (姓名,数学,计算机) values('李蔚','88','77');
即往aaa这个库中的bbb表中的字段 姓名,数学,计算机 里插入数据 李蔚 88 77

查询数据记录
mysql>select 字段1名称,字段2名称,... from 库名.表名 where 条件; (select * 表示查询全部,也可以根据条件查询)
如:select 姓名,数学,计算机 from aaa.bbb where 数学= '88';
即查询aaa这个库中bbb中数学成绩等于88 的数据,并且只显示字段 姓名,数学,计算机
    select  * from aaa.bbb; 
即查询aaa库中的bbb表中的所有数据记录

修改表中的数据
mysql>update 库名.表名 set 字段1名称=新值,字段2名称=新值... where 条件;
如: update  aaa.bbb set 计算机='80',数学='82' where 姓名='刘波';
即把aaa库中的bbb表中的 姓名是刘波  的数据记录的 计算机字段值改成 80 ,数学字段值改成 82

删除表中指定的数据
mysql>delete from 库名.表名 where 条件;
如: delete from aaa.bbb where 姓名='魏帅'; 
即删除aaa库中的bbb表中的姓名字段值是魏帅的所有数据

Example:

  • Insert data into the table
    Insert data
  • Query data records
    Query data records
    Query by condition
  • Modify the data in the table
    Modify the data in the table
    Check again and find that the modification has been successful
    Modify the data in the table
  • Delete the specified data in the table
    Delete the specified data in the table
    Check again and find that it has been deleted successfully

Delete data in the table

Five, database user authorization

  • Reason: The root account in the Mysql database has all permissions for all libraries and tables(权限太高会产生安全风险)
  • Solution: Create some low-privileged users and let the account only be responsible for part of the library and table management and maintenance

Command format:

授予权限
mysql>grant 权限列表 on 库名.表名 to 用户名@指定的ip地址 identified by '密码值';
权限列表:select (查询)、  insert (插入)、update (修改)、delete (删除)、all(所有)等
库名.表名 针对指定的库和表,* 代表所有
用户名@指定的ip地址  用于指定用户名称和允许访问的客户端地址 (localhost就是本机)
identified  by '密码值'  设置用户连接mysql数据库时所用的密码
如:grant select on aaa.* to 'aaa'@'localhost' identified   by  '123456';
即授权一个用户名是aaa,允许从本机登录,对aaa库中的所有表有查询的权限,密码是123456

查看权限
mysql>show grants for 用户名@指定的ip地址;
如:show grants for boss@localhost;
即查看boss在本机的权限

取消权限
mysql>revoke 权限列表 on 库名.表名 from 用户名@指定的ip地址;
如:revoke all on aaa.* from boss@loaclhost;
即取消boss对aaa库中表的所有权限

Example:

  • Granted permission
    Granted permission
  • View permissions
    View the permissions of the specified user
  • Cancel permission
    Insert picture description here
    Check again
    Check permissions again

Guess you like

Origin blog.csdn.net/rzy1248873545/article/details/110518619