三、对数据库表记录进行操作(修改DDL)

1、插入记录:insert

语法:
insert into 表 (列名1,列名2,列名3..) values (值1,值2,值3..);--向表中插入某些列
insert into 表 values (值1,值2,值3..);--向表中插入所有列

mysql> insert into tbl_user(uid,uname,upassword) values(null,'zhangsan','');
Query OK, 1 row affected (0.27 sec) mysql> select * from tbl_user;
+-----+----------+-----------+
| uid | uname | upassword |
+-----+----------+-----------+
| 1 | zhangsan | 123 |
+-----+----------+-----------+
mysql> insert into tbl_user values(null,'xiaobing','');
Query OK, 1 row affected (0.06 sec) mysql> select * from tbl_user;
+-----+----------+-----------+
| uid | uname | upassword |
+-----+----------+-----------+
| 1 | zhangsan | 123 |
| 2 | xiaobing | 456 |
+-----+----------+-----------+
2 rows in set (0.00 sec)

注意:中文插入乱码解决【方式一】修改安装目录的配置文件my.ini;---character-set-server=utf8;【方式二】mysql> set names utf-8;

2、更新记录:update
语法:
update 表名 set 字段名=值,字段名=值 ...;
update 表名 set 字段名=值,字段名=值 ... where 条件;

mysql> select * from tbl_user;
+-----+----------+-----------+
| uid | uname | upassword |
+-----+----------+-----------+
| 1 | zhangsan | 123 |
| 2 | xiaobing | 456 |
| 3 | 小兵 | 789 |
+-----+----------+-----------+
3 rows in set (0.00 sec) mysql> update tbl_user set upassword='';
Query OK, 3 rows affected (0.09 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> select * from tbl_user;
+-----+----------+-----------+
| uid | uname | upassword |
+-----+----------+-----------+
| 1 | zhangsan | 666 |
| 2 | xiaobing | 666 |
| 3 | 小兵 | 666 |
+-----+----------+-----------+
3 rows in set (0.00 sec)
mysql> update tbl_user set upassword='' where uid=3;
mysql> select * from tbl_user;
+-----+----------+-----------+
| uid | uname | upassword |
+-----+----------+-----------+
| 1 | zhangsan | 666 |
| 2 | xiaobing | 666 |
| 3 | 小兵 | 999 |
+-----+----------+-----------+
3 rows in set (0.00 sec)

3、删除记录:delete

语法:
delete from 表名 [where 条件];

mysql> select * from tbl_user;
+-----+----------+-----------+
| uid | uname | upassword |
+-----+----------+-----------+
| 1 | zhangsan | 999 |
| 2 | xiaobing | 666 |
+-----+----------+-----------+
2 rows in set (0.00 sec) mysql> delete from tbl_user where uid=1;
Query OK, 1 row affected (0.08 sec) mysql> select * from tbl_user;
+-----+----------+-----------+
| uid | uname | upassword |
+-----+----------+-----------+
| 2 | xiaobing | 666 |
+-----+----------+-----------+
注意:删除后uid不会重置!以上是带条件删除,以下是不带条件删除!
 mysql> delete from tbl_user;
Query OK, 1 row affected (0.07 sec) mysql> select * from tbl_user;
Empty set (0.00 sec)
注意:delete和truncate的区别?
  delete 删除的时候是一条一条的删除,它配合事务,可以将删除的数据找回。
  truncate 删除,它是将整个表摧毁,然后再创建一张一模一样的表,它的删除数据无法找回!
mysql> select * from tbl_user;
+-----+----------+-----------+
| uid | uname | upassword |
+-----+----------+-----------+
| 4 | xiaobing | 666 |
| 5 | xiaobing | 666 |
| 6 | xiaobing | 666 |
+-----+----------+-----------+
3 rows in set (0.00 sec) mysql> start transaction;(开启事务)
Query OK, 0 rows affected (0.08 sec) mysql> delete from tbl_user;
Query OK, 3 rows affected (0.00 sec) mysql> select * from tbl_user;
Empty set (0.00 sec) mysql> rollback;(回滚事务)
Query OK, 0 rows affected (0.08 sec) mysql> select * from tbl_user;
+-----+----------+-----------+
| uid | uname | upassword |
+-----+----------+-----------+
| 4 | xiaobing | 666 |
| 5 | xiaobing | 666 |
| 6 | xiaobing | 666 |
+-----+----------+-----------+
3 rows in set (0.00 sec)
mysql> start transaction;(开启事务)
Query OK, 0 rows affected (0.00 sec) mysql> truncate table tbl_user;
Query OK, 0 rows affected (0.41 sec) mysql> select * from tbl_user;
Empty set (0.00 sec) mysql> rollback;(回滚事务)
Query OK, 0 rows affected (0.00 sec) mysql> select * from tbl_user;
Empty set (0.00 sec)
mysql> insert into tbl_user values(null,'xiaobing','');
Query OK, 1 row affected (0.06 sec) mysql> select * from tbl_user;(truncate先摧毁表,后重建表!)
+-----+----------+-----------+
| uid | uname | upassword |
+-----+----------+-----------+
| 1 | xiaobing | 666 |
+-----+----------+-----------+
1 row in set (0.00 sec)
注意:delete删除,uid不会重置,不清空auto_increment记录数。!而使用truncate操作,uid会重置,auto_increment 将置为零,重新开始。!

四、查询操作(DQL 简单查询)

1、创建商品表,准备数据!

mysql> create table product(
-> pid int primary key auto_increment,
-> pname varchar(20),
-> price double,
-> pdate timestamp
-> );
Query OK, 0 rows affected (0.70 sec) mysql> desc product;
+-------+-------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+-------------------+-----------------------------+
| pid | int(11) | NO | PRI | NULL | auto_increment |
| pname | varchar(20) | YES | | NULL | |
| price | double | YES | | NULL | |
| pdate | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-------------+------+-----+-------------------+-----------------------------+
4 rows in set (0.05 sec)
#自动增长列:auto_increment,要求:1.必须整形(int) 2.必须键(主键)
mysql> insert into product values (null,'小兵',0.03,null)
Query OK, 1 row affected (0.10 sec) mysql> insert into product values (null,'小王',33,null);
Query OK, 1 row affected (0.07 sec) mysql> insert into product values (null,'小赵',-50,null);
Query OK, 1 row affected (0.11 sec)
2、查询操作,语法:select [distinct] *| 列名,列名 from 表名 [where条件]
2.1 查询所有
mysql> select * from product;
+-----+--------+-------+---------------------+
| pid | pname | price | pdate |
+-----+--------+-------+---------------------+
| 1 | 小兵 | 0.03 | 2019-01-28 15:48:46 |
| 2 | 小王 | 33 | 2019-01-28 15:49:11 |
| 3 | 小赵 | -50 | 2019-01-28 15:49:32 |
+-----+--------+-------+---------------------+
3 rows in set (0.05 sec) 2.2 查询商品名称和商品价格
mysql> select pname,price from product;
+--------+-------+
| pname | price |
+--------+-------+
| 小兵 | 0.03 |
| 小王 | 33 |
| 小赵 | -50 |
+--------+-------+
3 rows in set (0.00 sec) 2.3 查询所有商品信息使用表别名!
mysql> select * from product as p;
+-----+--------+-------+---------------------+
| pid | pname | price | pdate |
+-----+--------+-------+---------------------+
| 1 | 小兵 | 0.03 | 2019-01-28 15:48:46 |
| 2 | 小王 | 33 | 2019-01-28 15:49:11 |
| 3 | 小赵 | -50 | 2019-01-28 15:49:32 |
+-----+--------+-------+---------------------+
3 rows in set (0.00 sec) mysql> select * from product p;(as可以省略)
+-----+--------+-------+---------------------+
| pid | pname | price | pdate |
+-----+--------+-------+---------------------+
| 1 | 小兵 | 0.03 | 2019-01-28 15:48:46 |
| 2 | 小王 | 33 | 2019-01-28 15:49:11 |
| 3 | 小赵 | -50 | 2019-01-28 15:49:32 |
+-----+--------+-------+---------------------+
3 rows in set (0.00 sec) 2.4 查询商品名使用别名! mysql> select pname as p from product;
+--------+
| p |
+--------+
| 小兵 |
| 小王 |
| 小赵 |
+--------+
3 rows in set (0.00 sec) 2.5 去掉重复值(按照价格)
mysql> select * from product;
+-----+--------+-------+---------------------+
| pid | pname | price | pdate |
+-----+--------+-------+---------------------+
| 1 | 小兵 | 0.03 | 2019-01-28 15:48:46 |
| 2 | 小王 | 33 | 2019-01-28 15:49:11 |
| 3 | 小赵 | -50 | 2019-01-28 15:49:32 |
| 4 | 小王 | 33 | 2019-01-28 16:16:45 |
+-----+--------+-------+---------------------+
4 rows in set (0.00 sec) mysql> select distinct(price) from product;
+-------+
| price |
+-------+
| 0.03 |
| 33 |
| -50 |
+-------+
3 rows in set (0.06 sec) 2.6 将所有的价格+10,进行展示:
mysql> select pname,price+10 from product;
+--------+----------+
| pname | price+10 |
+--------+----------+
| 小兵 | 10.03 |
| 小王 | 43 |
| 小赵 | -40 |
| 小王 | 43 |
+--------+----------+
4 rows in set (0.04 sec)

五、查询操作(DQL 条件查询)

1、查询名称为小王的商品信息

mysql> select * from product where pname='小王';
+-----+--------+-------+---------------------+
| pid | pname | price | pdate |
+-----+--------+-------+---------------------+
| 2 | 小王 | 33 | 2019-01-28 15:49:11 |
| 4 | 小王 | 33 | 2019-01-28 16:16:45 |
+-----+--------+-------+---------------------+
2 rows in set (0.08 sec)

2、查询商品价格>0元的所有的商品信息 

mysql> select * from product where price>0;
+-----+--------+-------+---------------------+
| pid | pname | price | pdate |
+-----+--------+-------+---------------------+
| 1 | 小兵 | 0.03 | 2019-01-28 15:48:46 |
| 2 | 小王 | 33 | 2019-01-28 15:49:11 |
| 4 | 小王 | 33 | 2019-01-28 16:16:45 |
+-----+--------+-------+---------------------+
3 rows in set (0.03 sec)

3、查询商品价格带有“赵”字的商品信息;(模糊查询)

mysql> select * from product where pname like '%赵%';
+-----+--------+-------+---------------------+
| pid | pname | price | pdate |
+-----+--------+-------+---------------------+
| 3 | 小赵 | -50 | 2019-01-28 15:49:32 |
+-----+--------+-------+---------------------+
1 row in set (0.04 sec)

4、查询商品ID在(1,3,5)范围内的所有商品;

mysql> select * from product where pid in (1,3,9);
+-----+--------+-------+---------------------+
| pid | pname | price | pdate |
+-----+--------+-------+---------------------+
| 1 | 小兵 | 0.03 | 2019-01-28 15:48:46 |
| 3 | 小赵 | -50 | 2019-01-28 15:49:32 |
+-----+--------+-------+---------------------+
2 rows in set (0.04 sec)

5、查询名称为小王并且(and)ID为4的商品信息

mysql> select * from product where pname='小王' and pid=4;
+-----+--------+-------+---------------------+
| pid | pname | price | pdate |
+-----+--------+-------+---------------------+
| 4 | 小王 | 33 | 2019-01-28 16:16:45 |
+-----+--------+-------+---------------------+
1 row in set (0.04 sec)

注意参考补充:

MySQL---DDL+DQL---(四)的更多相关文章

  1. MySQL 系列(四)主从复制、备份恢复方案生产环境实战

    第一篇:MySQL 系列(一) 生产标准线上环境安装配置案例及棘手问题解决 第二篇:MySQL 系列(二) 你不知道的数据库操作 第三篇:MySQL 系列(三)你不知道的 视图.触发器.存储过程.函数 ...

  2. MySQL表的四种分区类型

    MySQL表的四种分区类型 一.什么是表分区 通俗地讲表分区是将一大表,根据条件分割成若干个小表.mysql5.1开始支持数据表分区了. 如:某用户表的记录超过了600万条,那么就可以根据入库日期将表 ...

  3. MySQL基础(四)——索引

    MySQL基础(四)--索引

  4. MySQL ddl丢表

      MySQL ddl丢表: MySQL server层为了和innodb层保持数据一致性,在写binlog和redo log时,引入了两阶段提交,但不同的变更产生的日志并非都使用这种策略. 下面就来 ...

  5. 数据库操作事物的四大特性以及MySQL数据库的四种隔离级别

    1 .事物操作数据库的四大特性(ACID) 1.原子性 (Atomicity) 原子性:就是事物的所包含的所有操作,要么全部成功,要么全部失败回滚. 2.一致性 (Consistency) 一致性:简 ...

  6. linux下查看mysql版本的四种方法

    Linux查看MySQL版本的四种方法 1 在终端下执行 mysql -V 2 在help中查找 mysql --help |grep Distrib 3 在mysql 里查看 select vers ...

  7. mysql DDL 锁表

    mysql DDL 锁表 select trx_state, trx_started, trx_mysql_thread_id, trx_query from information_schema.i ...

  8. MySql学习笔记四

    MySql学习笔记四 5.3.数据类型 数值型 整型 小数 定点数 浮点数 字符型 较短的文本:char, varchar 较长的文本:text, blob(较长的二进制数据) 日期型 原则:所选择类 ...

  9. MySQL性能优化(四):SQL优化

    原文:MySQL性能优化(四):SQL优化 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/ ...

  10. MySQL的DQL语言(查)

    MySQL的DQL语言(查) DQL:Data Query Language,数据查询语言. DQL是数据库中最核心的语言,简单查询,复杂查询,都可以做,用select语句. 1. 查询指定表的全部字 ...

随机推荐

  1. Azure DevOps

    Azure DevOps https://azure.microsoft.com/zh-cn/services/devops/ It looks great!

  2. python读取导出数据

    1,python读取csv的某一列 import pandas as pd data1 = pd.read_csv('cotton.csv', usecols=[0, 1], encoding='ut ...

  3. Git中.gitignore文件不起作用的解决以及Git中的忽略规则介绍

    在Studio里使用Git管理代码的过程中,可以修改.gitignore文件中的标示的方法来忽略开发者想忽略掉的文件或目录,如果没有.gitignore文件,可以自己手工创建.在.gitignore文 ...

  4. jira,Confluence 的JVM内存优化

    个人喜好,可能不是很适合大众 JIRA 修改setenv.sh,调整JVM为4096m [root@jira_confluence ~]# cd /app/atlassian/jira/bin/ [r ...

  5. 命令级的python静态资源服务。

    python -m SimpleHTTPServer 在当前目录起python静态资源服务.

  6. 3d

    http://jokerwang.com/diy-3d%E6%89%93%E5%8D%B0%E6%9C%BA1-%E7%A1%AC%E4%BB%B6%E7%AF%87/

  7. Mysql 查询当月时间数据

    SELECTDATE_FORMAT(CURDATE(), '%Y%m'), DATE_FORMAT(t.transactiontime, '%Y%m'),t.*FROM ttransactions t ...

  8. UVA - 11427 Expect the Expected (概率dp)

    Some mathematical background. This problem asks you to compute the expected value of a random variab ...

  9. Django web编程1 -- 创建项目和应用

    python:3.7.2 Django:2.1.7 1.创建虚拟环境 虚拟环境是系统的一个位置,可以在其中安装包,并将其与其他python包隔离. 创建目录,命名为learning_log,并切换到这 ...

  10. Tomcat系列(9)——Tomcat 6方面调优(内存,线程,IO,压缩,缓存,集群)

    核心部分 内存 线程 IO 压缩 缓存 集群 一.JVM内存优化 Tomcat内存优化,包括内存大小,垃圾回收策略. Windows 下的catalina.bat,Linux 下的catalina.s ...