Mariadb

数据库是一堆表的集合

主键

外键

索引

安装:

Centos7

[root@host]# mysqladmin -u root password "new_password";     #创建新密码
MariaDB [(none)]> show databases;   #显示数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> use mysql;    #使用mysql数据库
MariaDB [mysql]> show tables;   #显示mysql数据库中的所有表
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
......
MariaDB [mysql]> desc user;     #查看user表结构
+------------------------+-----------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+---------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(16) | NO | PRI | | |
| Password | char(41) | NO | | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
......
MariaDB [mysql]> select * from user\G    #查看user表中所有的数据
*************************** 1. row ***************************
Host: localhost
User: root
Password:
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
........
MariaDB [mysql]> select User from user;     #从user表中显示所有的User(用户)
+------+
| User |
+------+
| root |
| root |
| |
| whoi |
| root |
......
MariaDB [mysql]> GRANT ALL ON test.* TO 'tom'@'localhost' identified by '';   #在本机创建tom用户并赋予所有权限在test库的所有表上,密码为123
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show grants for 'tom'@'localhost';   # 显示用户tom的权限
+------------------------------------------------------------------------------------------------------------+
| Grants for tom@localhost |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'localhost' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |
| GRANT ALL PRIVILEGES ON `test`.* TO 'tom'@'localhost' |
+------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
MariaDB [(none)]> create database db;   #创建数据库db
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show create database db;   # 显示创建的数据库db,默认创建的字符集是latin,只能输入英文
+----------+---------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------+
| db | CREATE DATABASE `db` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> create database db_utf charset utf8;  #创建字符集为utf8的数据库,可以输入中文
Query OK, 1 row affected (0.00 sec)

MySQL数据类型

常用数值

SMALLINT

INT

FLOAT

日期/时间

字符串

CHAR  定长字符串

VARCHAR  变长字符串

TEXT  长文本数据

MySQL常用命令

创建数据表

MariaDB [db]> create table student(            # 创建student表
id int auto_increment,
name char() not null,
age int not null,
register_date date not null,
primary key (id));
Query OK, 0 rows affected (0.02 sec)
MariaDB [db]> show tables;
+--------------+
| Tables_in_db |
+--------------+
| student |
+--------------+
1 row in set (0.00 sec)
MariaDB [db]> desc student;
+---------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(32) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
| register_date | date | NO | | NULL | |
+---------------+----------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

插入数据

MariaDB [db]> insert into student (name,age,register_date) values ('zhangsan',12,'2017-11-11');
Query OK, 1 row affected (0.01 sec)
MariaDB [db]> select * from student;
+----+----------+-----+---------------+
| id | name | age | register_date |
+----+----------+-----+---------------+
| 1 | zhangsan | 12 | 2017-11-11 |
+----+----------+-----+---------------+
1 row in set (0.00 sec)

连续插入几条数据

riaDB [db]> select * from student;
+----+----------+-----+---------------+
| id | name | age | register_date |
+----+----------+-----+---------------+
| 1 | zhangsan | 12 | 2017-11-11 |
| 2 | lisi | 4 | 2017-11-11 |
| 3 | wangwu | 5 | 2017-11-11 |
| 4 | zhaoliu | 8 | 2017-11-11 |
| 5 | hongqi | 28 | 2017-11-11 |
| 6 | hongqi | 28 | 2016-04-11 |
+----+----------+-----+---------------+
6 rows in set (0.00 sec)

查询数据

MariaDB [db]> select * from student limit ;  #查询前两条数据
+----+----------+-----+---------------+
| id | name | age | register_date |
+----+----------+-----+---------------+
| 1 | zhangsan | 12 | 2017-11-11 |
| 2 | lisi | 4 | 2017-11-11 |
+----+----------+-----+---------------+
2 rows in set (0.00 sec)
MariaDB [db]> select * from student limit  offset ;  #偏移一位后查询两条
+----+--------+-----+---------------+
| id | name | age | register_date |
+----+--------+-----+---------------+
| 2 | lisi | 4 | 2017-11-11 |
| 3 | wangwu | 5 | 2017-11-11 |
+----+--------+-----+---------------+
2 rows in set (0.00 sec)
MariaDB [db]> select * from student limit  offset ;  #偏移两位后查询两条
+----+---------+-----+---------------+
| id | name | age | register_date |
+----+---------+-----+---------------+
| 3 | wangwu | 5 | 2017-11-11 |
| 4 | zhaoliu | 8 | 2017-11-11 |
+----+---------+-----+---------------+
2 rows in set (0.00 sec)

where子句

MariaDB [db]> select * from student where id > ;
+----+---------+-----+---------------+
| id | name | age | register_date |
+----+---------+-----+---------------+
| 4 | zhaoliu | 8 | 2017-11-11 |
| 5 | hongqi | 28 | 2017-11-11 |
+----+---------+-----+---------------+
2 rows in set (0.00 sec)
MariaDB [db]> select * from student where id >  and age < ;
+----+---------+-----+---------------+
| id | name | age | register_date |
+----+---------+-----+---------------+
| 4 | zhaoliu | 8 | 2017-11-11 |
+----+---------+-----+---------------+
1 row in set (0.00 sec)
MariaDB [db]> select * from student where id=;
+----+--------+-----+---------------+
| id | name | age | register_date |
+----+--------+-----+---------------+
| 5 | hongqi | 28 | 2017-11-11 |
+----+--------+-----+---------------+
1 row in set (0.00 sec)
MariaDB [db]> select * from student where register_date like "%";  #只取2016年的
+----+--------+-----+---------------+
| id | name | age | register_date |
+----+--------+-----+---------------+
| 6 | hongqi | 28 | 2016-04-11 |
+----+--------+-----+---------------+
1 row in set (0.00 sec)

UPDATE语句

MariaDB [db]> update student set name="wangjiu",age=11 where id=3;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
|  3 | wangjiu  |  11 | 2017-11-11    |

DELETE语句

MariaDB [db]> delete from student where name="wangjiu";
Query OK, 1 row affected (0.01 sec)

排序

MariaDB [db]> select * from student order by id desc;  #降序排列
+----+----------+-----+---------------+
| id | name | age | register_date |
+----+----------+-----+---------------+
| 6 | hongqi | 28 | 2016-04-11 |
| 5 | hongqi | 28 | 2017-11-11 |
| 4 | zhaoliu | 8 | 2017-11-11 |
| 2 | lisi | 4 | 2017-11-11 |
| 1 | zhangsan | 12 | 2017-11-11 |
+----+----------+-----+---------------+
5 rows in set (0.00 sec)

做两条更改

MariaDB [db]> update student set register_date="2017-08-20" where id=2;
MariaDB [db]> update student set register_date="2013-01-20" where id=3;
MariaDB [db]> select * from student order by register_date;
+----+----------+-----+---------------+
| id | name | age | register_date |
+----+----------+-----+---------------+
| 6 | hongqi | 28 | 2016-04-11 |
| 2 | lisi | 4 | 2017-08-20 |
| 1 | zhangsan | 12 | 2017-11-11 |
| 4 | zhaoliu | 8 | 2017-11-11 |
| 5 | hongqi | 28 | 2017-11-11 |
+----+----------+-----+---------------+
5 rows in set (0.00 sec)

GROUP BY 分组统计

MariaDB [db]> insert into student (name,age,register_date) values ('zhaoliu',4,'2015-12-20');
Query OK, 1 row affected (0.01 sec)
MariaDB [db]> select * from student;
+----+----------+-----+---------------+
| id | name | age | register_date |
+----+----------+-----+---------------+
| 1 | zhangsan | 12 | 2017-11-11 |
| 2 | lisi | 4 | 2017-08-20 |
| 4 | zhaoliu | 8 | 2017-11-11 |
| 5 | hongqi | 28 | 2017-11-11 |
| 6 | hongqi | 28 | 2016-04-11 |
| 7 | zhaoliu | 4 | 2015-12-20 |
+----+----------+-----+---------------+
6 rows in set (0.00 sec)
MariaDB [db]> select name,count(*) from student group by name;
+----------+----------+
| name | count(*) |
+----------+----------+
| hongqi | 2 |
| lisi | 1 |
| zhangsan | 1 |
| zhaoliu | 2 |
+----------+----------+
4 rows in set (0.00 sec)
MariaDB [db]> select register_date,count(*) from student group by register_date;
+---------------+----------+
| register_date | count(*) |
+---------------+----------+
| 2015-12-20 | 1 |
| 2016-04-11 | 1 |
| 2017-08-20 | 1 |
| 2017-11-11 | 3 |
+---------------+----------+
4 rows in set (0.01 sec)
MariaDB [db]> select register_date,count(*) as stu_num from student group by register_date; # 用as重命名
+---------------+---------+
| register_date | stu_num |
+---------------+---------+
| 2015-12-20 | 1 |
| 2016-04-11 | 1 |
| 2017-08-20 | 1 |
| 2017-11-11 | 3 |
+---------------+---------+
4 rows in set (0.00 sec)
MariaDB [db]> select name,sum(age) from student group by name;  # 统计字段之和
+----------+----------+
| name | sum(age) |
+----------+----------+
| hongqi | 56 |
| lisi | 4 |
| zhangsan | 12 |
| zhaoliu | 12 |
+----------+----------+
4 rows in set (0.00 sec)
MariaDB [db]> select name,sum(age) from student group by name with rollup;  #加起来的总和
+----------+----------+
| name | sum(age) |
+----------+----------+
| hongqi | 56 |
| lisi | 4 |
| zhangsan | 12 |
| zhaoliu | 12 |
| NULL | 84 |
+----------+----------+
5 rows in set (0.00 sec)

把NULL名改为Total Age

MariaDB [db]> select coalesce(name,'Total Age'),sum(age) from student group by name with rollup;
+----------------------------+----------+
| coalesce(name,'Total Age') | sum(age) |
+----------------------------+----------+
| hongqi | 56 |
| lisi | 4 |
| zhangsan | 12 |
| zhaoliu | 12 |
| Total Age | 84 |
+----------------------------+----------+
5 rows in set (0.00 sec)

修改数据表名或数据表字段

MariaDB [db]> alter table student add sex enum('M','F');
Query OK, 6 rows affected (0.02 sec)
MariaDB [db]> insert into student (name,age,register_date,sex) values('wangsen',22,'2012-04-22','M');
Query OK, 1 row affected (0.00 sec)
MariaDB [db]> select * from student;
+----+----------+-----+---------------+------+
| id | name | age | register_date | sex |
+----+----------+-----+---------------+------+
| 1 | zhangsan | 12 | 2017-11-11 | NULL |
| 2 | lisi | 4 | 2017-08-20 | NULL |
| 4 | zhaoliu | 8 | 2017-11-11 | NULL |
| 5 | hongqi | 28 | 2017-11-11 | NULL |
| 6 | hongqi | 28 | 2016-04-11 | NULL |
| 7 | zhaoliu | 4 | 2015-12-20 | NULL |
| 8 | wangsen | 22 | 2012-04-22 | M |
+----+----------+-----+---------------+------+
7 rows in set (0.00 sec)
MariaDB [db]> alter table student drop age;
+----+----------+---------------+------+
| id | name | register_date | sex |
+----+----------+---------------+------+
| 1 | zhangsan | 2017-11-11 | NULL |
| 2 | lisi | 2017-08-20 | NULL |
| 4 | zhaoliu | 2017-11-11 | NULL |
| 5 | hongqi | 2017-11-11 | NULL |
| 6 | hongqi | 2016-04-11 | NULL |
| 7 | zhaoliu | 2015-12-20 | NULL |
| 8 | wangsen | 2012-04-22 | M |
+----+----------+---------------+------+
7 rows in set (0.00 sec)
MariaDB [db]> desc student;
+---------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(32) | NO | | NULL | |
| register_date | date | NO | | NULL | |
| sex | enum('M','F') | YES | | NULL | |
+---------------+---------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
MariaDB [db]> alter table student modify sex enum('F','M') not null;  #把sex字段改为不能为空
MariaDB [db]> desc student;
+---------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(32) | NO | | NULL | |
| register_date | date | NO | | NULL | |
| sex | enum('F','M') | NO | | NULL | |
+---------------+---------------+------+-----+---------+----------------+

更改的另一种语法:change

MariaDB [db]> alter table student change sex gender char(32) not null default 'X';
MariaDB [db]> select * from student;
+----+----------+---------------+--------+
| id | name | register_date | gender |
+----+----------+---------------+--------+
| 1 | zhangsan | 2017-11-11 | |
| 2 | lisi | 2017-08-20 | |
| 4 | zhaoliu | 2017-11-11 | |
| 5 | hongqi | 2017-11-11 | |
| 6 | hongqi | 2016-04-11 | |
| 7 | zhaoliu | 2015-12-20 | |
| 8 | wangsen | 2012-04-22 | M |
+----+----------+---------------+--------+
MariaDB [db]> desc student;
+---------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(32) | NO | | NULL | |
| register_date | date | NO | | NULL | |
| gender | char(32) | NO | | X | |
+---------------+----------+------+-----+---------+----------------+

外键

特殊的索引,用于关联两个表,只能是指定内容

联合查询(left join,right join,inner join,full join)

MariaDB [db]> create table A (a int not null);

MariaDB [db]> create table B (b int not null);

MariaDB [db]> insert into A (a) values(1);

MariaDB [db]> insert into A (a) values(2);

MariaDB [db]> insert into A (a) values(3);

MariaDB [db]> insert into A (a) values(4);

MariaDB [db]> insert into B(b) values(3);

MariaDB [db]> insert into B(b) values(4);

MariaDB [db]> insert into B(b) values(5);

MariaDB [db]> insert into B(b) values(6);

MariaDB [db]> insert into B(b) values(7);
MariaDB [db]> select * from A;
+---+
| a |
+---+
| 1 |
| 2 |
| 3 |
| 4 |
+---+
4 rows in set (0.01 sec) MariaDB [db]> select * from B;
+---+
| b |
+---+
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
+---+
5 rows in set (0.00 sec)

交集(inner join)

MariaDB [db]> select * from A inner join B on A.a = B.b;   # 常用
+---+---+
| a | b |
+---+---+
| 3 | 3 |
| 4 | 4 |
+---+---+

MariaDB [db]> select A.*,B.* from A,B where A.a =B.b;
+---+---+
| a | b |
+---+---+
| 3 | 3 |
| 4 | 4 |
+---+---+

差集(Left join,right join)

MariaDB [db]> select * from A left join B on A.a = B.b;
+---+------+
| a | b |
+---+------+
| 3 | 3 |
| 4 | 4 |
| 1 | NULL |
| 2 | NULL |
MariaDB [db]> select * from A right join B on A.a = B.b;
+------+---+
| a | b |
+------+---+
| 3 | 3 |
| 4 | 4 |
| NULL | 5 |
| NULL | 6 |
| NULL | 7 |
+------+---+

并集(full join),但是mysql不支持

MariaDB [db]> select * from A left join B on A.a = B.b union select * from A right join B on A.a=B.b;
+------+------+
| a | b |
+------+------+
| 3 | 3 |
| 4 | 4 |
| 1 | NULL |
| 2 | NULL |
| NULL | 5 |
| NULL | 6 |
| NULL | 7 |
+------+------+
7 rows in set (0.00 sec)

事物

原子性

稳定性

隔离性

可靠性

索引

MariaDB [db]> select * from student;
+----+----------+---------------+--------+
| id | name | register_date | gender |
+----+----------+---------------+--------+
| 1 | zhangsan | 2017-11-11 | |
| 2 | lisi | 2017-08-20 | |
| 4 | zhaoliu | 2017-11-11 | |
| 5 | hongqi | 2017-11-11 | |
| 6 | hongqi | 2016-04-11 | |
| 7 | zhaoliu | 2015-12-20 | |
| 8 | wangsen | 2012-04-22 | M |
+----+----------+---------------+--------+
MariaDB [db]> create index index_name on student(name); # 创建索引
MariaDB [db]> show index from student;
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student | 0 | PRIMARY | 1 | id | A | 7 | NULL | NULL | | BTREE | | |
| student | 1 | index_name | 1 | name | A | 7 | NULL | NULL | | BTREE | | |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
MariaDB [db]> drop index index_name on student; # 删除索引

唯一索引

索引列的值必须唯一,但允许有空值。如果是组合索引,则列值得组合必须唯一

python操作mysql

python-mysqldb

yum install MySQL-python   # 3.0不能用

pymysql

pip3 install pymysql  # 适用于3.0版本

SQLAlchemy

SQLALchemy是python下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作。将对象转换成SQL,然后使用数据API执行SQL并获取执行结果

pip3 install SQLAlchemy

ORM介绍

对象关系映射,通过orm将编程语言模型和数据库的关系模型建立映射关系,这样在使用编程语言对数据库进行操作的时候可以直接使用编程语言的对象模型进行操作就可以了,而不用直接使用sql语言

优点:

1.隐藏数据访问细节,封闭通用数据库交互,完全不用考虑SQL语句

2.构造数据库结构更简单方便

MySQL,sqlalchemy的更多相关文章

  1. flask连接数据库mysql+SQLAlchemy

    使用flask框架链接2种数据库 ----------db.py # -*- coding: utf-8 -*- # Flask hello world from flask import Flask ...

  2. sqlalchemy精华版

    上一篇主要粗略讲了Flask+mysql+sqlalchemy的使用,这次精讲下sqlalchemy的用法,话不多说,上代码. ----------sqlalchemy_test.py # -*- c ...

  3. Flask SQLAlchemy & model

    Flask-SQLAlchemy Flask-SQLAlchemy库让flask更方便的使用SQLALchemy,是一个强大的关系形数据库框架,既可以使用orm方式操作数据库,也可以使用原始的SQL命 ...

  4. "Mysql has gone away"的几种可能

    现象: 在本地利用Flask自带的WSGI服务进行调试没有问题后,通过Gunicorn进行部署. 但是在一晚上没有访问之后,第二天再次访问会出现500(Internal error). 原因: 通过追 ...

  5. Python的平凡之路(18)

    一.JS 正则部分 test   - 判断字符串是否符合规定的正则rep = /\d+/;rep.test("asdfoiklfasdf89asdfasdf")# truerep ...

  6. 怎样将Emoj表情插入mysql5.6数据库__python+mysqldb

    废话不多说,相信看到这里的看客已经看过非常多配置文件的设置方法.可是问题还是没有解决.本文就具体记录一下我的解决方法吧. 我的环境:mysql5.6+python2.7.3+MySQLdb1.2.4 ...

  7. GitHub awesome Resource

    各种Awesome技术资源的资源聚合: https://github.com/sindresorhus/awesome Contents Platforms Programming Languages ...

  8. python-day18 JS正则,组件BootStrap、EasyUI、JQueryUI1,插件轮播,django

    @第一节课程内容 js正则 登录注册验证 @text-判断字符串是否符合规定的正则 @exec-获取匹配的数据 bootstrap(组件) –css –js 学习bootstrap规则 jQueryu ...

  9. python学习笔记_week18

    note 1.JS 正则 test - 判断字符串是否符合规定的正则 rep = /\d+/; rep.test("asdfoiklfasdf89asdfasdf") # true ...

随机推荐

  1. 有关Servlet的生命周期说法正确的有( )。

    A.Servlet的生命周期由Servlet实例控制 B.init()方法在创建完Servlet实例后对其进行初始化,传递的参数为实现ServletContext接口的对象 C.service()方法 ...

  2. Kivy: 用于NUI开发的跨平台Python框架

    http://top.jobbole.com/9915/ http://kivy.org/

  3. hdu 1253:胜利大逃亡(基础广搜BFS)

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  4. Mongodb默认开启与关闭

    默认启动:   $ ./mongodb   默认数据保存路径:/data/db/ 默认端口:27017   修改默认路径:   --dbpath $ ./mongdb --dbpath /mongod ...

  5. 数据库为什么要用B+树结构--MySQL索引结构的实现(转)

    B+树在数据库中的应用 { 为什么使用B+树?言简意赅,就是因为: 1.文件很大,不可能全部存储在内存中,故要存储到磁盘上 2.索引的结构组织要尽量减少查找过程中磁盘I/O的存取次数(为什么使用B-/ ...

  6. vs 代码自动对其(注释,等号...)

    插件:Code alignment  下载地址

  7. 【BZOJ2067】[Poi2004]SZN 二分+树上贪心

    [BZOJ2067][Poi2004]SZN Description String-Toys joint-stock 公司需要你帮他们解决一个问题. 他们想制造一个没有环的连通图模型. 每个图都是由一 ...

  8. 【BZOJ5071】[Lydsy十月月赛]小A的数字 发现性质

    [BZOJ5071][Lydsy十月月赛]小A的数字 题解:一般遇到这种奇奇怪怪的操作,常用的套路是将原序列差分一下,或者求个前缀和什么的.本题就是直接对原序列求前缀和,然后发现一次操作相当于交换两个 ...

  9. ng2-file-upload上传附件同时传参

    由于业务需要,需要的场景是发某条公告的时候能够上传附件,不只是图片,图片的话可以直接用base64传给后台,但上传附件这个就不能这样干了, 与此同时,每条公告都有一个对应的唯一标识id, 附件以文件流 ...

  10. Windows下使用Gflags检查内存越界

    环境:windows xp. vs2005 Gflags可用于查找内存越界的问题. 访问一块申请的内存时,当访问的地址超过申请的范围时,就发生了内存越界的问题. 编写测试程序MemoryOverflo ...