MySQL---DDL+DQL---
Posted い哎哟喂〤
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL---DDL+DQL---相关的知识,希望对你有一定的参考价值。
三、对数据库表记录进行操作(修改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\',\'123\'); 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\',\'456\'); 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=\'666\'; 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=\'999\' 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\',\'666\'); 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 | priceMySQL--各版本DDL 操作总结如何将 Aurora Mysql ddl 翻译成 Redshift ddl