Python小推车python学习

Python学习打call第四十五天:MySQL数据库使用(一)

2019-03-21  本文已影响3人  暖A暖

1.数据库概念

2.常用的数据库分类

3.关系型数据库的优缺点

优点:

缺点:

4.非关系型数据库的优缺点

优点:

缺点:

5.MySQL数据库的概念

MySQL在过去由于性能高、成本低、可靠性好,已经成为最流行的开源数据库,因此被广泛地应用在Internet上的中小型网站中。随着MySQL的不断成熟,它也逐渐用于更多大规模网站和应用,比如维基百科、Google和Facebook等网站。非常流行的开源软件组合LAMP中的“M”指的就是MySQL。

6.MySQL数据类型

7.SQL语句分类

# 创建库
create database Student;

# 如果没有Student这个库则创建它,character set指定字符集为utf8, 排序规则为utf8_bin
create datebase if not exists Student character set utf8 collate utf8_bin

# 删除库
drop database Student;

# 修改库
alter database Student character set = utf8mb4
alter database Student collate = utf8mb4_general_ci

# 创建表
create table students (id int not null primary key auto_increment, name varchar(250) not null, class varchar(250) not null) engine=InnoDB default charset=utf8;

# 查看表结构
desc students;

# 修改表
alter table students add gender enum('f','m');
alter table students drop gender;
alter table students change name username varchar(100) after id;
alter table students modify username varchar(100) first;

# 删除表
drop table students;
# 给students表增加数据
insert into students (class,username) values ('一年级', 'Luky'), ('二年级', 'Tom'), ('三年级', 'Mark');

# 修改students表中id = 1 的字段class的值为“初一”
update students set class = '初一' where id = 1;

# 删除class为一年级的字段
delete from students where class = '一年级';
# 先创建用户,再授权
create user Mark@'172.16.19.%' identified by '123456';
grant all on *.* to Mark@'172.16.19.%';
flush privileges;

# 创建用户的同时给用户授权
grant all on *.* to Mark@'172.16.19.%' identified by '123456';
flush privileges;

# 给用户授予某些权限
show grants for Mark@'172.16.19.%';

# 撤销授权
revoke select ON *.* from Mark@'172.16.19.%';
flush privileges;

# 查看用户的权限
show grants for Mark@'172.16.19.%';

# 删除用户
delete from mysql.user where user = "Mark";
flush privileges;

# 删除用户
drop user 'Mark'@'192.168.%.%';

# 修改密码
(1)set password for 'Mark'@'192.168.%.%' = password('123456');
(2)使用update命令,直接修改 mysql.user 用户表,修改之后flush priveleges;
# SQL删除记录语句
sql = "DELETE FROM Student WHERE AGE > %s" % (30)
try:
    cursor.execute(sql)
    # 向数据库提交
    db.commit()
except:
    # 发生错误时回滚
    db.rollback()

8.SELECT 语句

SELECT语句处理流程.png
select * from Student
select id,name from Student;
select id as num,name from Student;

select id as num,name as username from Student;
select * from Student where id >= 1;
select * from Student where id <= 5 and id >1;
select * from Student where id <3 or id >10;
select * from Student where id between 1 and 5;    # between 较小的数 and 较大的数
select * from Student where class like 'Ma%';

select * from Student where class like 'Ma_k';
select * from Student where id is not null;
select * from Student where id is null;

select age,gender from students group by gender; 
select avg(age),gender from students group by gender; 
select min(age), gender from students group by gender;
select max(age), gender from students group by gender;
select count(id), gender from students group by gender; 
select * from Student order by id desc;
select avg(age) as 'average_age', gender from students group by gender having average_age > 50;
# 选前5行
select id,name from students order by id desc limit 5; 

# 前4个不选,从第5行开始选2行
select id,name from students order by id limit 4, 2; 
select *,avg(score) as '各班平均成绩' from students where id > 1 group by class having avg(score) > 55 order by score desc limit 3 ;
上一篇下一篇

猜你喜欢

热点阅读