The detailed operation of three rows of

An introduction

MySQL data operations: DML

========================================================

  In MySQL management software, the operating data can be achieved through the SQL statement DML language, including

  1. Insertion of data implemented using INSERT
  2. UPDATE realize update data
  3. Use DELETE for data deletion
  4. SELECT query and use data.

=========================================================

  The main contents include:

    Insert data
    update data
    delete data
    query data

Two insertion (increase) the data insert

1. 插入完整数据(顺序插入)
    语法一:
    INSERT INTO 表名(字段1,字段2,字段3…字段n) VALUES(值1,值2,值3…值n); #指定字段来插入数据,插入的值要和你前面的字段相匹配

    语法二:
    INSERT INTO 表名 VALUES (值1,值2,值3…值n); #不指定字段的话,就按照默认的几个字段来插入数据

2. 指定字段插入数据
    语法:
    INSERT INTO 表名(字段1,字段2,字段3…) VALUES (值1,值2,值3…);

3. 插入多条记录
    语法:#插入多条记录用逗号来分隔
    INSERT INTO 表名 VALUES
        (值1,值2,值3…值n),
        (值1,值2,值3…值n),
        (值1,值2,值3…值n);
        
4. 插入查询结果
    语法:
    INSERT INTO 表名(字段1,字段2,字段3…字段n) 
                    SELECT (字段1,字段2,字段3…字段n) FROM 表2
                    WHERE …; #将从表2里面查询出来的结果来插入到我们的表中,但是注意查询出来的数据要和我们前面指定的字段要对应好

Three update (modify) data UPDATE

语法:
    UPDATE 表名 SET 
        字段1=值1,  #注意语法,可以同时来修改多个值,用逗号分隔
        字段2=值2,
        WHERE CONDITION; #更改哪些数据,通过where条件来定位到符合条件的数据

示例:
    UPDATE mysql.user SET password=password(‘123’) 
        where user=’root’ and host=’localhost’; #这句话是对myslq这个库中的user表中的user字段为'root'并且host字段为'localhost'的这条记录的password字段的数据进行修改,将passord字段的那个数据改为password('123')这个方法对123加工后的密码数据,password()这个方法是mysql提供的密码进行加密用的方法。
        定位到某个记录,并把这个记录中的某项内容更改掉

Four delete data delete

语法:
    DELETE FROM 表名 
        WHERE CONITION; #删除符合条件的一些记录
    DELETE FROM 表名;如果不加where条件,意思是将表里面所有的内容都删掉,但是清空所有的内容,一般我们用truncate ,能够将id置为零,delete不能将id置零,再插入数据的时候,会按照之前的数据记录的id数继续递增
示例:
    DELETE FROM mysql.user 
        WHERE password=’123’;

练习:
    更新MySQL root用户密码为mysql123
    删除除从本地登录的root用户以外的所有用户

Five query data SELECT (Key)

  In our work less, most of the scenes are additions and deletions to the operation of the data, and more data read operation, so we focus on reading data here.

  Before we say that we are not the data into multiple tables to save, rather than all the data into a table inside, such as we said earlier the department table and an employee table, employee information into a table information into a department inside the department table inside.

  Although we have data assigned to different tables inside, but these data are not belong to us with a project, that is, your future query data, it may not just to query data from a table inside, may involve check out what data from multiple tables, which is associated with multi-table queries, right, so let's take a look at the single-table queries learning, multi-table queries come to learn, see the following two blog:

Single-table query: Go ---> single-table queries mysql's

Multi-table query: Go ---> MySQL table as much query

Six rights management

#授权表
user #该表放行的权限,针对:所有数据,所有库下所有表,以及表下的所有字段
db #该表放行的权限,针对:某一数据库,该数据库下的所有表,以及表下的所有字段
tables_priv #该表放行的权限。针对:某一张表,以及该表下的所有字段
columns_priv #该表放行的权限,针对:某一个字段

#按图解释:
user:放行db1,db2及其包含的所有
db:放行db1,及其db1包含的所有
tables_priv:放行db1.table1,及其该表包含的所有
columns_prive:放行db1.table1.column1,只放行该字段
#创建用户
create user 'egon'@'1.1.1.1' identified by '123';
create user 'egon'@'192.168.1.%' identified by '123';
create user 'egon'@'%' identified by '123';


#授权:对文件夹,对文件,对文件某一字段的权限
查看帮助:help grant
常用权限有:select,update,alter,delete
all可以代表除了grant之外的所有权限

#针对所有库的授权:*.*
grant select on *.* to 'egon1'@'localhost' identified by '123'; #只在user表中可以查到egon1用户的select权限被设置为Y

#针对某一数据库:db1.*
grant select on db1.* to 'egon2'@'%' identified by '123'; #只在db表中可以查到egon2用户的select权限被设置为Y

#针对某一个表:db1.t1
grant select on db1.t1 to 'egon3'@'%' identified by '123';  #只在tables_priv表中可以查到egon3用户的select权限

#针对某一个字段:
mysql> select * from t3;
+------+-------+------+
| id   | name  | age  |
+------+-------+------+
|    1 | egon1 |   18 |
|    2 | egon2 |   19 |
|    3 | egon3 |   29 |
+------+-------+------+

grant select (id,name),update (age) on db1.t3 to 'egon4'@'localhost' identified by '123'; 
#可以在tables_priv和columns_priv中看到相应的权限
mysql> select * from tables_priv where user='egon4'\G
*************************** 1. row ***************************
       Host: localhost
         Db: db1
       User: egon4
 Table_name: t3
    Grantor: root@localhost
  Timestamp: 0000-00-00 00:00:00
 Table_priv:
Column_priv: Select,Update
row in set (0.00 sec)

mysql> select * from columns_priv where user='egon4'\G
*************************** 1. row ***************************
       Host: localhost
         Db: db1
       User: egon4
 Table_name: t3
Column_name: id
  Timestamp: 0000-00-00 00:00:00
Column_priv: Select
*************************** 2. row ***************************
       Host: localhost
         Db: db1
       User: egon4
 Table_name: t3
Column_name: name
  Timestamp: 0000-00-00 00:00:00
Column_priv: Select
*************************** 3. row ***************************
       Host: localhost
         Db: db1
       User: egon4
 Table_name: t3
Column_name: age
  Timestamp: 0000-00-00 00:00:00
Column_priv: Update
rows in set (0.00 sec)

#删除权限
revoke select on db1.* from 'egon'@'%';

Guess you like

Origin www.cnblogs.com/zhaohaiyu/p/11446921.html