zoukankan      html  css  js  c++  java
  • MySQL,sqlalchemy

    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 userG    #查看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 '123';   #在本机创建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(32) 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 2;  #查询前两条数据
    +----+----------+-----+---------------+
    | 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 2 offset 1;  #偏移一位后查询两条
    +----+--------+-----+---------------+
    | 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 2 offset 2;  #偏移两位后查询两条
    +----+---------+-----+---------------+
    | 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 > 3;
    +----+---------+-----+---------------+
    | 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 > 3 and age < 20;
    +----+---------+-----+---------------+
    | 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=5;
    +----+--------+-----+---------------+
    | 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%";  #只取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)
    View Code
    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)
    View Code
    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    |                |
    +---------------+---------------+------+-----+---------+----------------+
    View Code

    更改的另一种语法: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      |
    +----+----------+---------------+--------+
    View Code
    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       |                |
    +---------------+----------+------+-----+---------+----------------+
    View Code

    外键

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

    联合查询(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);
    View Code
    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)
    View Code

    交集(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)
    View Code

    事物

    原子性

    稳定性

    隔离性

    可靠性

    索引

    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      |
    +----+----------+---------------+--------+
    View Code
    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      |         |               |
    +---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    View Code
    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.构造数据库结构更简单方便

     

  • 相关阅读:
    机器学习--强化学习
    机器学习--深度学习
    机器学习--维度灾难
    机器学习--最优化
    机器学习--降维
    机器学习--聚类
    机器学习--模型提升
    Git和gitHub用户名 邮箱
    Git线上操作
    版本控制器:Git
  • 原文地址:https://www.cnblogs.com/Ryans-World/p/7233388.html
Copyright © 2011-2022 走看看