1.MYSQL 语言的分类
(1) DDL 数据库定义
(2) DQL 数据库查询
(3) DML 数据库操作
(4) DCL 数据库权限
2.MYSQL 操作
(1) 创建数据库mysql> create database cmdb default charset utf8;
(2)查看所有的数据库mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cmdb |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
(3) 使用cmdb数据库mysql> use cmdb;
(4) 查看数据库的创建语法mysql> show create database cmdb;
(5) 删除数据库mysql> drop database cmdb;
(6) 查看所有的表mysql> show tables;
(7) 创建用户表mysql> create table user(id int,name varchar(64),age int, sex boolean,telphone varchar(32), addr varchar(512))engine=innodb default charset utf8;
(8) 查看创建表的过程mysql> show create table user;
(9) 删除表mysql> drop table user;
(10) 查看表结构mysql> desc user;
(11) 插入数据mysql> insert into user(id,name,age,sex,telphone,addr)values(1,'李宽',25,1,'18829787559','陕西省西安市');
(12) 查看数据mysql> select * from user;
(13) 只查询指定的列mysql> select name,addr from user;
(14) 条件查询
where
逻辑关联词 and or
关系表达式 > = <= !=
like表达式
(1) % 占多位 'abc%' '%abc'
(2) _ 占一位 ‘abc_’ '_abc'
in 的使用 colname in (a,b)
not in 的使用 colname not in (a,b)select name,age,addr from user where addr = '陕西省西安市' and age=25;
mysql> select name,age,addr from user where addr = '陕西省西安市' or age = 25;
select name,age,addr from user where addr = '陕西省西安市' or age > 25;
mysql> select name,age,addr from user where age >= 25;
mysql> select name,age,addr from user where age != 25;
select name,age,addr from user where age
mysql> select name,age,addr from user where addr like '陕西省%';
mysql> select name,age,addr from user where addr like '%市';
mysql> select name,age,addr from user where not (addr like '临汾市');
mysql> select name,age,addr from user where age in (23,25);
mysql> select name,sex,age,addr from user where age not in (15,25);
(15) 查询总数mysql> select count(*) from user;
3.创建CMDB的用户表
建表的sql,性别在数据库中存储的时候,男存1,女存0CREATE TABLE user(
id int primary key auto_increment,
name varchar(32) unique not null default '',
password varchar(512) not null default '',
age int not null default 18,
sex boolean not null default 1,
tel varchar(16) not null default '',
addr text,
add_time datetime
)ENGINE=INNODB DEFAULT CHARSET utf8mb4;
批量插入测试数据insert into user(name, password, age, sex, tel, addr, add_time) values ('kk', md5('kk'), 30, 1, '15200000000', '西安市', now()),\
('woniu', md5('woniu'), 30, 1, '15200000001', '北京市', now()),('zhangzhengguang', md5('zhangzhengguang'), 30, 1, '15200000003', '杭州市', now()),\
('likuan', md5('likuan'), 30, 1, '15200000002', '西安市', now())
查看用户登录的用户名和密码mysql> select name,password from user where name='likuan' and password=md5('likuan');
查找所有的数据mysql> select id,name,password,age,sex,tel,addr from user ;
限制查询的数据 (limit可以用来做分页)mysql> select id,name,password,age,sex,tel,addr from user limit 1;
Limit 和 offset结合使用mysql> select id,name,password,age,sex,tel,addr from user limit 2 offset 2;
排序 (降序和升序)
降序(desc)Mysql> select id,name,password,age,sex,tel,addr from user order by age desc;
升序(asc)mysql> select id,name,password,age,sex,tel,addr from user order by age asc;
更新操作mysql> update user set age=15 where id = 3;
mysql> update user set name='kk',tel='152',sex=1,addr='西安市' where id = 1;
删除操作mysql> delete from user where id = 1;
mysql> delete from user;
聚合函数mysql> select max(age),min(age),avg(age),count(age),sum(age) from user;
分类统计mysql> select addr, count(*) from user group by addr;
mysql> select addr,age, count(*) from user group by addr,age;
4.Python代码里操作mysql
首先需要安装mysql的开发包 mysql-devel
其次pip安装 mysqlclient
使用是导入包 MysqlSQLdb
Python操作mysql的七步
(1)导入模块import MySQLdb
(2)创建连接conn=MySQLdb.connect(host='127.0.0.1',port=3306,user='root',passwd='passwd',db='cmdb')
(3)获取游标cursor = conn.cursor()
(4)执行sql(DQL 和 DML)
DQL
返回符合条件的个数cursor.execute("select id,name from user where name='likuan' and password=md5('likuan');")
DMLcursor.execute("update user set age = 35 where id = 1")
(5)DQL获取结果 、DML提交执行
DQL(元组)cursor.fetchall()
cursor.fetchone()
>>> cursor.fetchall()
(('kk',), ('likuan',), ('woniu',), ('zhangzhengguang',))
DML 提交conn.commit()
(6)关闭游标cursor.close()
(7)关闭连接conn.close()
5.提交sql采用预处理的方式(预防sql注入)
(1)将操作和数据分开
(2)两个变量,一个是sql操作,一个是对应的数据
(3)只有数据才可以占位,操作不能占位
标签:addr,运维,Python,age,Mysql,user,mysql,select,name
来源: https://blog.51cto.com/12217124/2359822