数据库:关系型 和 非关系型数据库
关系型数据库的优势
复杂查询
可以使用SQL语句 在一个或者多个表之间 进行复杂的查询
事物支持
可以提高安全性能
非关系型数据库
性能
NOSQL 是基于键值对 不需要SQL层的解析 性能高
可扩展性
因为基于键值对 所以水平扩展非常的容易
一 进入MySQL数据库
标准写法
mysql -h主机名 -u用户名 -p
输入密码
mysql -hlocalhost -uroot -p
mysql -h127.0.0.1 -uroot -p
本地ip 不走网络 127.0.0.1-127.255.255.254
简写
mysql -uroot -p
二 介绍数据库
MySQL->小数据库->数据表->字段->数据
三 对于数据库的操作
命令:
库和表 create 增 drop删 alter 改 show查
查看所有数据库
show databases;
进入数据库
use 库名
查看当前所在的数据库
select database();
创建数据库
create database 库名;
查看当前所创建的数据库
show create database 库名
创建一个不存在的库
create database if not exists 库名
删除数据库
drop database 库名
drop database if exists 库名;
创建库 并设置字符集
create database python1710 character set utf8
修改库的字符集
alter database python1710 character set utf8;
表的操作
- 创建表
> create table myint(
-> age tinyint, 字段名 字段类型
-> age2 tinyint unsigned 字段名 字段类型 约束条件
-> );
drop table if exists user;
3.查看表
show create table 表名;
Create Table: CREATE TABLE `myint` (
`age` tinyint(4) DEFAULT NULL,
`age2` tinyint(3) unsigned DEFAULT NULL,
`myint` int(10) unsigned DEFAULT NULL,
`money1` float(6,2) DEFAULT NULL,
`money2` decimal(6,2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.01 sec)
修改表结构
修改字符集
alter table 表名 character set 字符集
给表添加新字段
alter table 表名 add 字段名 字段类型 约束条件 after 字段名/first (默认排在最后一位)
修改表字段类型 约束条件
alter table 表名 modify 字段名 类型 约束条件 after z字段名/first (默认排在最后一位)
alter table 表名 modify 字段名 类型 约束条件 character set 字符集 after z字段名/first (默认排在最后一位)
修改字段名
alter table 表名 change 旧字段名 新字段名 类型 约束调教 after z字段名/first (默认排在最后一位)
删除字段名
alter table 表名 drop 字段名
修改表名
alter table 表名 rename 新表名
查看表结构
desc 表名
添加索引(不起索引名)
alter table 表名 add 索引类型(索引字段)
alter table a add index(username);
添加索引(起索引名)
alter table 表名 add 索引类型 索引名称(索引字段)
alter table a add unique uname(username);
查看当前表的所有 索引
show index from 表名
创建一个表b 和a一样
create table b like a
删除索引
alter table 表名 drop key 索引名称
注意:
分号作为命令的结束
命令不区分大小写
数据库,表不能重名
当在命令中 多输入引号以后 所有输入的内容都被认为是引号内的内容 将引号不全 \c撤销当前命令
\c撤销当前命令
数据库的退出 \q/exit/quit
MySQL开启不严谨报错
修改my.ini配置文件
路径:C:\ProgramData\MySQL\MySQL Server 5.7
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
MySQL表的创建
一 字段类型
数值类型
类型 大小 范围(有符号) 范围(无符号) 用途 tinyint 1字节 -128,127 0,,255 最小整数值 smallint 2字节 -32768,32767 0,65535 大整数值 int 4字节 0,4294967295(10位) 大整数值 float 4字节 单精度浮点型 double 8字节 双精度浮点型 decimal 更加精准的小数类型
创建表的语句
实例
> create table myint(
-> age tinyint, 字段名 字段类型
-> age2 tinyint unsigned 字段名 字段类型 约束条件
-> );
注意
int(3) 或 tinyint(2) 并不会影响数值存储的范围 只会影响显示 在配合zerofill零填充的时候
如果不需要0填充的时候 后面的值不需要给
当存储关于钱类型的 对于浮点数要求精确的时候 使用decimal 不会造成精度损耗
2.日期和时间类型
类型 | 大小 | 范围 | 格式 | 用途 |
---|---|---|---|---|
date | 3 | 1000-01-01~999912-31 | YYYY-MM-DD | 日期值 |
time | 3 | -838:59:59~838:59:59 | HH:MM:SS | 时间值 |
year | 1 | 1901-2155 | YYYY | 年份值 |
datetime | 8 | 1000-01-01 00:00:00~9999-12-31 23:59:59 | YYYY-MM-DD HH:MM:SS | 混合日期 |
注意
在存储时间的时候 最好使用时间戳来存储 这样方便我们对时间的计算
3.字符串类型
类型 | 大小 | 用途 |
---|---|---|
char | 0-255 | 存储定长字符串 |
varchar | 0-255 | 存储变长字符串 |
text | 0-65535 | 长文本数据 |
enum('w','m') | 65535个成员 | 枚举:可赋予某个枚举成员 |
set('w','m') | 64个成员 | 集合:可赋予多个集合成员 用逗号隔开 |
实例
mysql> create table if not exists mystring(
-> username varchar(10),
-> password char(6),
-> article text,
-> sex enum('w','m'),
-> hobby set('代码','game','beautiful girl')
-> );
注意
char和varchar 的区别
char的执行相率高于varchar varchar比char更节省空间
char和varchare存储范围都在0-255
enum和set的区别
enum只能选择其中某一个值进行存储
set可以选择 一个到多个值进行存储 如果有重复的值 set会去除重复
二 字段约束
unsigned 无符号 正数
只能适用于 设置数值类型 不能有负号
最大存储长度 增加一倍
zerofill 零填充
只能设置数值类型 当数值长度不足你设定的长度时 会使用0 自动填充到指定的长度
auto_increment 主键自增
用于设置字段值的自动增长 当每增加一条数据的时候 当前的值会自动加1
default 默认值
可以给某个字段 设置一个默认值 当不给当前字段添加值的时候 该字段的值为默认值
null 和 not null
默认为null 当给当前表添加数据的时候 不给某个字段添加值 则当前字段的值为null
如果设置了 not null 那么在添加数据的时候 就必须给当前字段添加值
comment 设置当前字段的说明
注意:
和null进行算数运算结果都为Null
null 意味着没有值 或者是 位置值
三 MySQL的索引
主键索引 primary key
唯一索引 unique
常规索引 index
全文索引 fulltext
(1) 主键索引
主键索引是关系型数据库中最常见一种索引类型 用来记录一条数据的位置
注意:
每个表都最好有一个主键索引 但不是必须指定
一个表只能指定一个主键 主键的值不能为null
主键可以有多个约束条件 比如 auto_increment、not null
每次删除所有的数据 下次在添加数据 会从上次记录的位置继续自增
将主键自增归位
alter table 表名 auto_increment=1;
清空表并将自增归位
truncate 表名
(2) 唯一索引
唯一索引和主键索引相同的地方是 都可以防止创建值的重复 确保数据的唯一性
不同:一个表只能有一个主键索引 但是可以有多个 唯一索引
使用索引的关键字 unique 对应字段去添加
(3) 常规索引
常规索引就是为了提高数据库的性能(查询效率)
缺点:
多占用磁盘空间
会减慢增删改的效率
使用index 或者是 key去创建
创建表 添加索引不起名实例
mysql> create table imgtable(
-> id int unsigned primary key auto_increment not null,
-> username varchar(50) not null,
-> userpass varchar(50) not null,
-> telno varchar(20) not null,
-> sex enum('w','m') not null default 'm',
-> birthday date not null default '0000-00-00',
-> index(username), #索引类型 (索引字段)
-> index(userpass),
-> unique(telno)
-> );
创建表 添加索引起名实例
mysql> create table imgtable2(
-> id int unsigned primary key auto_increment not null,
-> username varchar(50) not null,
-> userpass varchar(50) not null,
-> telno varchar(20) not null,
-> sex enum('w','m') not null default 'm',
-> birthday date not null default '0000-00-00',
-> index iuser(username),#索引类型 索引名称(索引字段)
-> unique upass(userpass)
-> );
四 数据表的存储类型
MyISAM 和 InnoDB 俩种表的类型最为重要
MyISAM不支持事物处理 InnoDB支持
MyISAM不支持外检 InnoDB支持
MyISAM的执行效率高于InnoDB
MyISAM默认会产生3个文件 innoDB只有一个
五 表产生文件的区别
.frm文件: 存储数据表的框架结构 名称与表同名 每个表都会对应一个同名的frm文件
1.MyISAM 数据库表文件
.MYD: 即My Data 表数据文件
.MYI: 即 My Index 索引文件
.log: 日志文件
通常用于对数据库要求不高的网站来使用 比如 贴吧.博客
2.InnoDB 数据库表文件
.ibd: 存储数据库的表数据和索引
通常用于对于数据安全性要求高的 比如 电商
六 InnoDB事物处理
先查看当前的 表的存储引擎是否为 innodb
alter table 表名 engine=innodb
查看是否为自动提交
select @@autocommit #如果为1 则自动提交
将自动提交改为手动提交
set autocommit = 0
开启事物
begin;
进行sql语句的操作
提交或者回滚
commit work;提交
rollback work;回滚
注意:
innodb引擎 当开始事物处理以后 不对他进行提交或者回滚 直接退出 那么数据将不会保存
只有innoDB支持事物处理
七 建表的注意事项
表的字段 使用逗号作为分隔
字段最后一句 不需要加逗号
数据表名 最好不要和字段重名
字段名称 不能使用mysql的关键字
八 数据的增删改查
增 INSERT INTo
删 DELETE FROM
改 UPDATE
查 SELECT
INSERT 添加
指定字段添加值
insert into 表名(字段名1,字段2...) values(值1,值2...)
不指定字段添加值(有多少个字段 就要添加多少个值 一一对应)
insert into 表名 values(值1,值2...)
添加多个值
不指定字段添加多个值
insert into 表名 values(值1,值2...),(值1,值2...)...
指定字段添加多个值
insert into 表名(字段名1,字段2...) values(值1,值2...),(值1,值2...)...
SELECT 查询
主体结构
SELECT 字段 FROM 表名 [WHERE条件] [GROUP BY HAVING][ORDER BY]
不指定字段查询
select * from 表名
指定字段查询
select 字段名1,字段名2... from 表名
给查询字段起别名
select 字段名 别名,字段名 别名 from 表名;
select 字段名 as 别名,字段名 as 别名 from 表名;
WHERE 条件
(1) 比较运算符
>
select * from a where age>72;
<
select * from a where age<72;
>=
select * from a where age>=72;
<=
select * from a where age<=72;
!=/<>
select * from a where id != 1;
select * from a where id <> 1;
=
select * from a where id = 1;
(2) 逻辑运算符
and 逻辑与
select * from a where username='张三' and age=72;
or 逻辑或
select * from a where username='张三' or age=72;
between and 在...之间 包含值的本身
select * from a where id between 3 and 9;
select * from a where id>=3 and id<=9;
not between and 不在...之间
select * from a where id not between 3 and 9;
select * from a where id<3 or id>9;
in 在...里面
select * from a where username in('张三','李四');
not in ...不在...里面
select * from a where username not in('张三','李四');
(3) 子查询 sql的条件还是一条SQL语句
select * from a where id in(select id from user where password=123456);
(4) order by 排序
order by 字段名 asc/desc 升序降序
select * from a order by id desc;
select * from a order by id asc;
select * from a where age>72 order by age;
注意:
不加asc 默认就是升序
order by 要放在所有数据都处理完毕 在将数据排序显示
(5) is not null
因为null是一个特殊的值 不能使用比较运算符操作
select * from a where username is null;
select * from a where username is not null;
(6) limit 取值
limit y 从0取出y条数据
limit y 从0取出y条数据
select * from a order by age desc limit 0,2;
select * from a order by age desc limit 2;
select * from a where age between 112 and 255 and username is not null order by id desc limit 1;
(7) MySQl聚合函数
count(字段名) 统计个数
select count(*) from a;
max(字段名) 最大值
min(字段名) 最小值
sum(字段名) 求和
avg(字段名) 平均数
select count(*) as con,max(age) as mage,min(age) as minage,sum(age) sumage,avg(age) as avgage from a;
(8) GROUP BY 分组
select classid,count(*) as count from a group by classid
select sex,count(*) from a group by sex;
select sex,count(*) as con from a group by sex order by con desc;
select classid,sex,count(*) from a group by classid,sex;
(9) HAVING 条件
select classid,sex,count(*) as con from a group by classid,sex having con>1; 查询人数大于1人
select classid,sex,count(*) as con from a group by classid,sex having con>1 and sex='w'; 查询人数大于1人 并且性别为w
select classid,sex,count(*) as con from a group by classid,sex having classid in('python1708');
(10) LIKE 模糊查询
'%value%' 值包含的就查询
select * from a where username like '%四%';
‘value%’ 以value值作为开头的数据
select * from a where username like '四%';
‘%value’ 以value值作为开头的数据
select * from a where username like '%四';
select * from a where username like '%张%' and age>72 order by age desc limit 2;
DELETE 删除
主体结构
delete from 表名 【where】
注意:
where 如果不加 则删除所有数据
UPDATE 修改
主体结构
update 表名 set 字段名=值【,字段名=值,...】【where】
注意:
where 如果不加 则修改当前字段的所有数据
DISTINCT 去除重复数据
select distinct age from a;
select age from a group by age;
多表联查
(1) 隐式内连接
select 表名.字段名[,表名.字段名...] from 表1,表2[,表3...] where 关联条件
select u.username,g.goodsname from user as u,goods as g where u.id = g.uid and u.username=15611833906
(2) 显示内连接
select 表名.字段名[,表名.字段名...] from 表1 inner join 表2 on 关联条件
select u.username,g.goodsname from user as u inner join goods as g on u.id = g.uid and u.username=15611833906
(3) 左关联
select * from user as u left join goods as g on u.id = g.uid
注意
左关联 以 左表为主表 右表为辅表 会把主表的所有数据都查询出来 辅表没有匹配的数据 使用null来占位
(4) 右关联
select * from user as u right join goods as g on u.id = g.uid
注意
右关联 以 右表为主表 左表为辅表 会把主表的所有数据都查询出来 辅表没有匹配的数据 使用null来占位
以下作为了解
一 修改密码
set password for 用户名@localhost = password('新密码')
二 创建用户
(1) 进入mysql数据库
use mysql
(2) 查询 当前数据库下都有哪些用户
select user from user;
(3) 创建用户
create user 用户名 identifited by '密码'
(4) 赋予权限
grant all on 库名.* to 用户名
(5) 回收权限
revoke all on 库名.* from 用户名
(6) 删除用户
drop user 用户名
(7) 刷新服务
flush privileges;
以上内容来源于同学。