mysql> update student set age = 50 where name = 'lisi'; //修改lisi的年龄为50
Query OK, 1 row affected (0.19 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
| 6 | zhangsan | 20 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | quiyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
11 rows in set (0.10 sec)
mysql> update student set age = 100 where name = 'wangwu'; //修改wangwu的年龄为100
Query OK, 1 row affected (0.10 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
| 6 | zhangsan | 20 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 100 |
| 10 | quiyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
11 rows in set (0.00 sec)
SELECT查
mysql> select * from student; //*表示所有字段 //查看student表的内容
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
| 6 | zhangsan | 20 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | NULL |
| 10 | quiyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
11 rows in set (0.00 sec)
mysql> select name as n,age as a from student; //as表示字段别名,当表名很长时用别名代替
+-------------+------+
| n | a |
+-------------+------+
| tom | 20 |
| jerry | 23 |
| wangqing | 25 |
| sean | 28 |
| zhangsan | 26 |
| zhangsan | 20 |
| lisi | 50 |
| chenshuo | 10 |
| wangwu | NULL |
| quiyi | 15 |
| qiuxiaotian | 20 |
+-------------+------+
11 rows in set (0.00 sec)
mysql> select * from student where name = 'zhangsan'; //查询student表中名字叫zhangshan的记录
+----+----------+------+
| id | name | age |
+----+----------+------+
| 5 | zhangsan | 26 |
| 6 | zhangsan | 20 |
+----+----------+------+
2 rows in set (0.00 sec)
mysql> select * from student where age <25; //查找年龄小于25的记录
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 6 | zhangsan | 20 |
| 8 | chenshuo | 10 |
| 10 | quiyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
6 rows in set (0.09 sec)
mysql> select * from student where age between 23 and 30; //查询student表中年龄大于23小于30
+----+----------+------+
| id | name | age |
+----+----------+------+
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
+----+----------+------+
4 rows in set (0.00 sec)
mysql> select * from student order by age limit 7,4; //查询student表中年龄最大的4位同学
+----+----------+------+
| id | name | age |
+----+----------+------+
| 3 | wangqing | 25 |
| 5 | zhangsan | 26 |
| 4 | sean | 28 |
| 7 | lisi | 50 |
+----+----------+------+
4 rows in set (0.00 sec)
mysql> select * from student where age >20 and name = 'zhangsan'; //查询student表中名字叫zhangshan且年龄大于20岁的记录
+----+----------+------+
| id | name | age |
+----+----------+------+
| 5 | zhangsan | 26 |
+----+----------+------+
1 row in set (0.00 sec)
mysql> select * from student order by age desc; //以age字段降序排序
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 7 | lisi | 50 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
| 3 | wangqing | 25 |
| 2 | jerry | 23 |
| 1 | tom | 20 |
| 6 | zhangsan | 20 |
| 11 | qiuxiaotian | 20 |
| 10 | quiyi | 15 |
| 8 | chenshuo | 10 |
| 9 | wangwu | NULL |
+----+-------------+------+
11 rows in set (0.00 sec)
mysql> select * from student order by age limit 2,1; //查询student表中年龄最小的3位同学跳过前2位
+----+-------+------+
| id | name | age |
+----+-------+------+
| 10 | quiyi | 15 |
+----+-------+------+
1 row in set (0.00 sec)
DELETE 删
mysql> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangsan | 10 |
| 6 | zhangsan | 26 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | NULL |
| 10 | quiyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
11 rows in set (0.00 sec)
mysql> delete from student where age <20 and name = 'zhangsan'; //删除student中名字叫zhangshan且年龄小于等于20的记录
Query OK, 1 row affected (0.01 sec)
mysql> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 6 | zhangsan | 26 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | NULL |
| 10 | quiyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
10 rows in set (0.00 sec)
查看授权
mysql> show grants; //查看当前登录用户的授权
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
授权
mysql> grant all on *.* to 'root'@'192.168.147.1' identified by 'ZHANGde12+Jun'; //给192.168.147.1主机所有的权限
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges; //刷新权限重新读取
Query OK, 0 rows affected (0.00 sec)
mysql> show grants; //显示权限
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0.01 sec)
mysql> show grants for 'root'@'192.168.147.1'; //显示权限
+-------------------------------------------------------+
| Grants for root@192.168.147.1 |
+-------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.147.1' |
+-------------------------------------------------------+
1 row in set (0.00 sec)
取消权限
mysql> revoke select on *.* from 'root'@'192.168.147.1'; //取消权限
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for 'root'@'192.168.147.1'; //显示取消后的权限
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@192.168.147.1 |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE ON *.* TO 'root'@'192.168.147.1' |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)