mysql
一、数据库的介绍
1、什么是数据库?
定义:数据库是存放数据的电子仓库
2、是以某种方式存储百万条、上亿条数据,提供多个用户访问共享
3、每个数据数据库有一个或多个api用于创建,访问,管理和复制所保存的数据
4、系统中有很多动态数据存储在数据库中,需要通过访问数据库才能显示
5、数据库的类型:
a、关系型数据库:
(1)db2
(2)mysql(我们学习)  目前也被oracle 公司收购
(3)oracle  (甲骨文)
(4)sql  server
特点:安全、保持数据的一致性、实现表与表进行复杂的数据查询
b、非关系型数据库
(1)redis    (键值对类型)缓存
(2)hbase (列表型)
特点:效率高、容易拓展、使用更加灵活
二、mysq介绍
1、Mysql是关系型数据库管理系统,我们常说的xxxx数据库就是指xxxx数据库管理系统。
2、Mysql数据库由瑞典Mysql DB公司开发,目前属于Oracle公司,
3、在web应用方面,Mysql是最好的关系型数据库管理系统
4、Mysql特点
a.体积小,安装简单,维护成本低
b、开源,免费
c.使用C++编写
d.支持多系统
e、mysql与其他工具组合可以搭建网站系统:
lamj=linux+apache+msyql+java
lamj=linux+anginx+msyql+java
5、mysql 的应用结构:
a、单点数据库:使用小规模应用(现在学)
b、复制:适用中小规模的应用
c、数据库集群:适合大规模的应用 (举例:mgr集群,三主三从,一主三从)
6、数据库中的术语:
(1)数据库
(2)表
(3)行
(4)列
(5)值
(6)字段名
(7)字符类型
(8)索引
(9)主键
(10)唯一
(11)默认值
(12)视图
(13)外键
(14)存储过程
(15)多表
(16)单表
三、msyql安装
1、安装流程
(1)查询linux中是否已经安装msyql:
rpm -qa|grep mysql
在安装以前,把所有sql包删除干净
(2)删除包名:
a、rpm -e --nodeps  msyql包名
b、yum  remove  mysql  删除在线安装 的包
(3)yum  install  mysql  在线安装数据的客户端
(4)yum  install  mysql-server  在线安装数据库服务端
安装后在查询有三个包就表示安装成功
(5)启动数据库
service  mysqld status 查看数据库状态
service  mysqld start   重启数据库
service  mysqld stop    关闭数据库
service  mysqld restart   重启数据库
(6)设置密码:
格式:mysqladmin -u root password "密码”
mysqladmin -u root password "123456"
(7)登录数据库:
mysql -u root -p   敲回车
输入密码:123456
进入到mysql界面
msyql开头的就是mysql界面,root开头就是linux界面
退出:
ctrl+c 或ctrl+z
(8)show databases ; 显示所有的库
查看到三个原始的库
(9) create database  库名 新建库
举例:create database dcs
(10)use  使用库
举例:use  dcs ;
(11)show  tables  显示所有的表
(12)创建表:
格式:create table  表名 (字段名1  字符类型1(字符长度1),字段2  字符类型2(字符长度2));
举例:create table  aa (id int(10),name varchar(20));
(13)查看表结构:
desc  表名
举例:desc  aa ;
(14)授权命令:
grant  all privileges on . to  root@"%" identified by  "123456";
刷新权限:
flush  privileges ;
四、安装navicat
1、下载安装包、解压、在安装中进行注册,
2、输入注册码:
名称和组织不用填写
NAVH-WK6A-DMVK-DKW3密钥
3、连接
名称:自定义
ip地址:ip地址
端口:3306
账号:root
密码:123456
4、显示 连接的库,选择一个库,点击查询,新建查询
5、在查询的编辑中输入sql语句
select  * from bb ;
6、连接的注意事项:
a、关闭防火墙:service  iptables stop
b、开启数据库:service mysqld start
c、授权:grant  all privileges on . to  root@"%" identified by  "123456";
刷新权限:flush  privileges ;
d、连接的ip地址,账号、密码等是否错误
e、密码报错:1045
修改:
解决办法
第—步:关闭mysql
第二步:mysqld_safe --user=mysql --skip-grant-tables --skip-networking &mysql -u root mysql
第三步:UPDATE user SET Password=PASSWORD(123456') where USER='root';
第四步:FLUSH PRIVILEGES;
五、数据库的运用
(1)操作流程
1、进入数据库mysql -u root -p123456
2、查看数据所有库:show  databases ;
3、新建一个仓库:create  database  库名
4、使用具体的库: use   库名
5、查看库中的所有表:show tables
6、建表语句:create table  ss(sid  int(10),sex int(10));
7、查看表结构:desc  表名
8、查看所有表的内容:select  * from   表名
(2)插入语句:
a、插入全部字段
insert  into  表  values(字段值1,字段值2)
案例:insert  into  bb  VALUES(2,"lisi");
b、插入指定字段
insert  into  表名(字段名)  VALUES(对应字段值);
案例:insert  into  bb(id)  VALUES(3);
(3)解决中文显示问题,建表加上字符编码格式: DEFAULT charset=utf8;
案例:
create  table cc(id  int(10),name VARCHAR(20)) DEFAULT charset=utf8;
(4)删除表 drop table   表名;
格式:drop  table 表名;
案例:drop  table cc;
(5)创建表:
create  table 表名 (字段名1 字符类型2(字符长度),字段名2 字符类型2(字符长度2)) DEFAULT charset=utf8;
案例:
create  table cc(id  int(10),name VARCHAR(20)) DEFAULT charset=utf8;
建表的字符类型:
数值类型:
int 或intteger    大整数值  4个字节
float   单精度,浮点数      4个字节
字符类型:
char    定长字符类型      0-255字节
varchar     边长字符       0-65535字节
时间类型:
date    年月日     3个字节
time  时分秒  3个字节
year  年  1个字节
datatime  年月日、时分秒  8个字节数
(6)对表格中的字段进行操作
a、建表
案例:create  table cc(id  int(10),name VARCHAR(20)) DEFAULT charset=utf8;
b、add 添加表字段
格式:ALTER  table  表名  add 字段名 字符类型(字符长度);
案例:ALTER  table  cc  add sg char(10);
c、修改表字段
格式:
ALTER  table  表名 change 原字段名  新字段名  新字符类型(新字符长度);
案例:
ALTER  table  cc  change sg tz char(10);
d、drop 删除字段
格式:
ALTER  table  表名 DROP  字段名;
案例:
ALTER  table  cc DROP  tz;
e、rename  修改表名
格式:ALTER  table  源表名 RENAME  新表名;
案例:ALTER  table  cc  RENAME  h1;
f、modify   after   字段的调换
格式:
ALTER  table   表名   modify  调换的字段名  字符类型(字符长度)  AFTER   指定字段 ;
案例:
ALTER  table h1  modify  sex  char(10) AFTER id ;
g、first  添加字段到第一位
格式:
ALTER  table   表名 add    新字段名  字符类型(字符长度) FIRST ;
案例:
ALTER  table h1  add  no int(20) FIRST ;
六、数据库中的增删改查
1、查询字段  select
(1)查询所有内容
格式:select  * from 表名 ;    *  表示的所有
案例:select  * from h1 ;
(2)查询部分字段内容
格式:
select  字段1,字段2  from  表名
案例:
select  id,name  from h1   ;  用逗号区分
(3)查询的字段设置成别名  as
格式:
select  字段名1  as "别名1",字段2 "别名2"  from  表;
案例:select  id as "编号",name "姓名"  from h1
注意点:as  可以省略不写
(4)查询内容可以接条件
where  +条件
条件1:
=,!=,>,<,<>,>=,<=
条件2:
and 、or、between...and ,in ,not  in, null,is  not null
新建一个表:
create table student2(
id int primary key ,
name char(20),
sex char(10),
age int(3),
mobile char(20),
class char(10),
english int(10),
chinese int(10),
math int(10)
)engine=innodb default charset=utf8;
insert into student2 values
(1,'小红','女',23,'13813828824','1719',77,88,98),
(2,'小明','男',23,'13713713711','1720',56,66,55),
(3,'小李','男',23,'15915913911','1719',78,64,87),
(4,'小张','男',23,'15915913912','1720',77,76,77),
(5,'小白','女',24,'15915913913','1719',90,89,98),
(6,'小陈','女',19,'15915913914','1719',84,100,81),
(7,'小钱','女',20,'15915913915',null,45,99,93);
案例:
-- 等于
select  *  from  student2 where id=3;
-- 不等于
select  *  from  student2 where id!=3;
-- 不等于
select  *  from  student2 where id<>3;
-- 大于
select  *  from  student2 where id>3;
-- 小于
select  *  from  student2 where id<3;
-- 小于等于
select  *  from  student2 where id<=3;
-- 大于等于
select  *  from  student2 where id>=3;
案例:
AND 同时满足条件
select  *  from  student2 where id>3  and math>90;
-- or  满足其中一个条件
select  *  from  student2 where id>3  or math>90;
between and 在什么范围之间 包含开始值和结束值
select * from student2 where id BETWEEN 3 and 6 ;
in 匹配符合条件的内容
select * from student2 where id in (1,3,6,9)
not in 匹配不符合条件的内容
select * from student2 where id not in (1,3,6,9);
IS NULL 为空
select  *  from  student2  where class  is null ;
-- is not NULL 不为空
select  *  from  student2  where class  is  not null ;
(5)order by 排序
a、降序 desc
格式:
select  *  from  表名  order by 字段名   desc;
案例:select  *  from  student2  order by  math   desc;
b、升序
asc 可以省略不写
select  *  from  student2  order by  math   ; #升序
select  *  from  student2  order by  math   asc;#升序
c、二次排序(第一次排序存在相同,就排第二个字段)
案例:select  *  from  student2  order by  math   desc,chinese desc;
(6)like 模糊查询
% :表示匹配1个字符或多个字符
_:下划线表示一个字符
案例:
模糊匹配9开头的数据
select * from student2 where math like "9%"
模糊匹配7结尾的数据
select * from student2 where math like "%7"
模糊匹配包含8的数据
select * from student2 where math like "%8%"
模糊匹匹配指定的位数
select  *  from  student2  where chinese like "___"
(7)limit 显示指定的行数,
格式:limt (索引位,步长)
索引:一个表格中的索引位从0开始, 举例:第一行索引就0
步长:显示多少行
案例:
1、select  *  from  student2  limit 2; 显示两行,默认从索引0开始,显示两行
2、
select  *  from  student2  limit 2,3;  2是显示从第三行开始索引0开始,3是显行
3、先降序在取指定的行数
select  *  from  student2  order by  math   desc LIMIT 3;
(8)sql的聚合函数
max  最大值
min  最小值
avg  平均值
sum   求和
count   统计
distinct 去重
案例:
max最大值
select max(id) from student2 ;
min最小值
select min(id) from student2 ;
avg平均值
select  avg(id)  from  student2  ;
-- sum   求和
select  sum(id)  from  student2  ;
-- count   统计
select  count(id)  from  student2  ;
-- distinct 去重
select  DISTINCT(class)  from  student2  ;
(9)分组  group  by
案例:
select  class,sum(id) from  student2 group by class
(10)having  和where的意思是一样,也是接条件
案例:分组后接having 再接条件
select  class,sum(id) from  student2 group by class HAVING sum(id)>10;
select  class,sum(id) as a from  student2 group by class HAVING a>10;
having一般接在group by后面
(11)改
格式:
UPDATE 表名 set  字段名=新值 where 条件 ;
案例:
UPDATE  student2 set name=" 小才" where id=1 ;
(12)删除数据
a、删除表中一条数据
格式:
DELETE from 表名 where 条件 ;
案例:
DELETE from  student2 where id =1 ;
b、删除表中所有的数据
格式:DELETE from  表名
案例:
DELETE from  student2 ;
(13)删除的三种方法
drop >truncate>delete    速度的优先级
a、truncate 删除大批量的数据
truncate student2 ;
b、drop  table 表名;   表名和表数据都删除
c、delete from  一般删除表内数据
(14)单行注释#
多行注释:ctrl+/
取消多行注释ctrl+shift+/
注意选中内容,注释
(15)备份
1、在mysql中备份
a、备份表结构
格式:
create  table 新表名 like 源表名;
案例:
create  table st like student2;
b、备份数据
格式:
INSERT into 新表结构 select *  from  表格 ;
案例:
INSERT into st select *  from  student2 ;
c、备份表和数据
格式:create  table 新表名   as(select  *  from  表名)
案例:create  table st2  as(select  *  from  student2)
                    
                
                
            
        
浙公网安备 33010602011771号