数据库基础
- 数据库安装
- 数据库的概念
- 数据库的操作
- 数据表的操作
- 数据行的操作
- 作业
默认端口号
3306
安装
1.yum makecache
2.yum clean all
3.wget -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
4.yum -y install mysql-server mysql-client libmysqld-dev
mysql服务器 mysql客户端 依赖库
5.service mysqld start #开启服务
mysql -u root -p
输入密码 #不行
# 修改mysql root 密码
vim /etc/my.cnf
[mysqld]
skip-grant-tables #绕过密码验证
mysql -u root -p
直接回车就登录成功
show databases; #列出所有的数据库
use mysql;
update mysql.user set authentication_string=password("新密码") where user='root'; # 更新密码
update user set plugin="mysql_native_password"; #更新原始密码
flush privileges; #刷新权限
vim /etc/my.cnf
#skip-grant-tables #注释掉
service mysqld restart #重启mysql服务
如果想用 本地终端 连接远程数据库
1.授权
如果遇到以下错误
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql -u root -p
> set global validate_password_policy=0;
> set global validate_password_length=1;
> alter user 'root'@'localhost' identified by '你的密码';
开始授权
grant all on *.* to root@'%' identified by '123456' with grant option;#授权 在任何主机上 用root 123456 登录 可以操作所有的数据库 以及所有的数据表
flush privileges; #刷新权限
数据库的五个基本单位
1.数据库服务器
2.数据库
3.数据表
4.数据字段
5.数据行
数据库的操作
show databases;#列出所有的数据库
use 数据库名字;选中数据库
show tables; #查看数据库有哪些数据表
drop database whpython;
数据表的操作
创建数据表
create table 表名(字段名 字段类型(长度))engine=Innodb default charset=utf8; #mysql没有utf-8 只有utf8
mysql> create table article(id int(11)); #采用默认引擎 默认的字符集
Query OK, 0 rows affected (0.03 sec)
mysql> create table book(id int(11))default charset=utf8; #指定字符集
Query OK, 0 rows affected (0.02 sec)
mysql> create table test(id int(11))engine=innodb default charset=utf8; #指定引擎 指定字符集
Query OK, 0 rows affected (0.04 sec)
查看表结构
desc 表名;
字段名 字段类型 是否为空 是否主键 是否有默认值 其它信息
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
show create table 表名;#查看建表语句
show create table 表名\G #以最佳的阅读体验阅读
engine mysql引擎
charset 字符集
删除数据表
drop table 表名;
数据字段的操作
alert table 表名
修改表字段类型 modify
alter table 表名 modify 字段名 字段类型(长度);
alter table article modify id varchar(50);
mysql> alter table article modify password after name;
mysql> alter table article modify password varchar(64) first;
增加表字段 add
alter table 表 add column 字段名 类型; #column 可加 可不加
alter table article add username varchar(50);
alter table article add username varchar(50) first;
alter table article add username varchar(50) after 字段;
alter table article add column password varchar(50);
增加表字段 调整增加的顺序 first 、after
默认把字段加到末尾处
alter table article add sex int first; #添加字段到最开头的位置
mysql> alter table article add title varchar(64) after username; #添加新字段到指定字段的后边
删除表字段 drop
mysql> alter table article drop column sex;
mysql> alter table article drop age; #column可加 可不加
表字段 改名字 change
mysql> alter table article change username name varchar(30);
alter table article change username name char(30) first;
alter table article change username name varchar(30) after 字段名 ;
总结
- modify 修改表字段类型 、change 表字段改名 、add 添加表字段 后边都可以跟上 first 和 after 字段名;
修改表名
alter table 旧表名 rename 新表名;
类型 、引擎、字符集、索引
类型
- 数值类型: 整型、浮点型
- 字符串类型
- 日期类型
- 符合类型
- 空间类型
数值类型
整型
类型 | 占字节数 | 范围 |
---|---|---|
int | 4 | |
tinyint | 1 | -128~127 |
smallint | 2 | -32768-32767 |
mediumint | 3 | -8388608~8388607 |
bigint | 8 |
浮点型
类型 | 占字节数 | 范围 |
---|---|---|
float(m,d) | 4 | 单精度 m 总个数 d 小数点后边位数 float(10,4)66666.66666 这个总共10位但是小数点后边5位 不满足条件 |
double(m,d) | 8 | 双精度 m 总个数 d 小数点后边位数 |
decimal | 储存为字符串的浮点数 |
使用注意事项
- 如果我们使用int的时候 不希望是负数 unsigned 表示无符号的 只能正数 不能负数
- 性别 无符号整型 一般不存男或者nv 一般用 tinyint 来表示 要么 0 要么1 要么2
- 年龄 无符号整型 smallint 就ok
- 能用数字表示的 不用字符串 为了节约空间
- 银行中 对金额要求比较高 经常用的是 decimal 本质是字符串存储
字符串类型
类型 | 字节数 | 范围 |
---|---|---|
varchar | 0-255 | |
char | 0-255 | |
tinytext | 0-255 | |
text | 0-65535 | |
longtext | 极大文本 | |
tinyblob | 二进制形式的文本数据 比如存图片 视频等 0-255 | |
blob | 0-65535 二进制形式的长文本 | |
longblob | 极大文本 | |
总结
- char 给他分配了30长度 如果实际上字符超出了30长度 那么会将该字符截短 只能存30 如果不足30 用空格补齐
- varchar 给他分配了30长度 超过30长度 还是会截短 如果不足30长度 不用空格补齐
- blob 和 text的区别 区分大小写 text 不区分大小写
时间类型
类型 | 字节数 | 范围 |
---|---|---|
date | 3 | 2020-07-20 |
time | 3 | 16:23:30 |
datetime | 8 | 2020-07-20 16:23:30 |
timestamp | 4 | 自动存储记录修改的时间 |
year | 1 | 年份 |
开发过程中 好多产品用时间戳 代替 日期和时间 这样的好处也是为了节约空间
复合类型 了解
类型 | 说明 | 举例 |
---|---|---|
set | 集合类型 | set(‘num1’,‘num2’,‘num3’) |
enum | 枚举类型 | enum(‘num1’,‘num2’,‘num3’) |
enum 只允许一个集合中取一个值
set 允许 一个集合取多个值
sql语句常见的限定条件
- unsigned 无符号 如果哪个字段的值 必须是正整数 那么需要加上它
- default 设置默认值
- zerofill 也是为了阻止填充负值
- not null 不能为空
create table if not exists numbers(
id int unsigned not null,
username varchar(64) not null,
content text not null,
age tinyint(4) zerofill,
sex tinyint unsigned not null default 1,
create_time datetime not null)engine=innodb default charset=utf8; #没有utf-8 只有utf8
字符集
- ASCII A 65 a 97
- gbk 汉字编码规范 向下兼容 gb2312
- unicode 国际组织定义的 所有语言编码规范 用来跨语言 跨平台 文本之间的转化
- utf8 针对 Unicode的一个可变长度编码
工作过程中使用的字符编码
:
- utf8_general_ci 多国语言不区分大小写
- gbk_general_ci 简体中文 不区分大小写
引擎
show engines; 列出所有的引擎
常用的: innodb myisam memory
innodb和 myisam 引擎的区别:
1.mysql 5.5.5以后默认的引擎是 innodb 之前默认的引擎是 myisam
2.myisam引擎 读效率高
3.innodb 写数据更加的安全
索引 重点
缩小查询的范围 提升查询的速度的
- 普通索引
- 主键索引 这一列不能有重复值
- 唯一索引
- 复合索引
- 全文索引
create table qf2002(id int(11) unsigned not null primary key auto_increment)default charset=utf8;
每个都有一个主键 一般创建表的时候都给他指定了
如果没有建表的时候指定主键
mysql> desc book;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> alter table book add primary key(id); #可以通过这种方式指定某个字段为主键
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc book;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
增删改查
- 增 insert
- 删 delete
- 改 update
- 查 select
增
insert into 表名 values(值1,值2,…值n) #表结构有多少个字段 values 你就要写多少个字段
insert into 表名(字段1,字段2,字段3,字段4) values(值1,值2,值3,值4) 有些字段为空 或者有默认值可以不用写 只需要插入要求的字段就好了
mysql> desc articles;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| age | int(11) | YES | | NULL | |
| password | varchar(64) | YES | | NULL | |
| id | varchar(50) | YES | | NULL | |
| sex | int(11) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| title | varchar(64) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> insert into articles values(18,'123456',1,1,'jiadong','武汉大东哥');
Query OK, 1 row affected (0.00 sec)
mysql> insert into articles(name,password,title,age) values('liangzai','654321','千锋靓仔',19);
Query OK, 1 row affected (0.00 sec)
mysql> select * from articles;
+------+----------+------+------+----------+-----------------+
| age | password | id | sex | name | title |
+------+----------+------+------+----------+-----------------+
| 18 | 123456 | 1 | 1 | jiadong | 武汉大东哥 |
| 19 | 654321 | NULL | NULL | liangzai | 千锋靓仔 |
+------+----------+------+------+----------+-----------------+
2 rows in set (0.00 sec)
以上是插入一条数据
接下来 插入多条数据
mysql> insert into articles values(20,'123456789',3,0,'weiming','武汉大明.哥'),(20,'123456789',3,0,'weiming','武汉大明.哥'),(20,'123456789',3,0,'weiming','武汉大明.哥');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
删除数据
delete from 表名;#删除整张表的数据
delete from 表名 where 条件; #删除制定条件的数据
mysql> delete from articles where age=25; #删除 age为25的数据行
truncate table 表名; #也是删除整张表的数据
区别:delete from 表名; 清空数据以后 再次插入数据 id 从原来的id 往后顺延
truncate table 表名;清空数据以后 再次插入数据 id 从1开始
改
update 表名 set 字段名1=值1,字段名2=值2 where 条件;
mysql> update articles set age=19,name='二十不惑' where id=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from articles;
+----+------+----------+------+--------------+----------------------------------+
| id | age | password | sex | name | title |
+----+------+----------+------+--------------+----------------------------------+
| 1 | 19 | 123456 | 1 | 二十不惑 | 我不是出色的电焊工 |
| 2 | 18 | 123456 | 1 | kangbazi | 没有点到你 |
| 3 | 18 | 123456 | 1 | kangbazi | ..有把你我的心焊在一起 |
+----+------+----------+------+--------------+----------------------------------+
3 rows in set (0.00 sec)
查
select 你要查询的字段名 from 表名 where 条件;
mysql> select name,title from articles where id>=1;
+--------------+----------------------------------+
| name | title |
+--------------+----------------------------------+
| 二十不惑 | 我不是出色的电焊工 |
| kangbazi | 没有点到你 |
| kangbazi | ..有把你我的心焊在一起 |
+--------------+----------------------------------+
3 rows in set (0.00 sec)
mysql> select name as 姓名,title as 题目 from articles where id>=1;
+--------------+----------------------------------+
| 姓名 | 题目 |
+--------------+----------------------------------+
| 二十不惑 | 我不是出色的电焊工 |
| kangbazi | 没有点到你 |
| kangbazi | ..有把你我的心焊在一起 |
+--------------+----------------------------------+
3 rows in set (0.00 sec)
select * from 表名; #显示所有的 字段 以及所有的数据行
数据库 进阶
- 视图
- 内置函数
- 预处理
- 事务
- 触发器
- 存储
MySQL视图
? view
ALTER VIEW
CREATE VIEW
DROP VIEW
-
干什么
当我们经常查询某个条数的数据 比如 select * from user where id>5 and id<10;
-
主表在 临时表在 主表损坏 临时表也损坏 主表恢复 临时表也恢复
create view 临时表的名字 as select * from 主表名 where id>1 and id<10;
mysql> show create view v_stars\G
drop view 视图名字;
内置函数
- 日期
- 字符串
- 数学
help contents;
? Functions
? Date and Time Functions
日期
- curdate() 返回当前的日期
- curtime() 返回当前的时间
- now() 现在的日期和时间
- unix_timestamp()
- from_unixtime()
- datediff()
mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2020-07-22 |
+------------+
1 row in set (0.00 sec)
mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 14:32:05 |
+-----------+
1 row in set (0.00 sec)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2020-07-22 14:32:11 |
+---------------------+
1 row in set (0.00 sec)
mysql> select unix_timestamp(now());
+-----------------------+
| unix_timestamp(now()) |
+-----------------------+
| 1595399565 |
+-----------------------+
1 row in set (0.00 sec)
mysql> select from_unixtime('1595399565');
+-----------------------------+
| from_unixtime('1595399565') |
+-----------------------------+
| 2020-07-22 14:32:45.000000 |
+-----------------------------+
1 row in set (0.01 sec)
mysql> select datediff(curdate(),'1998-10-20');
+----------------------------------+
| datediff(curdate(),'1998-10-20') |
+----------------------------------+
| 7946 |
+----------------------------------+
1 row in set (0.00 sec)
mysql> select datediff(curdate(),'1998-10-20') DIV 365;
+------------------------------------------+
| datediff(curdate(),'1998-10-20') DIV 365 |
+------------------------------------------+
| 21 |
+------------------------------------------+
1 row in set (0.00 sec)
字符串
- concat 连接字符串
- lcase 转成小写
- ucase 转成大写
- length 字符串长度
- ltrim 取出左侧的空格
- rtrim 去除右侧的空格
- repeat(3) 重要的话说三遍
- replace 字符串替换
- substr 字符串 切割
mysql> select concat('python','是世界上最好的语言');
+------------------------------------------------+
| concat('python','是世界上最好的语言') |
+------------------------------------------------+
| python是世界上最好的语言 |
+------------------------------------------------+
1 row in set (0.00 sec)
mysql> select lcase("MYSQL");
+----------------+
| lcase("MYSQL") |
+----------------+
| mysql |
+----------------+
1 row in set (0.00 sec)
mysql> select ucase("mysql");
+----------------+
| ucase("mysql") |
+----------------+
| MYSQL |
+----------------+
1 row in set (0.00 sec)
mysql> select length("前途无量");
+------------------------+
| length("前途无量") |
+------------------------+
| 12 |
+------------------------+
1 row in set (0.00 sec)
mysql> select length("qiantuwuliang");
+-------------------------+
| length("qiantuwuliang") |
+-------------------------+
| 13 |
+-------------------------+
1 row in set (0.00 sec)
mysql> select concat("whpython",ltrim(" 前途无量"));
+------------------------------------------------+
| concat("whpython",ltrim(" 前途无量")) |
+------------------------------------------------+
| whpython前途无量 |
+------------------------------------------------+
1 row in set (0.00 sec)
mysql> select concat(rtrim(" 前途无量 "),"武汉python");
+-----------------------------------------------------------+
| concat(rtrim(" 前途无量 "),"武汉python") |
+-----------------------------------------------------------+
| 前途无量武汉python |
+-----------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select repeat("python is so good",3);
+-----------------------------------------------------+
| repeat("python is so good",3) |
+-----------------------------------------------------+
| python is so goodpython is so goodpython is so good |
+-----------------------------------------------------+
1 row in set (0.00 sec)
mysql> select replace("python is so good","python","mysql");
+-----------------------------------------------+
| replace("python is so good","python","mysql") |
+-----------------------------------------------+
| mysql is so good |
+-----------------------------------------------+
1 row in set (0.00 sec)
mysql> select substr("python is so good",3,5);
+---------------------------------+
| substr("python is so good",3,5) |
+---------------------------------+
| thon |
+---------------------------------+
1 row in set (0.00 sec)
mysql> select concat(space(20),"python is so good");
+---------------------------------------+
| concat(space(20),"python is so good") |
+---------------------------------------+
| python is so good |
+---------------------------------------+
1 row in set (0.00 sec)
mysql> select concat(space(2),"python is so good");
+--------------------------------------+
| concat(space(2),"python is so good") |
+--------------------------------------+
| python is so good |
+--------------------------------------+
1 row in set (0.00 sec)
数学函数
- bin 转二进制
- ceiling 向上取整
- floor 向下取整
- max 最大值
- min 最小值
- rand 随之值
mysql> select bin(20);
+---------+
| bin(20) |
+---------+
| 10100 |
+---------+
1 row in set (0.00 sec)
mysql> select ceiling(1.3);
+--------------+
| ceiling(1.3) |
+--------------+
| 2 |
+--------------+
1 row in set (0.00 sec)
mysql> select floor(1.3);
+------------+
| floor(1.3) |
+------------+
| 1 |
+------------+
1 row in set (0.00 sec)
mysql> ? max
Name: 'MAX'
Description:
Syntax:
MAX([DISTINCT] expr)
Returns the maximum value of expr. MAX() may take a string argument; in
such cases, it returns the maximum string value. See
https://dev.mysql.com/doc/refman/5.7/en/mysql-indexes.html. The
DISTINCT keyword can be used to find the maximum of the distinct values
of expr, however, this produces the same result as omitting DISTINCT.
If there are no matching rows, MAX() returns NULL.
URL: https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html
Examples:
mysql> SELECT student_name, MIN(test_score), MAX(test_score)
FROM student
GROUP BY student_name;
mysql> select rand();
+--------------------+
| rand() |
+--------------------+
| 0.7413666637068304 |
+--------------------+
1 row in set (0.00 sec)
mysql> select sqrt(4);
+---------+
| sqrt(4) |
+---------+
| 2 |
+---------+
1 row in set (0.00 sec)
预处理
防止sql注入的一种方式
提前进行响应的处理 你只需要传过来符合要求的值就好了
select * from users where id>?
mysql> set @i=1; #设置变量
Query OK, 0 rows affected (0.00 sec)
mysql> select @i; #打印变量
+------+
| @i |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> prepare yuxing from "select * from stars where id>?";
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> set @i=3;
Query OK, 0 rows affected (0.00 sec)
mysql> execute yuxing using @i;
+----+-----------+---------+----------+------+------+
| id | username | balance | province | age | sex |
+----+-----------+---------+----------+------+------+
| 4 | 刘亦菲 | 2000.12 | 湖北 | 29 | 1 |
| 5 | 陈好 | 4000.00 | 山东 | 31 | 1 |
| 6 | 贾玲鹏 | 6666.32 | 湖北 | 34 | 1 |
+----+-----------+---------+----------+------+------+
3 rows in set (0.00 sec)
事务
保证一组数据库操作 要么全部成功 要么全部失败
myisam 和 innodb 引擎的区别之一 就是 innodb引擎支持事务 而 myisam 引擎不支持事务
- 原子性 A atomicity 要么全部成功 要么全部失败
- 一致性 C consistency a 和 b之间转装 a增加的钱 就是 b 减的前
- 隔离性 I isolation 四个隔离级别
- 读未提交 read uncommitted 事务还没有提交 它所做的变更已经被其他事务看到了
- 事务a 对数据做了修改 还没有提交 就被事务b 看到了 这就是脏读 如何解决 用下面的读提交
- 读提交 read committed 一个事务提交以后 它所做的变更才能被其它事务看到 更新
- 事务a 对数据做了修改 还没有提交 就被事务b 看到了 这就是脏读 如何解决 用下面的读提交
- 可重复读 repeatable read 一个事务执行过程中看到数据 总是跟事务启动的时候看到 数据是一致的 针对insert而言
- 序列化 serializable 同一行记录 读+读锁 写+写锁
- 读未提交 read uncommitted 事务还没有提交 它所做的变更已经被其他事务看到了
- 持久性 D durability
- mysql 是以文件的形式保存在磁盘中
create table t1(id int)engine=innodb default charset=utf8;
insert into t1 values(1);
事务a | 事务b |
---|---|
启动事务 查询得到值 1 | 启动事务 |
查询值得到 1 | |
将1 改成了2 | |
查询得到值 V1 | |
提交事务b | |
查询得到值V2 | |
提交事务a | |
查询值得到V3 |
不同隔离级别下 V1 V2 V3的值分别是多少?
读未提交 V1=2 V2=2 V3=2 虽然事务b并没有提交 但是它做的变更被a事务读到了 所以V1的值是2
读提交 V1=1 V2=2 V3=2
可重复读 最开始 看到的是 1 执行过程中 跟刚开始看到的是一致的 所以 V1 V2都是1 V3是2
串行化或者序列化 事务b将1变成2 上锁 事务a提交以后 事务b才可以继续执行 所以 从事务a的角度看 V1 V2的值都是1 V3的值是2
sqlserver 、Oracle 他们默认隔离级别是 读提交
mysql 默认的隔离级别是 可重复读
事务的引擎必须是 innodb
alter table 表名 engine=innodb;
开始事务 begin; 或者 start transaction
回滚 rollback;
提交 commit;
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into qifan values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
Query OK, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0
mysql> savepoint p1;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into qifan values(11),(12),(13),(14),(15),(16),(17),(18),(19,),(20);
Query OK, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0
mysql> savepoint p2;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into qifan values(21),(22),(23),(24),(25),(26),(27),(28),(29)),(30);
Query OK, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0
mysql> savepoint p3;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from qifan;
Query OK, 30 rows affected (0.00 sec)
mysql> rollback to p3; #如果 开始回到 p3 那么还能回到 p2 回到p1 如果 从p1 回到p3 不行
Query OK, 0 rows affected (0.00 sec)
mysql> select * from qifan;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 20 |
| 21 |
| 22 |
| 23 |
| 24 |
| 25 |
| 26 |
| 27 |
| 28 |
| 29 |
| 30 |
+----+
30 rows in set (0.00 sec)
mysql> delete from qifan;
Query OK, 30 rows affected (0.00 sec)
mysql> rollback to p2;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from qifan;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 20 |
+----+
20 rows in set (0.00 sec)
mysql> delete from qifan;
Query OK, 20 rows affected (0.00 sec)
mysql> rollback to p1;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from qifan;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
+----+
10 rows in set (0.00 sec)
mysql> delete from qifan;
Query OK, 10 rows affected (0.00 sec)
mysql> rollback to p3;
ERROR 1305 (42000): SAVEPOINT p3 does not exist
Python 操作MySQL
创建虚拟环境
1.yum -y install python3
which python3 #获取python3 的路径
2.pip3 install virtualenvwrapper
find / -name virtualenvwrapper.sh #找到这个脚本的路径
3.sudo ~/.bashrc
export WORKON_HOME=$HOME/.virtualenvs # 这个=目录用来存放所有的虚拟环境
source /usr/local/bin/virtualenvwrapper.sh #这个是virtualenvwrapper的 脚本路径
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 #默认创建python3的虚拟环境 指定 python3的位置
4. source ~/.bashrc #让配置文件立即生效
5.mkvirtualenv test_env
6.pip install pymysql
连接数据库
import pymysql
db = pymysql.connect("127.0.0.1",'root','123456','school')
#创建一个句柄
cursor = db.cursor()
sql = "select * from tb_student where stuid>1"
try:
cursor.execute(sql)
results = cursor.fetchall()
for result in results:
print("%s--%s"%(result[0],result[1]))
except:
db.rollback()
cursor.close()
db.close()
插入数据
import pymysql
db = pymysql.connect("127.0.0.1",'root','123456','school')
#创建一个句柄
cursor = db.cursor()
sql = "insert into tb_student(stuid,stuname,stubirth,collid) values(1234,'asdfad','1999-06-01',2)"
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
cursor.close()
db.close()