数据库的语法

数据库的操作

DML操作

INSERT增

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| yh                 |
+--------------------+
5 rows in set (0.00 sec)

mysql> use yh;
Database changed

mysql> show tables;
+--------------+
| Tables_in_yh |
+--------------+
| student      |
+--------------+
1 row in set (0.00 sec)

上面创建了一个名为yh的库,在yh数据库中添加了一个名改为student的表
mysql> insert student(name,age) value('tom',20),('jerry',23),('wangqing',25),('sean',28),('zhangsan',26),('zhangsan',20),('lisi',null),('chenshuo',10),('wangwu',3),('quiyi',15),('qiuxiaotian',20);
//往student这个表中插入上述多条记录
Query OK, 11 rows affected (0.67 sec)
Records: 11  Duplicates: 0  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        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | quiyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
11 rows in set (0.14 sec)

UPDATE 改

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)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值