事务:是一系列的数据库操作,是数据库应用的基本逻辑单位。
特性:
(1)原子性:即不可分割性,事务要么全部被执行,要么就全部不被执行。
(2)一致性或可串性。事务的执行使得数据库从一种正确状态转换成另一种正确状态
(3)隔离性。在事务正确提交之前,不允许把该事务对数据的任何改变提供给任何其他事务,
(4) 持久性。事务正确提交后,其结果将永久保存在数据库中,即使在事务提交后有了其他故障,事务的处理结果也会得到保存。
简单理解:在事务里的操作,要么全部成功,要么全部失败。
mysql -uroot -ppython
exit; quit;
select version();
select now();
show databases;
create database 数据库的名字 charset=utf8;
show create database pymysql_2019;
select database();
use 数据库的名字;
drop database 数据库的名字;
drop database pymysql_2018;
alter table 表名 drop 字段名;
alter table students drop birth;
drop table 表名
delete from 表名 where 条件;
show tables;
create table students(
id int unsigned not null auto_increment primary key,
name varchar(30) not null,
age tinyint unsigned default 0,
high decimal(5,2),
gender enum('男','女','中性','保密') default "保密",
class_id int unsigned
);
create table students1(
id int unsigned not null auto_increment primary key,
name varchar(30) not null,
age tinyint unsigned default 0,
high decimal(5,2),
gender enum('男','女','中性','保密') default "保密",
class_id int unsigned
);
desc 数据表的名字;
select * from 表名; select 列表1 from 表名;(查询指定字段)
#####16.查看表的创建语句
show create table 表名;
alter table students add birth datetime;
alter table 表名 modify 列名 类型;
alter table 表名 change 列名 类型;
insert into classes values(0, "python2班");
insert into students values(0,"张三",15,170,"女",1);
insert into 表名(列名1,) value ()
update 表名 set 列名1=值 where 条件;
update students set name = "张三" where id =1;
select * from students where id<5;
select * from students where gender="女" and age>18;
select * from students where age>18 or height>=180;
select name from students where name like "小%";
select name from students where name rlike "^周.+";
1.非连续范围
select name from students where age in(18, 34);
2.连续范围
select * from students where age between 18 and 34;
select * from students where height is null;(判断是空)
select * from students where height is not null;(判断是非空)
select 字段 as 姓名 from 表名 条件
select name as 姓名 from students where id>1;
select s.name,s.age from students as s;
select distinct 字段 from 表名;
select * from 表名 where gender = 1 order by 列名 asc;(从小到大)
select * from students where gender = 2 order by id asc;
select * from 表名 where gender = 1 order by 列名 desc;(从大到小)
select * from students where gender = 2 order by id desc;
select count(*) as "男性人数" from students where gender=1;
select max(age) from students;
select min(age) from students;
select sum(age) from students;
select avg(age) from students;
select round (avg(height),2) from students where gender=1;
select gender, count(*) from students where gender=1 group by gender;
select gender, group_concat(name) from students group by gender having count(*)>2;
select * from students where gender=1 limit 2;
select * from students limit 0, 2;
select s.*,c.name from students as s inner join classes as c on s.cls_id = c.id;
select * from students where height = (select max(height) from students);
source areas.sql;
alter table goods add foreign key (brand_id) references goods_brands(id);
python与mysql的最原始的交互