单表查询语法
select distinct 字段1,字段2,字段3。。。。 from 表名
where 约束条件
group by 分组的字段
having 过滤条件
order by 排序字段
limit 限制条件;
简单查询
SELECT id,name,sex,age,hire_date,post,post_comment,salary,office,depart_id FROM employee; SELECT * FROM employee; SELECT name,salary FROM employee;
避免重复DISTINCT
SELECT DISTINCT post FROM employee;
通过四则运算查询
SELECT name, salary*12 FROM employee; SELECT name, salary*12 AS Annual_salary FROM employee; SELECT name, salary*12 Annual_salary FROM employee;
定义显示格式
CONCAT() 函数用于连接字符串 SELECT CONCAT('姓名: ',name,' 年薪: ', salary*12) AS Annual_salary FROM employee; CONCAT_WS() 第一个参数为分隔符 SELECT CONCAT_WS(':',name,salary*12) AS Annual_salary FROM employee;
强调
#1、分组之后,select只能查看到分组的字段,要想查组内内容
#不能直接查看,需要借助于聚合函数max,min,avg,sum,count
#2、分组的目的是为类以组为单位来处理记录,而不是处理单独的记录
#3、如果我们用unique的字段作为分组的依据,则每一条记录自成一组,这种分组没有意义
多条记录之间的某个字段值相同,该字段通常用来作为分组的依据
分组
#查询总员工数:没有分组,默认整体一组
select count(id) from employee;
#查看每个部门的员工数
select post,count(id) from employee group by post;
2、内连接inner join:取两张表交集
mysql> select * from employee inner join department on employee.dep_id = department.id;
此外还有左链接右链接分别为保存表的左右
备份与导入
1 语法
mysqldump -h 服务器 -u用户名 -p密码 数据库名 > 备份文件.sql
2
备份库
mysqldump -uroot -p --database day45 > C:\day45_bak_2017_10_30.sql
恢复库
mysql -uroot -p < C:\day45_bak_2017_10_30.sql
3
备份多个库 mysqldump -uroot -p --databases day43 day44 day45 > C:\day43_day45_day44_bak_2017_10_30.sql 恢复库 mysql -uroot -p < C:\day45_bak_2017_10_30.sql
4
备份多个表 mysqldump -uroot -p day45 employee t1 t2 > C:\day45_t1_t2_employee_bak_2017_10_30.sql 恢复表 mysql -uroot -p day45 < C:\day45_t1_t2_employee_bak_2017_10_30.sql
5 备份所有的库
mysqldump -uroot -p --all-databases > C:\all.sql 恢复库 mysql -uroot -p < C:\all.sql
6 导出表
mysql> SELECT * FROM school.student1 INTO OUTFILE 'student1.txt' FIELDS TERMINATED BY ',' //定义字段分隔符 OPTIONALLY ENCLOSED BY '”' //定义字符串使用什么符号括起来 LINES TERMINATED BY ' ' ; //定义换行符
7 导入表
mysql> LOAD DATA INFILE '/tmp/student1.txt' INTO TABLE school.student1 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '”' LINES TERMINATED BY ' '; delimiter $$ #声明存储过程的结束符号为$$
1 创建表时就指定索引
create table t1( id int, name char(5), unique key uni_name(name), primary key(id) ); create table t2( id int, name char(5), index idx_name(name) );
2 在创建完表后为其添加索引
create table t3( id int, name char(5) ); create index idx_name on t3(name); alter table t3 add index idx_id(id); alter table t3 add primary key(id); 查看 mysql> show create table t3; +-------+------------------------------------- | Table | Create Table +-------+------------------------------------- | t3 | CREATE TABLE `t3` ( `id` int(11) DEFAULT NULL, `name` char(5) DEFAULT NULL, KEY `idx_name` (`name`), KEY `idx_id` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +-------+------------------------------------- 1 row in set (0.00 sec) 删除 drop index idx_id on t3; alter table t3 drop primary key;
mysql与python的关联使用
使用pymysql模块吗,使用字符串进行命令的拼接,格式化方式进行数据查询条件的导入
在使用mysql模块时候要注意mysql语句注入,还有注意pymysql模块所提供的各种功能
pymysql在使用之前要做准备,像绑定链接一样获得游标
# import pymysql # # conn=pymysql.connect( # host='localhost', # port=3306, # user='root', # password='', # database='day46', # charset='utf8' # ) # # cur=conn.cursor() # # # sql='insert into dep(name) values(%s)' # # cur.execute(sql,('yyyyy',)) # # cur.executemany(sql,[('aa'),('bb'),('ccc')]) # # # # conn.commit() # # cur.close() # conn.close()
事务
start transaction; try: update user set balance=900 where id=1; update user set balance=1010 where id=2; update user set balance=1090 where id=3; commit; except 异常: #一旦出现异常应该执行 #rollback;