MySql常用数据操作

1.数据库操作:
MySQL服务管理命令:
1.启动服务:sudo service mysql start
2.停止服务:sudo service mysql stop
3.重新启动服务:sudo service mysql restart
4.查看服务状态:sudo service mysql statyus
 
2.创建数据库名:
1.创建数据库: create database xxxx charset=utf8;
2.显示数据库创建信息: show create database xxxx;
3.修改数据库编码: alter database xxxx charset=ut8;
4.显示数据库:show databases;
5.切换数据库:use xxxx
6.删除数据库:drop database xxxx;
 
3.数据表操作:
1.查看数据表:show tables;
2.创建表:create table xxxx(id int,name char(20),age int);
3.显示创建表的信息: show create table xxxx
4.删除字段: alter table xxxx drop age
5.修改字段的数据类型: alter table xxxx modify name varchar(20)
6.修改列的数据类型并且改名:alter table xxxx change id sumber sunmber smallint
 
4.常用字段类型:
整数类型:int samllint
小数类型:float(5,2) double
字符串类型:char varchar
枚举类型:enum
 
5.查询数据: select * from xxxx;
 
6.插入数据:
1.插入所有字段数据: insert into xxxx values(有几个字段就传几个数);
2.插入指定字段: insert into xxxx(字段) values(该传入字段的数);
3.插入多条数据: insert into xxxx values(xxx),(xxx);
 
7.修改数据:
1.更新所有数据: update xxxx set 字段=xx;
2.更新满足条件的数据:uptade xxxx set 字段=xx where 条件
 
8.修改数据:
语法1:truncate 表名
语法2: delete from 表名[条件]
1.删除全部数据:truncate xxxx;
2.不需要条件删除: dalete from xxxx;
3.删除满足条件的数据: delete from xxxx where 条件;
 
9.添加约束:(数据不会重复)
1.主键约束: primary key
create table xxxx(id int primary key,name(10));
 
2.自动增长:让数字值自动增加,语法:auto_increment,配合主键约束一起使用
create table xxxx(id int auto_increment primary key,name char(20))
 
3.唯一性约束:作用:保证数据的准确性,不会出现重复,语法:unique
create table xxxx(id int uniqe, name char(10)); #插入数据的时候不能出现重复的数据
 
4.非空约束:作用:不允许字段为空,添加数据是必须给值 语法:not null
create table xxxx(id int,name char(10) not null)
 
5.默认约束:作用:在添加数据是,如果没有默认约束字段的数据,该字段使用默认值进行填充 语法:default
create table xxxx(id int,name char(10) default xx);
 
6.外键约束:让两表之间产生联动关系 语法:foreign key(字段名) references(字段名)
表1:create table xxxx(id int primary key,name char(10));
表2: create table xxxx(id int primary key auto_increment,name char(10),
cid int ,foreign key(cid) references 表1(id)
 
删除的时候必须先删除外键关联的表
 
10.增加删除约束:
1.添加主键约束:语法:alter table 表名 add constraint 约束名 prinmary key(字段名)
alter table xxxx add constraint PK_id prinmary key
2.删除主键约束:一个表中最多只能有一个主键约束 语法:alter table 表名 drop primary key
alter table xxxx drop prikary key
3.添加外键约束:语法: alter table 表名 add constraint 外键约束名 foreigh key
(外键字段名) references 关联表(关联字段名)
alter table xxxx add constraint FK_id foreigh key (id) references xxxx1(id)
4.删除外键约束:语法:alter table 表名 drop foreign key 外键名
alter table xxxx drop foeign key FK_id
 
 
11.数据库导入导出:
导出:mysqldup -uroot -p 要导出的数据库名 要导出的数据表 > 目标文件
导入: 导入数据前先创立一个空的数据库
语法:mysql -uroot -p < 要导入的文件.sql
 
 
12.数据表查询操作:(重要)
1.单标查询:select * from xxxx;
2.查询指定字段的显示: slect 字段1,字段2 from xxxx
3.as取别名 select id as "编号", name as "姓名", address as"地址" from xxxx
(可以在后段直接取别名,省略as)
 
4.消除重复数据:查询结果会出现重复的数据,使用distinct来实现
语法:select distinct字段名 from 表名
select distinct id from xxxx
5.带条件查询where子句:
select * from xxxx where xx=xx;
 
13.运算符:
1.比较
2.逻辑运算符:and or not
3.模糊查询:like
1.% 表示任意多个字符
2._表示一个任意字符(有几个下划线就有几个字符)
 
14.范围查询:
1.in:表示非连续范围
select * from xxxx where id in(1,3,5,7,9) (有id就显示出来,没有就不显示出来)
 
2.between... and...:表示一个连续的范围:
select * from xxxx where id between 2 and 5;
 
3.空判断:
1.判断空值:is null
select * from xxxx where id is null;
2.判断非空值: is not null
select * from xxxx where id is not null;
 
15.排序:
1.单子段排序: order by asc(默认)/desc(降序)
语法:select * from 表名 order by 列1 asc/desc[列2 asc/desc]
 
2.多字段排序:对多个字段进行排序,将排序的字段一次写在order by后面
select * from xxxx order by id ase,age asc;
 
16.分页查询::select from 表名 limit start=0,count *说明
select * from xxxx limit 3;
select * from xxxx limit 2,3;
 
17.聚合函数:
求和:sum()
求平均:avg()
求最小:min()
求最大:man()
求记录的数量:count()
例: select sum(id) from xxxx;
 
18.分组:将相同数据放在一起进行处理,需要配合聚合函数一起使用
语法:select 分组的字段名,聚合函数 from 表名 group by 分组字段名 having 分组后的条件 注意:在 group by分组时,select 后只能有被分组的字段,不允许有其他的字段,其他的字段只能出现在聚合函数里面
 
1.单子段分组:
select xx from xxxx group by xx;
 
2.多字段分组:
select xx, xx, from xxxx group by xx,xx;
 
3.group_count()作用:根据分组结果,使用 group_concat()来获取分组中指定的集合
语法:group_concat(字段名)
select xx gorup_concat(xx) from xxxx group by xx;
 
4.分组和聚合函数一起使用:(单纯的使用没有意义)
select xx,max(xx),min(xx),sum(xx),avg(xx),count(xx) from xxxx group by xx;
 
5.having 条件子句(跟where的作用相似)
where 是对 from表中取数据进行筛选
having是对group by 分组后的数据进行筛选(在执行顺序时,是先执行where取得的条件取出数据进行分组)
select xx,group_conact(xx) from xxxx group by xx having xx
select xx,group_concat(xx) from xxxx where 条件 group by xx hacing 条件
 
6.分组汇总
语法:with rollup
select xx from xxxx group by xx with rollup
 
19.多表查询数据:
1.多表查询链接条件:
select 表1.字段1,表2.字段1 from 表1, 表2 where 表1字段2=表2字段2;
slect t_student.c_name, t_class.c_name from t_student,t_class where t_student.c_class_id=t_class.cid;
 
2.表别名:语法:select 别名.字段名 from 表1 as 表1别名,表2别名[条件]
select ts.c_name as'姓名',tc.c_name'班级名' from t_student as ts,t_class tc where ts.c_class_id=tc.c_id
 
3.内连接查询:查询的结果为两个表匹配的数据
语法:select * from 表1 inner join 表2 on 表1.列(数据库默认连接是内连接查询,inner join可以不显示出来,但是这种连接无意义,所以要加上连接条件,用on进行指定)
select ts.c_name, tc.c_name from t_student as ts inner join t_class tc on ts.c_class_id =tc.cid
 
4.左连接:语法:select * from 表1 left join 表2 on 表1.列 运算符 表2.列
select ts.c_name,tc.c_name from t_student as ts left join t_class tc on ts.c_claas_id= tc.c_id
 
5.右链接:语法:select* from 表1 right join 表2 on 表1.列 运算符 表2.列
 
select tc.c_name,tc.c_name from t_student as ts right join t_class tc on ts.c_class_id=tc.c_id
 
20.查询:
1.子查询:在一个select语句中,嵌入另外一个select语法
语法:select * from 表1 where 条件 运算符 (select查询)
 
1.标量子查询(1.查询班级学生平均年龄,2.查询大于平均年龄的学生)
select * from t_student where c_age>(select avg(c_age) from t_student);
 
2.列级子查询(1.查询学生表中所有班级id,找出班级表中对应的名字)
select * from t_class where c_id in (select c_class_id from t_student);
 
3.行级子查询(1.找出最大年龄和最先班号,2.找出年龄和满足条件的学生)
select * from t_student where(c_age,c_class_id) = (select max(c_age),min(c_class_id) from t_student);
 
2.自链接查询:在查询数据是,只有一张表,自己查询自己 语法:select 8 from 表1 inner join 表2 on 表1.列 运算符 表2.列 where 条件
 
 
21.数据库操作:
1.创建链接 conn=connection(host='localhost',port=3306,database='数据库',user='root', passwd='密码(mysql)',sharset='utf8')
2.获取游标 cur=conn.cursor()
3.执行sql语句(增,删,改,查)
1.增,删,改:
执行:cur.execute(sql)
提交:conn.commit()
 
2.查询:
执行: cur .excute(sql)
result=cur.fetchone(查询一行)
for i in result
print(i)
 
result=cur.fatchmany(要查询的行数) ##(获取指定的行数数据)
result=cur.fatchall() ##查询所有的数据
 
4.关闭游标 cur.close()
5.关闭链接 conn.close()
 
posted @ 2018-09-11 14:03  晴空!  阅读(443)  评论(0编辑  收藏  举报