1.支持复杂的sql语句查询python
2.支持事务mysql
1.NQSQL不须要通过SQL层的处理sql
2.可扩展性,由于是键值对的形式因此水平扩展很是容易数据库
事务是逻辑上的一组操做,组成这组操做的各个单元,要不所有成功,要不所有失败,这个特性就是事务安全
解决这个问题: mysql的事务解决这个问题,由于mysql的事务特性,要求这组操做,要不全都成功,要不全都失败,这样就避免了某个操做成功某个操做失败。利于数据的安全bash
1.在执行sql语句以前,咱们要开启事务start transaction; 2.正常执行咱们的sql语句; 3.当sql语句执行完毕,存在2种状况 第一种;所有成功,咱们讲sql语句对数据库形成的影响提交到数据库中,commit 第二种;某些sql语句失败,咱们执行rollback(回滚),将对数据库的操做赶忙撤销函数
net start mysql57fetch
-h host 主机名ui
-u user 用户名编码
-p password 密码
mysql -hlocalhost -uroot -p
注:root超级管理员 能够建立和管理其余的用户,root用户不能够远程登陆
CREATE 建立
DROP 删除
ALTER 修改
SHOW 展现
show databases;
create database 库名;
create database if not exists 库名; 防止建立同名的库出现错误
show create database 库名;
use 库名
select database
drop database 库名
create database 库名 character set utf8;
create database 小杰 character 字符集;
show tables;
mysql> create table if not exists fs(
-> id int unsigned primary key auto_increment,
-> username varchar(20),
-> sex tinyint,
-> age tinyint unsigned,
-> info varchar(100)
-> );
复制代码
desc 表名;
mysql> create table if not exists fs(
-> id int unsigned primary key auto_increment,
-> username varchar(20),
-> sex tinyint,
-> age tinyint unsigned,
-> info varchar(100)
-> );
复制代码
alter table 表名 drop 字段名;
drop table if exists 表名;删除表若是该表存在
insert into 表名 字段名 values(值);
select * from 表名;
show create table 表名(\G);(竖着查看)
rename table 原表名 to 新表名;
alter table 表名 add|change|drop 字段名 (类型);
字段类型
char和varchar的存储长度都为0-255
char的执行效率高于varchar
varchar要比 char更节省存储空间
当char存储的值达不到指定的长度时 则使用空来占位
enum和set区别
enum只能选择其中的一个值
set能够选择多个值 多个值使用逗号来隔开
unsigned 无符号 正数
只能用于数值类型 不容许出现负数 存储长度会扩大一倍
zerofill 零填充
只能用于数值类型 当指定的位数不足的时候 零填充
default 默认值
若是当前字段没有传值 则值为默认值 (不设定默认值 默认为null)
null 和 not null
默认为null 当不插入值则插入的为null,当设置当前字段为 not null的时候
则该字段必须传值
comment 设置当前字段的说明
auto_increment 自增
注意
SQL 语句以分号做为结束
SQL命令 不区分大小写
数据库的切换使用use
\c 撤销当前命令
\G 竖着查看
当遇到在终端中 无论怎样输入命令都不执行 name查看一下左侧 是否为->
在Windows下 不区分库,表名的大小写 可是在Ubuntu下区分
退出数据库的几种方式
\q exit quit
格式:insert into (表名) values(...);
说明:主键列是自动增加,可是在全列插入时须要占位,一般使用0,插入成功之后以实际数据为准
格式:insert into 表名 (字段1,字段2,...) values (...)
格式:insert into 表名 values (...),(...),...
格式:delete from 表名 where 条件;
示例:delete from student where id=4;
注意: !!没有条件是所有删除,慎用;
格式:update 表名 set 字段1=值1,字段2=值2,.... where 条件
示例:update student set age=16 where id = 7;
注意:若是没有条件,是所有字段都修改;
格式:select * from 表名
说明:from 关键字后面是表名,表示数据来源于这张表
select 后面写表中的字段名," * " 表示全部字段名,
在select 后面字段名,可使用as为字段名起别名
这个别名显示在结果集中
若是要查询多个字段,直接使用逗号分隔
复制代码
示例:select name,age from student; select name as a,age from student;
在select后面字段名的前面使用distinct能够消除重复的行
示例:select distinct genger from student
a.语法: select * from 表名 where 条件;
b.比较运算符
等于
大于
小于
大于等于
小于等于
不等于
复制代码
需求:查询id大于8的全部数据
示例:select * from student where id > 8;
and or not
需求;查询id>7的女同窗
示例;select * from student where id>7 and gender=0;
like %:表示任意多个任意字符 :表示任意一个字符 需求:查询姓习的同窗 示例:select * from student where name like "习%"; 示例:select * from student where name like "习";
in:表示在一个非连续的范围内
between..and...:表示在一个连续的范围内
需求:查询编号为8,10,12的学生
示例:select * from student where id in(8,10,12);
需求;查询编号为6-8的学生
示例; select * from student where id between 6 and 8;
注意:null与" "是不一样的
判断空: isnull
判断非空: is not null
需求:查询没有地址的同窗 示例;select * from student where address is null
小括号,not 比较运算符,逻辑运算符 and比or优先级高
为了快速获得统计的数据,提供了5个聚合函数
a. count(*) 表示计算总行数,括号中能够写*和字段名;
b. max(列) 表示求此列的最大值
c. min(列) 表示求此列的最小值
d. sum(列) 表示求此列的和
e. avg(列) 表示求此列的平均值
复制代码
需求:查询学生总数:
示例:select count(*) from student;
需求:查询女生编号的最大值;
示例:select max(id) from student where gender=0;
需求:查询女生编号的最小值;
示例:select min(id) from student where gender=0;
需求:查询全部学生的年龄和
示例:select sum(age) from student;
需求:查询全部学生的年龄平均值
示例:select avg(age) from student;
按照字段分组,表示此字段相同的数据会被放到一个集合中,分组后,只能查询出相同的数据列。对于有差别的数据列,没法显示在结果集中,能够对分组后的数据进行统计,作聚合运算
语法:select 列1,列2,聚合...... from 表名 group by 列1,列2,列3,......
需求:查询男女生总数
示例:select gender,count(*) from student group by gender;
python操做mysql步骤
import pymysql
(1) 连接mysql数据库
db = pymysql.connect(主机名,用户名,密码,数据库名)
(2) 设置字符编码
db.set_charset('utf8')
(3) 建立游标对象
cursor = db.cursor()
(4) 准备sql语句
sql = '...'
(5) 执行sql语句
cursor.execute(sql)
(6) 获取全部结果集
cuesor.fetchall()
(7) 获取一条结果集
cursor.fetchone()
(8) 获取受影响的行数
cursor.rowcount
(9) 关闭数据库连接
db.close()
默认开启事物处理
须要提交或者回滚
import pymysql
db = pymysql.connect('127.0.0.1','root','123456','hz03')
db.set_charset('utf8')
cursor = db.cursor()
try:
sql = 'insert into goods values(null,1,"商品名称",12.1)'
cursor.execute(sql)
db.commit()
except:
db.rollback()
print(cursor.rowcount)
db.close()
复制代码
示例: import pymysql
db = pymysql.connect('localhost','root','abcdef','test')
db.set_charset('utf8')
cursor = db.cursor()
sql = 'select * from hz03'
cursor.execute(sql)
date = cursor.fetchall() print('结果集为',date)
print('受影响的行数为',cursor.rowcount)
db.close()