MySQL数据库基础笔记
数据库
数据库就是存储和管理数据的仓库,用户可以对数据库中的数据进行增删改查等操作。
数据库的分类
-
关系型数据库(Oracle、MySQL、SQLite等)
-
非关系型数据库(Redis、MongoDB等)
MySQL简介
MySQL是一个关系型数据库,由MySQLAB公司开发,目前属于Oracle旗下。
特点:开源、支持大型数据库、使用标准SQL、适用于多种操作系统以及提供多种编程语言接口。
安装(Ubuntu中):
sudo apt-get install mysql-server
sudo apt-get install mysql-client
MySQL客户端连接服务端:
mysql -uusername -ppassword
退出:exit、quit
常用数据类型
int、bit、decimal、varchar、char、date、time、datetime、enum、text
常用数据约束
主键primary key、非空not null、唯一unique、默认default、外键foreign key
数据库设计三范式
-
原子性
-
满足1,表必须有主键,非主键字段必须完全依赖于主键
-
满足2,非主键必须直接依赖于主键
E-R模型
实体-关系模型,E-R模型就是描述数据库存储数据的结构模型
-
一对一
-
一对多
-
多对多
常用SQL语句
-
基本语句
-
查看所有数据库 show databases;
-
创建数据库 use database_name;
-
使用数据库 use database_name;
-
查看当前使用的数据库 selecet database();
-
删除数据库 drop database database_name;
-
查看当前库中所有表 show tables;
-
建表 create table table_name(id int unsigned primary key auto_increment, name varchar(30) not null, age tinyint unsigned not null);
-
修改表
-
添加字段 alter table table_name add sexy bit not null;
-
修改字段类型 alter table table_name modify sexy tinyint unsigned not null;
-
修改字段名 alter table table_name change sexy gender tinyint unsigned not null;
-
删除字段 alter table table_name drop gender;
-
设置外键 alter table table_name add foreign key(cls_id) references classes(id);
-
删除外键 alter table table_name drop foreign key cls_id;
-
-
查看建表(库)SQL语句 show create table(database) table_name(database_name);
-
删除表 drop table table_name
-
-
表数据的基本增删改查:
-
增
-
insert into table_name values();
-
insert into table_name(name) values('a');
-
insert into table_name values(),(),();
-
-
删:delete from table_name where id=33;
-
逻辑删除,设置一个bit类型字段表示是否删除
-
-
改: update table_name set name='c', age=18 where id=222;
-
查
-
select * from table_name;
-
select name,age from table_name;
-
-
-
as关键字(起别名)
-
select t.name,t.age from table_name as t;
-
-
distinct关键字(去重)
-
select distinct name from table_name;
-
-
where条件查询
-
select * from table_name where id<10 and age>18;
-
-
模糊查询
-
select * from table_name where name like '郭%' or name like '郭威_';
-
-
范围查询
-
select * from table_name where id between 5 and 10 or id in (3, 19);
-
-
空判断查询
-
select * from table_name where gender is null;
-
ifnull(age,18)判断字段是否为空,为空则使用提供值
-
-
排序查询
-
select * from table_name order by age desc;
desc表示倒序排序,ase升序排序,默认升序
-
-
分页查询
-
select * from table_name limit start,count;
-
-
聚合函数查询
count()、max()、min()、sun()、avg()、round(avg(age),保留小数位)
-
select avg(age) from table_name where gender=0;
-
-
分组查询
-
select gender,avg(gender) from table_name group by gender with rollup;
with rollup 在最后新增一行查询的记录
-
select gender,count(*) from table_name group by gender having count(*)>5;
having 过滤分组数据,只用于group by
-
-
连接查询
-
内连接
-
select name from table1 inner join table2 on table1.age = table2.age;
-
-
左连接
-
left join
-
-
右连接
-
right join
-
-
自连接
-
select a.id,a.name from table as a inner join table as b where a.pid=b.id;
-
-
-
子查询
-
select * from table where age > (select avg(age) from table);
性能较差
-
-
将查询结果插入其他表
-
insert into country(name) select hero_country from hero group by hero_country;
-
-
连接更新
-
update hero h inner join country c on h.country=c.name set h.country=c.id;
-
-
建表同时添加数据
-
create table country(id int unsigned primary key auto_increment,name varchar(30) not null)select hero_country from hero group by hero_country;
-
事务
事务就是一系列sql操作作为一个单元执行,要么全部执行,要么全部不执行。
-
事务的四大特性
-
原子性
-
一致性
-
隔离性
-
持久性
-
-
MySQL中使用事务需选择InnoDB存储引擎,其他引擎不支持事务
-
开启事务
-
begin;
-
start transaction;
-
set autocommit=0;
MySQL默认自动提交,该设置关闭默认提交功能
-
-
提交事务
-
commit;
-
-
回滚事务
-
rollback;
-
索引
-
创建索引 alter table table_name add index name_index(name);
-
联合索引 alter table table_name add index name_index(name,age);
-
删除索引 alter table table_name drop index name_index;
引擎
原文地址:https://www.cnblogs.com/mumuxin-gv/p/11882603.html
- SpringMVC+Hibernate +MySql+ EasyUI实现CRUD(一)
- 【Python3-API】通用文字识别示例代码
- Python入门教程之安装MyEclipse插件和安装Python环境
- AutoFlowLayout-多功能流式布局与网格布局控件
- RBAC新解:基于资源的权限管理(Resource-Based Access Control)
- 基于开源项目搭建属于自己的技术堆栈
- Redis整合Spring项目搭建实例
- SpringMVC+Hibernate +MySql+ EasyUI实现POI导出Excel(二)
- Nginx+Tomcat+Redis负载均衡Session共享实现超级简单(CentOS6.9系统 Java版本)
- Apache Ignite——新一代数据库缓存系统
- 微信JSSDK接入Java版--步骤及问题处理和解决
- 微信企业号回调模式配置讲解 Java Servlet+Struts2版本 echostr校验失败解决
- Android Material Design系列之RecyclerView和CardView
- 在Linux安装ASP.Net Core的运行时(Runtime)
- MySQL 教程
- MySQL 安装
- MySQL 管理与配置
- MySQL PHP 语法
- MySQL 连接
- MySQL 创建数据库
- MySQL 删除数据库
- MySQL 选择数据库
- MySQL 数据类型
- MySQL 创建数据表
- MySQL 删除数据表
- MySQL 插入数据
- MySQL 查询数据
- MySQL where 子句
- MySQL UPDATE 查询
- MySQL DELETE 语句
- MySQL LIKE 子句
- mysql order by
- Mysql Join的使用
- MySQL NULL 值处理
- MySQL 正则表达式
- MySQL 事务
- MySQL ALTER命令
- MySQL 索引
- MySQL 临时表
- MySQL 复制表
- 查看MySQL 元数据
- MySQL 序列 AUTO_INCREMENT
- MySQL 处理重复数据
- MySQL 及 SQL 注入
- MySQL 导出数据
- MySQL 导入数据
- MYSQL 函数大全
- MySQL Group By 实例讲解
- MySQL Max()函数实例讲解
- mysql count函数实例
- MYSQL UNION和UNION ALL实例
- MySQL IN 用法
- MySQL between and 实例讲解
- axios POST提交数据的三种请求方式写法
- Json对象和Json字符串的区别
- 合并/拆分 Excel?Python、VBA轻松自动化
- (数据科学学习手札96)在geopandas中叠加在线地图
- 秋天的第一杯奶茶该买哪家?Python 爬取美团网红奶茶店告诉你
- AttributeError: NoneType object has no attribute‘’
- tf.control_dependencies(control_inputs)
- tf.get_collection()
- tf.reduce_mean()
- tf.placeholder函数说明
- tf.sigmoid()用法
- Latex 表格过大(或过小)的调整方法
- Latex设置字体大小及表格中的文字大小等
- jQuery 选择器
- jQuery 介绍 以及基本使用