码迷,mamicode.com
首页 > 数据库 > 详细

mysql数据库基本操作

时间:2018-12-03 10:27:26      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:var   整数   balance   命令   mysql   ascii码   失效   3.5   表结构   

1.数据库的操作

1.1 创建数据库

mysql> create database python1806;
Query OK, 1 row affected (0.00 sec)

 

1.2 查看数据库

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

  

1.3 选中数据库

mysql> use python1806
Database changed

  

1.4 查看数据库中的数据表

mysql> show tables;
+----------------------+
| Tables_in_python1806 |
+----------------------+
| info                 |
| money                |
| order_goods          |
| table01              |
| table02              |
| table03              |
| table04              |
| tb                   |
| test                 |
| user                 |
| user_info            |
+----------------------+
11 rows in set (0.01 sec)

  

1.5 删除数据库

mysql> drop database hellow;
Query OK, 0 rows affected (0.00 sec)

  

2.数据表的操作

2.1 创建数据表

create table 表名(字段名 类型(长度),字段1 类型(长度));

mysql> create table user(id int(11),name char(64),age int(4));
Query OK, 0 rows affected (0.44 sec)

  

2.2 查看表结构

mysql> desc user;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | YES  |     | NULL    |       |
| name  | char(64) | YES  |     | NULL    |       |
| age   | int(4)   | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.11 sec)

  

2.3 删除数据表

mysql> drop table hellow;
Query OK, 0 rows affected (0.39 sec)

  

2.4 创建数据表(指定引擎和字符集)

如果只创建表不指定引擎,mysql5.7默认是innodb引擎,mysql5.7以前是myisam引擎。

mysql> create table info(id int(11),name varchar(64),age int(3))engine=myisam default charset=utf8;
Query OK, 0 rows affected (0.08 sec)

  

2.5 查看表的创建过程

mysql> show create table info;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                           |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
| info  | CREATE TABLE `info` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(64) DEFAULT NULL,
  `age` int(3) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.08 sec)
mysql> show create table info\G;
*************************** 1. row ***************************
       Table: info
Create Table: CREATE TABLE `info` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(64) DEFAULT NULL,
  `age` int(3) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
?
ERROR: 
No query specified

  

3.数据字段的操作

+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(64) | YES  |     | NULL    |       |
| age   | int(3)      | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
?
field      字段名 
type       字段类型 
null       是否为空 
key        是否为主键 
default    默认值
extra      备注信息

  

3.1 修改表字段类型

alter table 表名 modify 字段名 新类型(长度);

mysql> alter table info modify name char(32);
Query OK, 0 rows affected (0.63 sec)
Records: 0  Duplicates: 0  Warnings: 0

  

3.2 调整字段顺序

alter table 表名 modify 字段名 类型(长度) first;

mysql> alter table info modify name char(32) first;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

  

注意:

1.只有first,没有second,last等。

2.类型和长度可以自己重新定义,不需要非得是原来的类型和长度。

3.3 添加表字段

alter table 表名 add column 字段名 类型(长度);

mysql> alter table info add column sex int(1);
Query OK, 0 rows affected (0.07 sec)
Records: 0  Duplicates: 0  Warnings: 0

  

3.4 添加表字段(控制字段顺序)

alter table 表名 add 字段名 字段类型(长度) after 指定字段名;

mysql> alter table info add tel int(11) after name;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

  

3.5 删除表字段

alter table 表示 drop column 字段名;

mysql> alter table info drop column tel;
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0

  

3.6 表字段改名

alter table 表名 change 原字段名 新字段名 类型(长度);

mysql> alter table info change id idcard char(18);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

  

3.7 修改表名

alter table 表名 rename 新名字;

mysql> alter table info rename user_info;
Query OK, 0 rows affected (0.02 sec)

  

4.mysql数据类型

  • 数值类型(整型 浮点型)
  • 字符串
  • 日期时间
  • 符合类型
  • 空间类型 (用来科学计算)

4.1 数值类型

整型

技术分享图片

 

浮点型

技术分享图片

 

 

定点数

decimal(m,d) 参数m<65 是总个数,d<30且 d<m 是小数位。

注意:decimal定点数 定点类型在数据库中存放的是精确值 如果某个字段精度要求比较高比如银行金额选用decimal

4.2 字符类型

技术分享图片

注意:char的存储需求是定义时指定的固定长度;varchar的存储需求是取决于实际值长度。 

4.3 时间类型

技术分享图片

注意:若定义一个字段为timestamp,这个字段里的时间数据会随其他字段修改的时候自动刷新,所以这个数据类型的字段可以存放这条记录最后被修改的时间。

4.4 复合类型

复合类型

技术分享图片

 

5.unsigned和zerofill

5.1 unsigned

unsigned 就是将数字类型无符号化, 例如 int 型的范围:-2^31 ~ 2^31 - 1,而unsigned int的范围:0 ~ 2^32。看起来unsigned 是个不错的类型,尤其是用在自增或者没有负数的情况。

5.2 zerofill

zerofill 类似一个显示属性,首先来看看MySQL数据库中数字类型后面的长度是什么意思呢,使用show create table 命令来看表的建表语句。

6.字符集

  • ASCII码 单字节
  • GBK 双字节
  • Unicode 4个字节
  • 万国码 容纳世界上所有文字和符号的字符编码方案、能够跨语言跨平台
  • utf-8 1-6个字节 针对Unicode可变字符编码
实际工作用需要的编码
  • gbk_chinese_ci 简体中文 不区分大小写
  • utf8_general_ci unicode 多语言、不区分大小写

7.表引擎

技术分享图片

Innodb引擎

Innodb引擎提供了对数据库ACID事务的支持,并且实现了SQL标准的四种隔离级别。该引擎还提供了行级锁和外键约束,它的设计目标是处理大容量数据库系统,它本身其实就是基于MySQL后台的完整数据库系统,MySQL运行时Innodb会在内存中建立缓冲池,用于缓冲数据和索引。但是该引擎不支持FULLTEXT类型的索引,而且它没有保存表的行数,当SELECT COUNT(*) FROM TABLE时需要扫描全表。当需要使用数据库事务时,该引擎当然是首选。由于锁的粒度更小,写操作不会锁定全表,所以在并发较高时,使用Innodb引擎会提升效率。但是使用行级锁也不是绝对的,如果在执行一个SQL语句时MySQL不能确定要扫描的范围,InnoDB表同样会锁全表。

MyISAM引擎

MyISAM是MySQL默认的引擎,但是它没有提供对数据库事务的支持,也不支持行级锁和外键,因此当INSERT(插入)或UPDATE(更新)数据时即写操作需要锁定整个表,效率便会低一些。不过和Innodb不同,MyISAM中存储了表的行数,于是SELECT COUNT(*) FROM TABLE时只需要直接读取已经保存好的值而不需要进行全表扫描。如果表的读操作远远多于写操作且不需要数据库事务的支持,那么MyISAM也是很好的选择。

8.索引

  • 普通索引 最基本的索引 没有任何限制
  • 唯一索引 要求该字段对应的列 不能有重复值 年龄 性别 不能添加唯一索引
  • 主键索引 特殊的唯一索引 要求这一列不能有重复值 不能为空
  • 复合索引 所谓的复合索引就是 对多个字段同时添加索引
  • 全文索引 需要对数据全局搜索 需要添加全文索引

8.1 普通索引

show index from 表名

mysql> show index from user\G;
*************************** 1. row ***************************
        Table: user
   Non_unique: 0
     Key_name: PRIMARY
 Seq_in_index: 1
  Column_name: id
    Collation: A
  Cardinality: 9
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
1.Table 
表的名称。
2.Non_unique
如果索引不能包括重复词,则为0。如果可以,则为1。
3.Key_name
索引的名称。
4.Seq_in_index
索引中的列序列号,从1开始
5.Column_name
列名称。
6.Collation
列以什么方式存储在索引中。在MySQL中,有值‘A’(升序)或NULL(无分类)。
7.Cardinality
索引中唯一值的数目的估计值。通过运行ANALYZE TABLE或myisamchk -a可以更新。基数根据被存储为整数的统计数据来计数,所以即使对于小型表,该值也没有必要是精确的。基数越大,当进行联合时,MySQL使用该索引的机会就越大。
8.Sub_part
如果列只是被部分地编入索引,则为被编入索引的字符的数目。如果整列被编入索引,则为NULL。
9.Packed
指示关键字如何被压缩。如果没有被压缩,则为NULL。
10.Null
如果列含有NULL,则含有YES。如果没有,则该列含有NO。
11.Index_type
用过的索引方法(BTREE, FULLTEXT, HASH, RTREE)。
12.Comment
多种评注。
1)添加索引

alter table 表名 add index(字段);

alter table 表名  add index(字段);

  

2)查看索引

show index from 表名;<br/> \G 最佳阅读体验阅读

mysql> alter table user add index(name)show index from user\G
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘show index from user‘ at line 1
mysql> show index from user;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| user  |          0 | PRIMARY  |            1 | id          | A         |           9 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
?
mysql> show index from user\G
*************************** 1. row ***************************
        Table: user
   Non_unique: 0
     Key_name: PRIMARY
 Seq_in_index: 1
  Column_name: id
    Collation: A
  Cardinality: 9
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
1 row in set (0.00 sec)

  

3)添加索引并命名

alter table 表名 add index 索引名(字段名);

mysql> alter table user add index in_username(username);
Query OK, 9 rows affected (0.90 sec)
Records: 9  Duplicates: 0  Warnings: 0

  

4)删除索引

alter table 表名 drop index 索引名

mysql> alter table user drop index in_username;
Query OK, 9 rows affected (0.18 sec)
Records: 9  Duplicates: 0  Warnings: 0

  

8.2 唯一索引

alter table user add unique(name);

mysql> alter table user add unique un_id(id);
Query OK, 9 rows affected (0.08 sec)
Records: 9  Duplicates: 0  Warnings: 0
?
mysql> show index from user\G
*************************** 1. row ***************************
        Table: user
   Non_unique: 0
     Key_name: PRIMARY
 Seq_in_index: 1
  Column_name: id
    Collation: A
  Cardinality: 9
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
*************************** 2. row ***************************
        Table: user
   Non_unique: 0
     Key_name: un_id
 Seq_in_index: 1
  Column_name: id
    Collation: A
  Cardinality: 9
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
2 rows in set (0.00 sec)

  

8.3 主键索引

1)删除主键索引
mysql> alter table user drop primary key;
Query OK, 9 rows affected (0.07 sec)
Records: 9  Duplicates: 0  Warnings: 0

  

注意:如果主键自动递增,会删不掉,需要先消除主键自增后再次删除。(alter table user modify id int(11) unsigned not null;)

2)添加主键索引
mysql> alter table user add primary key(id);
Query OK, 9 rows affected (0.06 sec)
Records: 9  Duplicates: 0  Warnings: 0

  

注意:添加完主键索引让主键自动递增(alter table user modify id int(11) unsigned not null auto_increment;)

8.4 全文索引

alter table user add fulltext(字段名);

# 创建一个字段
mysql> alter table user add column contents text;
Query OK, 9 rows affected (0.06 sec)
Records: 9  Duplicates: 0  Warnings: 0
?
#给字段添加全文索引
mysql> alter table user add fulltext(contents);
Query OK, 9 rows affected (0.10 sec)
Records: 9  Duplicates: 0  Warnings: 0
?
#删除全文索引
mysql> alter table user drop index contents;
Query OK, 9 rows affected (0.02 sec)
Records: 9  Duplicates: 0  Warnings: 0

  

8.5 创建表的时候直接声明索引

create table test(
    id int(11) unsigned not null,
    username varchar(20) not null,
    password char(32) not null,
    content text,
    primary key(id),
    index pw(password),
    unique un_name(username),
    fulltext quanwen(content)
)engine=myisam default charset=utf8;

  

9.语句的操作(增删改查)

9.1 增

1)insert into 表名 values(值1,值2,值3,值4)
mysql> insert into users values(1,"zhangsan",18);
Query OK, 1 row affected (0.08 sec)

  

2) insert into 表名(字段1,字段2,字段3,字段4)

values(值1,值2,值3,值4);

mysql> insert into users(name,age) values("lishi",19);
Query OK, 1 row affected (0.14 sec)

  

3)批量插入
mysql> insert into users(id,name,age) values(3,"wangwu",20),(4,"zhaolou",21),(5,"tianqi",22);
Query OK, 3 rows affected (0.21 sec)
Records: 3  Duplicates: 0  Warnings: 0

  

9.2 查

1)基础重新

select * from 表名;

mysql> select * from money;
+----+--------------+------------+----------+-----+-----+
| id | username     | balance    | province | age | sex |
+----+--------------+------------+----------+-----+-----+
|  1 | 薛之谦       |   88888.12 | 上海     |  30 |   0 |
|  2 | 苍老师       |  852223.12 | 日本     |  32 |   1 |
|  3 | 奶茶妹妹     |   66666.66 | 北京     |  30 |   1 |
|  4 | 东哥         |  888999.69 | 北京     |  31 |   1 |
|  5 | 马云         | 1000000.00 | 杭州     |  35 |   0 |
+----+--------------+------------+----------+-----+-----+
5 rows in set (0.00 sec)

  

2)指定字段查询
mysql> select id,username,balance from money;
+----+--------------+------------+
| id | username     | balance    |
+----+--------------+------------+
|  1 | 薛之谦       |   88888.12 |
|  2 | 苍老师       |  852223.12 |
|  3 | 奶茶妹妹     |   66666.66 |
|  4 | 东哥         |  888999.69 |
|  5 | 马云         | 1000000.00 |
+----+--------------+------------+
5 rows in set (0.00 sec)

  

3)查询单个字段(无重复)
mysql> select distinct age from money;
+-----+
| age |
+-----+
|  30 |
|  32 |
|  31 |
|  35 |
+-----+
4 rows in set (0.05 sec)

  

注意:下面这种方式(不重复失效)

mysql> select distinct age,id,username from money;
+-----+----+--------------+
| age | id | username     |
+-----+----+--------------+
|  30 |  1 | 薛之谦       |
|  32 |  2 | 苍老师       |
|  30 |  3 | 奶茶妹妹     |
|  31 |  4 | 东哥         |
|  35 |  5 | 马云         |
+-----+----+--------------+
5 rows in set (0.00 sec)

  

4)条件查询
mysql> select * from money where age=30;
+----+--------------+----------+----------+-----+-----+
| id | username     | balance  | province | age | sex |
+----+--------------+----------+----------+-----+-----+
|  1 | 薛之谦       | 88888.12 | 上海     |  30 |   0 |
|  3 | 奶茶妹妹     | 66666.66 | 北京     |  30 |   1 |
+----+--------------+----------+----------+-----+-----+
2 rows in set (0.00 sec)

  

where 条件

技术分享图片

 

5)结果集排序(order by)
  • desc 倒序排列
  • asc 正序排列
mysql> select id,username,balance,province from money order by balance desc;
+----+--------------+------------+----------+
| id | username     | balance    | province |
+----+--------------+------------+----------+
|  5 | 马云         | 1000000.00 | 杭州     |
|  4 | 东哥         |  888999.69 | 北京     |
|  2 | 苍老师       |  852223.12 | 日本     |
|  1 | 薛之谦       |   88888.12 | 上海     |
|  3 | 奶茶妹妹     |   66666.66 | 北京     |
+----+--------------+------------+----------+
5 rows in set (0.05 sec)

  

6)多字段排序
mysql> select id,username,balance,age,province from money order by balance desc,age asc;
+----+--------------+------------+-----+----------+
| id | username     | balance    | age | province |
+----+--------------+------------+-----+----------+
|  5 | 马云         | 1000000.00 |  35 | 杭州     |
|  4 | 东哥         |  888999.69 |  31 | 北京     |
|  2 | 苍老师       |  852223.12 |  32 | 日本     |
|  1 | 薛之谦       |   88888.12 |  30 | 上海     |
|  3 | 奶茶妹妹     |   66666.66 |  30 | 北京     |
+----+--------------+------------+-----+----------+
5 rows in set (0.00 sec)

  

7)限制结果集(limit)
mysql> select id,username,balance,age,province from money order by balance desc,age asc limit 3;
+----+-----------+------------+-----+----------+
| id | username  | balance    | age | province |
+----+-----------+------------+-----+----------+
|  5 | 马云      | 1000000.00 |  35 | 杭州     |
|  4 | 东哥      |  888999.69 |  31 | 北京     |
|  2 | 苍老师    |  852223.12 |  32 | 日本     |
+----+-----------+------------+-----+----------+
3 rows in set (0.00 sec)

  

8)结果集区间选择
mysql> select * from money limit 2,3;
+----+--------------+------------+----------+-----+-----+
| id | username     | balance    | province | age | sex |
+----+--------------+------------+----------+-----+-----+
|  3 | 奶茶妹妹     |   66666.66 | 北京     |  30 |   1 |
|  4 | 东哥         |  888999.69 | 北京     |  31 |   1 |
|  5 | 马云         | 1000000.00 | 杭州     |  35 |   0 |
+----+--------------+------------+----------+-----+-----+
3 rows in set (0.00 sec)

  

9)分组统计

技术分享图片

 


mysql> select count(province),province from money group by province;
+-----------------+----------+
| count(province) | province |
+-----------------+----------+
|               1 | 上海     |
|               2 | 北京     |
|               1 | 日本     |
|               1 | 杭州     |
+-----------------+----------+
4 rows in set (0.06 sec)

  

10)过滤
mysql> select count(province) as result,province from money group by province having result>1;
+--------+----------+
| result | province |
+--------+----------+
|      2 | 北京     |
+--------+----------+
1 row in set (0.01 sec)

  

sql语句使用总结:

select [字段1 as 别名,统计函数()],[字段2] from 表名 where 条件 group by 分组 having 过滤条件 order by 字段 limit 数量or区间

9.3 多表联合查询

1)内连接
mysql> SELECT  u.username as 用户名,  u.id,o.uid, o.goodsname as 商品名称   FROM  user u, order_goods o  where  u.id=o.uid;
+--------------+----+-----+-----------------+
| 用户名       | id | uid | 商品名称        |
+--------------+----+-----+-----------------+
| 刘强东       |  1 |   1 | 抹茶            |
| 抹茶妹妹     |  2 |   2 | 帽子            |
| 云翔兄       |  3 |   3 | 小肚肚          |
| 李晨         |  6 |   6 | 苹果三件套      |
| 岳云鹏       |  9 |   9 | 锤子手机        |
| 张继科       |  8 |   8 | 娃娃            |
+--------------+----+-----+-----------------+
6 rows in set (0.00 sec)
#select 表1.字段[as 别名],表n.字段 from 表1 inner join 表n on 条件;
mysql> select user.id as 用户id,user.username as 用户名,order_goods.goodsname as 商品名称 from user inner join order_goods on user.id=order_goods.uid;
+----------+--------------+-----------------+
| 用户id   | 用户名       | 商品名称        |
+----------+--------------+-----------------+
|        1 | 刘强东       | 抹茶            |
|        2 | 抹茶妹妹     | 帽子            |
|        3 | 云翔兄       | 小肚肚          |
|        6 | 李晨         | 苹果三件套      |
|        9 | 岳云鹏       | 锤子手机        |
|        8 | 张继科       | 娃娃            |
+----------+--------------+-----------------+
6 rows in set (0.00 sec)

  

2)外链接
#select 表1.字段[as 别名],表n.字段 from 表1 left join 表n on 条件;
select 表1.字段[as 别名],表n.字段 from 表1 left join 表n on 条件;
mysql> select user.username as 用户名,order_goods.goodsname as 商品名称 from user left join order_goods on user.id=order_goods.uuid;
+--------------+-----------------+
| 用户名       | 商品名称        |
+--------------+-----------------+
| 刘强东       | 抹茶            |
| 抹茶妹妹     | 帽子            |
| 云翔兄       | 小肚肚          |
| 李晨         | 苹果三件套      |
| 岳云鹏       | 锤子手机        |
| 张继科       | 娃娃            |
| 范冰冰       | NULL            |
| 景甜         | NULL            |
| 周立泼       | NULL            |
+--------------+-----------------+
9 rows in set (0.00 sec)
#select 表1.字段[as 别名],表n.字段 from 表1 right join 表n on 条件;
mysql> select user.username as 用户名,order_goods.goodsname as 商品名称 from user right join order_goods on user.id=order_goods.uid;
+--------------+-----------------+
| 用户名       | 商品名称        |
+--------------+-----------------+
| 刘强东       | 抹茶            |
| 抹茶妹妹     | 帽子            |
| 云翔兄       | 小肚肚          |
| 李晨         | 苹果三件套      |
| 岳云鹏       | 锤子手机        |
| 张继科       | 娃娃            |
+--------------+-----------------+
6 rows in set (0.00 sec)

  

3)子查询
#查看那些用户买了东西
mysql> select uid from order_goods;
+-----+
| uid |
+-----+
|   1 |
|   2 |
|   3 |
|   6 |
|   9 |
|   8 |
+-----+
6 rows in set (0.00 sec)
#查看买了东西的用户的信息
mysql> select username from user where id in(1,2,3,6,9,8);
+--------------+
| username     |
+--------------+
| 刘强东       |
| 抹茶妹妹     |
| 云翔兄       |
| 李晨         |
| 张继科       |
| 岳云鹏       |
+--------------+
6 rows in set (0.05 sec)
#相当于上面两个
mysql> select username as ‘用户名‘ from user where id in(select uid from order_goods);
+--------------+
| 用户名       |
+--------------+
| 刘强东       |
| 抹茶妹妹     |
| 云翔兄       |
| 李晨         |
| 岳云鹏       |
| 张继科       |
+--------------+
6 rows in set (0.05 sec)

  

4)联合查询
#union
mysql> select id from user union select uid from order_goods;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  6 |
|  7 |
|  8 |
|  9 |
| 10 |
+----+
9 rows in set (0.05 sec)
#union all
mysql> select id from user union all select uid from order_goods;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  6 |
|  7 |
|  8 |
|  9 |
| 10 |
|  1 |
|  2 |
|  3 |
|  6 |
|  9 |
|  8 |
+----+
15 rows in set (0.00 sec)

  

9.4 数据更新

update 表名 set 字段1=值1,字段2=值2,字段3=值3 where 条件;

update money set balance=100,sex=1 where id>2 and id<5;
update money set balance=balance-100  where id>2 and id<5;

  

两个表的数据同时更新

mysql> update money m,user u set m.balance=m.balance*u.age where m.id=u.id;
Query OK, 9 rows affected (0.00 sec)
Rows matched: 9  Changed: 9  Warnings: 0

  

9.5 删除

delete from 表名 where 条件;

#不加条件表示清空数据表
mysql> delete from test;
Query OK, 2 rows affected (0.00 sec)
#加条件指定删除
mysql> delete from money where username=‘王思聪‘ or id=10;
Query OK, 0 rows affected (0.00 sec)
#truncase table 表名 (清空表内容)
mysql> truncate table test;
Query OK, 0 rows affected (0.06 sec)

  

注意:

  • 使用delete清空时,再插入数据id从原来的往后累加。

  • 使用truncase清空时,再插入数据id还是从1开始。

10.表结构、表数据的复制

10.1 复制表结构

mysql> create table test1 like test;
Query OK, 0 rows affected (0.10 sec)

  

10.2 复制表数据

mysql> insert into test1 select * from test;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

  

11.mysql视图

如果经常查询这个范围的内容select * from money where id>2 and id<6;?

解决:把这个区间的内容放到临时表中,下一次直接到这个临时表中取数据。

mysql> select * from money where id>2 and id<6;
+----+--------------+------------+----------+-----+-----+
| id | username     | balance    | province | age | sex |
+----+--------------+------------+----------+-----+-----+
|  3 | 奶茶妹妹     |   66666.66 | 北京     |  30 |   1 |
|  4 | 东哥         |  888999.69 | 北京     |  31 |   1 |
|  5 | 马云         | 1000000.00 | 杭州     |  35 |   0 |
+----+--------------+------------+----------+-----+-----+
3 rows in set (0.05 sec)
#创建视图
mysql> create view v_money as select * from money where id>2 and id<6;
Query OK, 0 rows affected (0.07 sec)

#show tables查看多了一个临时表
mysql> show tables;
+----------------------+
| Tables_in_python1806 |
+----------------------+
| money                |
| v_money              |
+----------------------+
2 rows in set (0.00 sec)
#使用视图
mysql> select * from v_money;
+----+--------------+------------+----------+-----+-----+
| id | username     | balance    | province | age | sex |
+----+--------------+------------+----------+-----+-----+
|  3 | 奶茶妹妹     |   66666.66 | 北京     |  30 |   1 |
|  4 | 东哥         |  888999.69 | 北京     |  31 |   1 |
|  5 | 马云         | 1000000.00 | 杭州     |  35 |   0 |
+----+--------------+------------+----------+-----+-----+
3 rows in set (0.00 sec)

#删除视图(v_money依托于money存在)
mysql> drop view v_money;
Query OK, 0 rows affected (0.05 sec)

 

 

12 mysql内置函数

12.1 字符串函数

1)concat(s1,s2....sn):把传入的参数连接成一个字符串
mysql> select concat("kangbazi","1806");
+---------------------------+
| concat("kangbazi","1806") |
+---------------------------+
| kangbazi1806              |
+---------------------------+
1 row in set (0.00 sec)

  

2)lower(str):将字符串转换为小写
mysql> select lcase("HELLO WORLD!");
+-----------------------+
| lcase("HELLO WORLD!") |
+-----------------------+
| hello world!          |
+-----------------------+
1 row in set (0.02 sec)

  

3)upper(str):将字符串转换为大写
mysql> select ucase("hello world!");
+-----------------------+
| ucase("hello world!") |
+-----------------------+
| HELLO WORLD!          |
+-----------------------+
1 row in set (0.26 sec)

  

4)ltrim(str):去除字符串左侧空格
mysql> select ltrim("     mysql");
+---------------------+
| ltrim("     mysql") |
+---------------------+
| mysql               |
+---------------------+
1 row in set (0.06 sec)

  

5)rtrim(str):去除字符串右侧空格
mysql> select ltrim("mysql    ");
+--------------------+
| ltrim("mysql    ") |
+--------------------+
| mysql              |
+--------------------+
1 row in set (0.00 sec)

  

6)repeat(str,count):返回str重复count次得到的新字符串
mysql> select repeat(‘linux‘,3);
+-------------------+
| repeat(‘linux‘,3) |
+-------------------+
| linuxlinuxlinux   |
+-------------------+
1 row in set (0.04 sec)

  

7)replace(str,a,b):在字符串str中用字符串b替换所有的字符串a
mysql> select replace("java是世界上最好的语言","java","python");
+------------------------------------------------------------+
| replace("java是世界上最好的语言","java","python")          |
+------------------------------------------------------------+
| python是世界上最好的语言                                   |
+------------------------------------------------------------+
1 row in set (0.04 sec)

  

8)substr(str,a,b):从str下标a处开始截取b个字符
mysql> select substr("python is so coole",2,4);
+----------------------------------+
| substr("python is so coole",2,4) |
+----------------------------------+
| ytho                             |
+----------------------------------+
1 row in set (0.00 sec)

  

12.2 数学函数

1)abs(x):返回绝对值

mysql> select abs(-5);
+---------+
| abs(-5) |
+---------+
|       5 |
+---------+
1 row in set (0.00 sec)

  

2)ceiling(x):返回大于x的最小整数
mysql> select ceiling(18.1);
+---------------+
| ceiling(18.1) |
+---------------+
|            19 |
+---------------+
1 row in set (0.00 sec)

  

3)floor(x):返回小于x的最大整数
mysql> select floor(18.9);
+-------------+
| floor(18.9) |
+-------------+
|          18 |
+-------------+
1 row in set (0.04 sec)

  

4)rand():返回0-1之间的随机数
mysql> select rand();
+--------------------+
| rand()             |
+--------------------+
| 0.9154018088387342 |
+--------------------+
1 row in set (0.01 sec)

  

5)round(x,y):返回参数x的y位小数的四舍五入结果
mysql> select round(3.1415926,2);
+--------------------+
| round(3.1415926,2) |
+--------------------+
|               3.14 |
+--------------------+
1 row in set (0.00 sec)

  

12.3 日期函数

1)now():返回当前时间
mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2018-09-09 08:15:39 |
+---------------------+
1 row in set (0.06 sec)

  

2)week():返回是第几周
mysql> select week(now());
+-------------+
| week(now()) |
+-------------+
|          36 |
+-------------+
1 row in set (0.02 sec)

  

3)year():返回是哪一年
mysql> select year(now());
+-------------+
| year(now()) |
+-------------+
|        2018 |
+-------------+
1 row in set (0.06 sec)

  

4)datediff(date1,date2):返回两个日期的差额
mysql> select datediff("2018-9-5","2019-10-5");
+----------------------------------+
| datediff("2018-9-5","2019-10-5") |
+----------------------------------+
|                             -395 |
+----------------------------------+
1 row in set (0.02 sec)

  

13 预处理(防sql注入)

mysql> select * from money where id>2;
+----+--------------+------------+----------+-----+-----+
| id | username     | balance    | province | age | sex |
+----+--------------+------------+----------+-----+-----+
|  3 | 奶茶妹妹     |   66666.66 | 北京     |  30 |   1 |
|  4 | 东哥         |  888999.69 | 北京     |  31 |   1 |
|  5 | 马云         | 1000000.00 | 杭州     |  35 |   0 |
+----+--------------+------------+----------+-----+-----+
3 rows in set (0.09 sec)

mysql> select * from money where id>4;
+----+----------+------------+----------+-----+-----+
| id | username | balance    | province | age | sex |
+----+----------+------------+----------+-----+-----+
|  5 | 马云     | 1000000.00 | 杭州     |  35 |   0 |
+----+----------+------------+----------+-----+-----+
1 row in set (0.01 sec)

  

select * from money where id>?;
mysql> prepare yuchuli from "select * from money where id>?";
Query OK, 0 rows affected (0.07 sec)
Statement prepared
?
mysql> set @a=2;
Query OK, 0 rows affected (0.00 sec)
?
mysql> execute yuchuli using @a;
+----+--------------+------------+----------+-----+-----+
| id | username     | balance    | province | age | sex |
+----+--------------+------------+----------+-----+-----+
|  3 | 奶茶妹妹     |   66666.66 | 北京     |  30 |   1 |
|  4 | 东哥         |  888999.69 | 北京     |  31 |   1 |
|  5 | 马云         | 1000000.00 | 杭州     |  35 |   0 |
+----+--------------+------------+----------+-----+-----+
3 rows in set (0.00 sec)

  

mysql数据库基本操作

标签:var   整数   balance   命令   mysql   ascii码   失效   3.5   表结构   

原文地址:https://www.cnblogs.com/huanggaoyu/p/10056432.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!