您的位置:首页 > 数据库 > MySQL

mysql数据的安装以及数据库的操作

2017-11-05 18:14 591 查看
mysql数据基本使用

数据库的安装:sudo apt-get install mysql-server
启动:sudo service mysql start

配置:配置文件目录为/etc/mysql/mysql.cnf
进入conf.d目录,打开mysql.cnf,发现并没有配置
进入mysql.conf.d目录,打开mysql.cnf,可以看到配置项

主要配置:bind-address表示服务器绑定的ip,默认为127.0.0.1
  port表示端口,默认为3306
  datadir表示数据库目录,默认为/var/lib/mysql
  general_log_file表示普通日志,默认为/var/log/mysql/mysql.log
  log_error表示错误日志,默认为/var/log/mysql/error.log

命令行客户端:
sudo apt-get install mysql-client
查看帮助文档   mysql --help
最基本的连接命令如下,输入后回车
mysql -u root -p mysql
按ctrl+d或输入如下命令退出   quit 或者 exit

查看版本:select version()

显示当前时间:select now()

修改输入提示符 prompt python>

\D 完整日期

\d 数据库名称

\u 使用用户

\h主机名称

数据库基本操作:查看所有数据库           show databases;
使用数据库               use 数据库名
查看当前使用的数据库      select database()
创建数据库                 create database 数据库名 charset=utf8
删除数据库  drop database 数据库名

数据表操作:查看当前数据库所有表:show tables;
  查看表结构: des 表名;
 创建表:create table 表名(指明字段名、类型)
修改表添加字段: alter table 表名  add  列名 类型
例子 alter table students add birthday datetime;
修改字段:重名    alter table 表名 change 原名 新名 类型及约束
例子 alter table students change birthday birth datetime not null;
修改字段类型:alter table 表名 modify 列明 类型及约束
例子 alter table students modify birth date not null;
修改表 删除字段 :alter table 表名 drop 列明
例子  alter table students drop  birthday;
删表  drop table 表名; drop tables students
查看表的创建语句: show create table 表名;
列子: show create table classes;

数据增删改查:

    查询 查询所有列:select * from 表名  例子:select * from classes
查询指定列 可以使用as为列或表指定别名:

select 列1,列2,.... from 表名
例子 select id,name from classes

    增加 格式说明:主键列是自动增长,全列插入时需要占位,通常使用0或者default或者null占位
全列插入:值的顺序与表中字段的顺序对应
instert into 表名 values()
例子  insert into students values(0,'郭靖',1,'蒙古','2016-1-2'
);

   部分列插入:值得顺序与给出的列顺序对应
insert into 表名(列1...)values(值1,....)
例子  insert into students(name,hometown,birthday)values('黄蓉',‘桃花岛’,‘2016-1-2’)

  全列多行插入:值的顺序与给出的列顺序对应
insert into 表名 values(...),(....):
例子:insert into classes values(0,'python1'),(0,'python2');

  修改:update 表名 set 列1=值,列2=值 where 条件

       例子  update students set gender=0 where id=5;

   删除: delete from 表名 where 条件
列子  delete from students where id=5

  逻辑删除,本质就是修改操作
update students set isdelete=1 where id=1;

备份操作:
运行mysqldump命令  
mysqldump -uroot -p 数据库名 > python.sql;
#按提示输入mysql密码

恢复:连接mysql,创建新的数据库  退出连接
执行命令  mysql -uroot -p 新数据名 < python.sql
#根据提示输入mysql密码

消除重复行: select distinct 列1,....from 表名;
列 select distinct gender from students;

mysql遵循的三范式:第一:列不可分割即原子性
 第二:主键唯一性
 第三:第三范式建立在第二范式的基础上,任何非主属性不依赖其他非主属性

E-R模型:实体关系模型
表的关系为由:一对一,一对多,多对多
处理多对多关系的表的时候:一般是建立第三章表,这张表中仅有两个字段分别为
是两张表的主键值

条件查询使用where:格式: select * from 表名 where 条件
例子  select * from students where id=1;
where 后面支持多种运算符,进行条件的处理
比较运算符: 等于:=    大于:>  大于等于:>=   小于:<
  小于:<   小于等于:<=   不等于:!= 或<>
例子: select * from students where id>3;
      select * from students where id<=4:
      select * from students where name!='黄蓉'
     select * from students where isdelete=0
逻辑运算符: and   or  not
  例子:  select * from students where id>3 and gender=0;
 select * from students where id<4 or isdeletes=0;
模糊查询:like    %表示任意多个任意字符  _ 表示一个任意字符
select * from students where name like ‘黄%’:
select * from students where name like ‘黄_’
select * fr
4000
om students where name like  ‘黄%’or name like ‘%靖’
范围查询: in表示在一个非连续的范围内   
      例子:select * from students where id in(1, 2, 3)
  between....and....表示在一个连续的范围内
例子: select * from students where id between 3 and 8:
空判断:注意:null与“”是不同的     判空:is null  判非空:is not null
select * from students where height is null
select * from students where height is not null
select * from students where height is not null and gender=0
优先级:优先级由高到低的顺序: 小括号 not  比较运算 逻辑运算
and比or先运算,如果同时出现并希望先运算or 需要结合()使用

排序:语法结构:  select * from 表名 order by 列1 asc|desc , 列2 asc|desc
默认按照列值从小到大排列   asc从小到大排列,即升序 desc从大到小即降序
例子: select * from students where gender=1 and isdelete=0  order by id desc;
      select * from students where isdelete=0 order by name;

聚合函数: 快速得到统计数据
count(*)表示计算总行数, 括号中写*与写列名结果使相同的
聚合函数不能再where中使用
select count(*) from sudents:
max(列)表示求此列的最大值
select max(id) from students where gender=0;
min(列)表示求此列的最小值
select min(id) from students where gender=0;
avg(列)表示求此列的平均值
select avg(id)fromstudents where isdelete=0 and gender=0

分组:按照字段分组,表示此字段相同的数据会被放到一个组中

      分组后,分组的依据列会显示在结果集中,其他列不会显示在结果集中

      可以对分组后的数据进程统计,做聚合运算

      select 列1,列2,聚合...from 表名 group by 列1,列2...

        例子: select gender as 性别,count(*) from students group by gender
select age as 年龄,count(*)as 数量 from students group by age;

分组后的数据筛选: 语法  select 列1,列2,聚合....from 表名 
group by 列1,列2,列3....
having 列1,...聚合....
having后面的条件运算符与where的相同
例子:第一种方法: 查询男生总数  select count(*)from students where gender=1
     第二种方法: select gender as 性别,count(*)from students group by gender having gender=1
对比where与having:
where是对from后面指定的表进行数据筛选,属于对原始数据的筛选
having是对group by 的结果进行筛选

获取部分分行:当数据量过大,在一页中查看数据是一件非常麻烦的事情
语法:select * from 表名 limit start,count
  参数 从start开始,获取count条数  start索引从0开始
 例子 查询三行男生信息
select * from students where gender=1 limit 0,3;

分页;已知每页显示m条数据,当前显示第n页 
求总页数:此段逻辑后面会在python中实现
查询总条数p1
使用p1除以m得到p2
如果正处则p2为总页数
如果不整除则P2+1为总页数

     求第n页的数据   select * from students where isdelete=0 limit (n-1)*m,m

连接查询:当查询结果的列来源于多张表时,需要将多张表连接成一个大的数据集,选择合适的列返回
 mysql支持三种类型的连接查询,分别为
   第一种:内连接查询:查询的结果为两个表匹配到的数据
   第二种:右连接查询:查询的结果为两个表匹配到的数据,右表特有的数据
    对于左表中不存在的数据使用null填充
   第三种:左连接查询:查询的结果为两个匹配到的数据,左表特有的数据
    对于右边中不存在的数据使用null填充
语法结构: 
select * from 表1 inner或left或right join 表2 on 表1.列=表2.列
使用内连接查询班级表
  例子: select * from students inner join pythons on students.cls_id=python.id
使用as为表起别名左连接查询,目的是编写简单
select * from students as s left join pythons as p on s.cls_id=p.id
使用右连接查询班级表与学生表
select * from students as s right join pythons as p on s.cls_id=p.id

自关联

子查询:在一个select语句中嵌入了另一select语句,被嵌入的select语句称为子查询语句

住查询的对象,第一条select语句

子查询和主查询的关系 1 子查询是嵌入到主查询中
   2  子查询是辅助主查询的要么充当条件,要么充当数据源
   3 子查询是可以独立存在的语句,是一条完整的select语句

子查询分类; 1 标量子查询:子查询返回的结果使一个数据(一行一列)
例子: select * from students where age>(select avg(age) from students)
  2 列子查询:返回的结果使一列(一列多行)
例子:select name from pythons where id in (select cls_id from students)
  3 行子查询:返回的结果使一行(一行多列)
例子:select * from students where (height,age)=(select max(height),max(age)from students)
  4 表级子查询:返回的结果使多行多列
例子:select * from (select stu.*,pys.name as clsname from students as

stu inner join pythons as pys on stu.cls_id = pys.id)as t1;

子查询中特定关键字使用

    in 范围  格式:主查询where条件in(列子查询)

    any|some 任意一个   格式:主查询where列=any(列子查询)
在条件查询的结果中匹配任意一个即可,等价于in

    all  格式:主查询where列=all(列子查询):等于里面所有
格式:主查询where列<>all(列子查询):不等于-其中所有

完整的select语句 : select distinct*
  from 表名
  where ...
  group by ... having...
  order by ...
  limit start,count

执行顺序为:
from 表名
where ...
group by...
select distinct*
having...
order by...
limit start,count
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql select 数据库
相关文章推荐