mysql 1

关系型:如sqllite,db2,oracle,access,sql server,MySQL,注意:sql语句通用
非关系型:mongodb,redis,memcache
MySQL是一个关系型数据库管理系统
MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件。
MySQL所使用的 SQL 语言是用于访问数据库的最经常使用标准化语言。MySQL 软件采用了双受权政策,分为社区版和商业版,
因为其体积小、速度快、整体拥有成本低,
尤为是开放源码这一特色,通常中小型网站的开发都选择 MySQL 做为网站数据库。
mysql -uroot -p set password = password('root'); # 给当前数据库设置密码 create user 'eva'@'%' #指示全部机器均可以链接 show grants for 'eva'@'192.168.10.5';查看某个用户的权限 mysql -uroot -p123 -h 192.168.10.3 #远程登陆 grant all on *.* to 'eva'@'%'; #受权 flush privileges; # 刷新使受权当即生效 grant all on *.* to 'eva'@'%' identified by '123'  # 建立帐号并受权

SQL : 结构化查询语言(Structured Query Language)html

SQL语言主要用于存取数据、查询数据、更新数据和管理关系数据库系统,SQL语言由IBM开发。mysql

一、DDL语句    定义: 数据库、表、视图、索引、存储过程,create drop alter正则表达式

二、DML语句   操纵: insert delete update selectredis

三、DCL语句    控制: 例如控制用户的访问权限 grant revoke(废除 撤销)sql

1. 操做文件夹(库) 增:create database db1 charset utf8; 查:show databases; 改:alter database db1 charset latin1; 删除: drop database db1; 2. 操做文件(表) 先切换到文件夹下:use db1 增:create table t1(id int,name char); 查:show tables; 改:alter table t1 modify name char(3); alter table t1 change name name1 char(2); 删:drop table t1; 3. 操做文件中的内容(记录) 增:insert into t1 values(1,'egon1'),(2,'egon2'),(3,'egon3'); 查:select * from t1; 改:update t1 set name='sb' where id=2; 删:delete from t1 where id=1; 清空表: delete from t1; #若是有自增id,新增的数据,仍然是以删除前的最后同样做为起始。 truncate table t1;数据量大,删除速度比上一条快,且直接从零开始, *auto_increment 表示:自增 *primary key 表示:约束(不能重复且不能为空);加速查找

mysql 存储引擎

InnoDB、MyISAM、MEMORY 、BLACKHOLE(黑洞)mongodb

 
  

InnoDB数据库

 
  

用于事务处理应用程序,支持外键和行级锁。若是应用对事物的完整性有比较高的要求,在并发条件下要求数据的一致性,数据操做除了插入和查询以外,还包括不少更新和删除操做,那么InnoDB存储引擎是比较合适的。InnoDB除了有效的下降由删除和更新致使的锁定,还能够确保事务的完整提交和回滚,对于相似计费系统或者财务系统等对数据准确要求性比较高的系统都是合适的选择。缓存

 
  

MyISAM安全

 
  

若是应用是以读操做和插入操做为主,只有不多的更新和删除操做,而且对事务的完整性、并发性要求不高,那么能够选择这个存储引擎。架构

 
  

Memory

 
  

将全部的数据保存在内存中,在须要快速定位记录和其余相似数据的环境下,能够提供极快的访问。Memory的缺陷是对表的大小有限制,虽然数据库由于异常终止的话数据能够正常恢复,可是一旦数据库关闭,存储在内存中的数据都会丢失。

存储引擎相关sql语句

查看当前的默认存储引擎:
show variables like "default_storage_engine";
查询当前数据库支持的存储引擎
show engines \G;

指定存储引擎建表

在建表时指定 

create table ai(id bigint(12),name varchar(200)) ENGINE=MyISAM;

create table country(id int(4),cname varchar(50)) ENGINE=InnoDB;

也可使用alter table语句,修改一个已经存在的表的存储引擎。
alter table ai engine = innodb;

在配置文件中指定

#my.ini文件
[mysqld]
default-storage-engine=INNODB

MySQL架构 四层

1 链接处理、受权认证、安全等

2 MySQL的核心服务。包括:查询解析、分析、优化、缓存以及全部的内置函数(例如:日期、时间、数学和加密函数)

3 存储引擎。存储引擎负责MySQL中数据的存储和提取。

4 文件系统,全部的表结构和数据以及用户操做的日志最终仍是以文件的形式存储在硬盘上。

查看表结构 desc 表名; show create table 表名\G;

mysql 支持的数据类型

https://www.cnblogs.com/Eva-J/articles/9683316.html

表的完整性约束

https://www.cnblogs.com/Eva-J/articles/9687915.html

主键  外键  联级更新  联级删除

#表类型必须是innodb存储引擎,且被关联的字段,即references指定的另一个表的字段,必须保证惟一 create table department( id int primary key, name varchar(20) not null )engine=innodb; #dpt_id外键,关联父表(department主键id),同步更新,同步删除 create table employee( id int primary key, name varchar(20) not null, dpt_id int, foreign key(dpt_id) references department(id) on delete cascade # 连级删除 on update cascade # 连级更新 )engine=innodb; #先往父表department中插入记录 insert into department values (1,'教质部'), (2,'技术部'), (3,'人力资源部'); #再往子表employee中插入记录 insert into employee values (1,'yuan',1), (2,'nezha',2), (3,'egon',2), (4,'alex',2), (5,'wusir',3), (6,'李沁洋',3), (7,'皮卡丘',3), (8,'程咬金',3), (9,'程咬银',3) ; #删父表department,子表employee中对应的记录跟着删 mysql> delete from department where id=2; Query OK, 1 row affected (0.00 sec) mysql> select * from employee; +----+-----------+--------+
| id | name      | dpt_id |
+----+-----------+--------+
|  1 | yuan      |      1 |
|  5 | wusir     |      3 |
|  6 | 李沁洋    |      3 |
|  7 | 皮卡丘    |      3 |
|  8 | 程咬金    |      3 |
|  9 | 程咬银    |      3 |
+----+-----------+--------+
6 rows in set (0.00 sec) #更新父表department,子表employee中对应的记录跟着改 mysql> update department set id=2 where id=3; Query OK, 1 row affected (0.01 sec) Rows matched: 1  Changed: 1  Warnings: 0 mysql> select * from employee; +----+-----------+--------+
| id | name      | dpt_id |
+----+-----------+--------+
|  1 | yuan      |      1 |
|  5 | wusir     |      2 |
|  6 | 李沁洋    |      2 |
|  7 | 皮卡丘    |      2 |
|  8 | 程咬金    |      2 |
|  9 | 程咬银    |      2 |
+----+-----------+--------+
6 rows in set (0.00 sec)

修改 表结构

alter table 表名 rename 新表名;

alter table 表名 add  字段名 数据类型 [完整性约束条件…];

alter table 表名 drop 字段名

alter table 表名 modify 字段名 数据类型 [完整性约束条件…];

alter table 表名 change 旧字段名 新字段名 旧数据类型(完整约束条件);

alter table 表名 change 旧字段名 新字段名 新数据类型(完整约束条件)

修改字段排列顺序/在增长的时候指定字段位置 ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件…] FIRST; ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件…] AFTER 字段名; ALTER TABLE 表名 CHANGE 字段名 旧字段名 新字段名 新数据类型 [完整性约束条件…] FIRST; ALTER TABLE 表名 MODIFY 字段名 数据类型 [完整性约束条件…] AFTER 字段名;
alter table staff modify sex enum('male','female') after sname; alter table staff modify id int(4) primary key auto_increment; 删除主键 须要先去掉主键的自增约束,而后再删除主键约束 alter table staff modify id int(11); alter table staff drop primary key; alter table staff add primary key (sname,age); alter table staff drop primary key; alter table staff add primary key (id); alter table staff modify id int(4) auto_increment;
-----多对一------ create table press( id int primary key auto_increment, name varchar(20) ); create table book( id int primary key auto_increment, name varchar(20), press_id int not null, foreign key(press_id) references press(id) on delete cascade on update cascade );
更新数据update update 表名 set 字段1=值1,字段2=值2,where condition; 实例 UPDATE mysql.user SET password=password(‘123’) where user=’root’ and host=’localhost’; ------------------------------------------------------ 删除数据 delete delete from 表名 where condition; 例子 DELETE FROM mysql.user WHERE password=’’;

单表查询

-------语法---------- SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数
------
关键字执行的优先级--------------
from > where > group by > having > select > distinct > order by > limit
--简单查询-- https://www.cnblogs.com/Eva-J/articles/9688313.html
---where约束---
1. 比较运算符:> < >= <= <> !=
2. between 80 and 100 值在10到20之间 3. in(80,90,100) 值是10或20或30 4. like 'egon%' pattern能够是%或_, %表示任意多字符 _表示一个字符 5. 逻辑运算符:在多个条件直接可使用逻辑运算符 and or not ---- group by ---------- 单独使用GROUP BY关键字分组 SELECT post FROM employee GROUP BY post; 注意:咱们按照post字段分组,那么select查询的字段只能是post,想要获取组内的其余相关信息,须要借助函数 GROUP BY关键字和GROUP_CONCAT()函数一块儿使用 SELECT post,GROUP_CONCAT(emp_name) FROM employee GROUP BY post;#按照岗位分组,并查看组内成员名 SELECT post,GROUP_CONCAT(emp_name) as emp_members FROM employee GROUP BY post; GROUP BY与聚合函数一块儿使用 select post,count(id) as count from employee group by post;#按照岗位分组,并查看每一个组有多少人 ---------------------------------------------------------- 若是咱们用unique的字段做为分组的依据,则每一条记录自成一组,这种分组没有意义 多条记录之间的某个字段值相同,该字段一般用来做为分组的依据 ----------------------------------------------------------- 聚合函数 #强调:聚合函数聚合的是组的内容,如果没有分组,则默认一组 SELECT COUNT(*) FROM employee; SELECT COUNT(*) FROM employee WHERE depart_id=1; SELECT MAX(salary) FROM employee; SELECT MIN(salary) FROM employee; SELECT AVG(salary) FROM employee; SELECT SUM(salary) FROM employee; SELECT SUM(salary) FROM employee WHERE depart_id=3; -------------------------------------------------------------- HAVING过滤 #!!!执行优先级从高到低:where > group by > having #1. Where 发生在分组group by以前,于是Where中能够有任意字段,可是绝对不能使用聚合函数。 #2. Having发生在分组group by以后,于是Having中可使用分组的字段,没法直接取到其余字段,可使用聚合函数 ------------------------------------------------------------ ORDER BY 查询排序 按单列排序 SELECT * FROM employee ORDER BY salary; SELECT * FROM employee ORDER BY salary ASC; SELECT * FROM employee ORDER BY salary DESC; 按多列排序:先按照age排序,若是年纪相同,则按照薪资排序 SELECT * from employee ORDER BY age, salary DESC; ------------------------------------------------------------- LIMIT 限制查询的记录数 SELECT * FROM employee ORDER BY salary DESC LIMIT 3; #默认初始位置为0 SELECT * FROM employee ORDER BY salary DESC LIMIT 0,5; #从第0开始,即先查询出第一条,而后包含这一条在内日后查5条 SELECT * FROM employee ORDER BY salary DESC LIMIT 5,5; #从第5开始,即先查询出第6条,而后包含这一条在内日后查5条 ----------------------------------------------------------- 使用正则表达式查询 SELECT * FROM employee WHERE emp_name REGEXP '^ale'; SELECT * FROM employee WHERE emp_name REGEXP 'on$'; SELECT * FROM employee WHERE emp_name REGEXP 'm{2}'; 小结:对字符串匹配的方式 WHERE emp_name = 'egon'; WHERE emp_name LIKE 'yua%'; WHERE emp_name REGEXP 'on$';

多表查询

#重点:外连接语法 SELECT 字段列表 FROM 表1 INNER|LEFT|RIGHT JOIN 表2 ON 表1.字段 = 表2.字段;
#示例1:之内链接的方式查询employee和department表,而且employee表中的age字段值必须大于25,即找出年龄大于25岁的员工以及员工所在的部门 select employee.name,department.name from employee inner join department on employee.dep_id = department.id where age > 25; #示例2:之内链接的方式查询employee和department表,而且以age字段的升序方式显示 select employee.id,employee.name,employee.age,department.name from employee,department where employee.dep_id = department.id and age > 25 order by age asc;

子查询

#1:子查询是将一个查询语句嵌套在另外一个查询语句中。
#2:内层查询语句的查询结果,能够为外层查询语句提供查询条件。
#3:子查询中能够包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等关键字
#4:还能够包含比较运算符:= 、 !=、> 、<等
带IN关键字的子查询 ----------------------------------------------- #查询平均年龄在25岁以上的部门名 select id,name from department where id in (select dep_id from employee group by dep_id having avg(age) > 25); #查看技术部员工姓名 select name from employee where dep_id in (select id from department where name='技术'); #查看不足1人的部门名(子查询获得的是有人的部门id) select name from department where id not in (select distinct dep_id from employee); ----------------------------------------------- 带比较运算符的子查询 #比较运算符:=、!=、>、>=、<、<=、<> #查询大于全部人平均年龄的员工名与年龄 mysql> select name,age from emp where age > (select avg(age) from emp); +---------+------+
| name | age |
+---------+------+
| alex | 48 |
| wupeiqi | 38 |
+---------+------+
2 rows in set (0.00 sec) #查询大于部门内平均年龄的员工名、年龄 select t1.name,t1.age from emp t1 inner join (select dep_id,avg(age) avg_age from emp group by dep_id) t2 on t1.dep_id = t2.dep_id where t1.age > t2.avg_age; ----------------------------------------------- 带EXISTS关键字的子查询 -------------------------- EXISTS关字键字表示存在。在使用EXISTS关键字时,内层查询语句不返回查询的记录。 而是返回一个真假值。True或False 当返回True时,外层查询语句将进行查询;当返回值为False时,外层查询语句不进行查询 -------------------------- #department表中存在dept_id=203,Ture select * from employee ->     where exists ->         (select id from department where id=200); #department表中存在dept_id=205,False select * from employee ->     where exists ->         (select id from department where id=204);

mysql 导入表

从init.sql文件中导入数据 ------------------------------- #准备表、记录 mysql> create database db1; mysql> use db1; mysql> source /root/init.sql 注意 Windows 下的 斜杠问题
相关文章
相关标签/搜索