Mysql是一个轻量型数据库,开源免费。Oracle是收费的并且价格很是高。mysql
Mysql一个实例能够操做多个库,而Oracle一个实例只能对应一个库。正则表达式
Mysql安装完后300M而Oracle有3G左右。sql
主键:Mysql通常使用自动增加类型,而Oracle则须要使用序列对象。数据库
单引号的处理:mysql里能够用双引号包起字符串,oracle里只能够用单引号包起字符串。api
分页的sql语句:mysql用limit,而oracle使用内建视图和rownum伪列。缓存
事务处理:mysql默认是自动提交,而oracle默认不自动提交,须要用户CTL语言进行事务提交。服务器
oracle装好后会有一个oracle实例还有一个库,库当中有数据文件,这数据文件在oracle中称为表空间。oracle
因此在Oracle装好之后,咱们首先要去建立一个永久表空间,再去建立用户。随后把这个永久表空间分配给这个用户。函数
接着再去建立一个用户,再给他分配一个表空间。经过表空间来实现物理隔离。性能
因此说在oracle中库有一个就够了,而后咱们再给他建立表空间。
mysql是一个实例能够对应多个库,mysql当中呢没有表空间这个概念,因此说咱们能够去建立不一样的库,而后用户直接去操做不一样的库。每一个库中放着不一样的数据文件。
create database 数据库名 default character set字符编码;
建立一个test的数据库,并查看该数据库,以及该数据库的编码。
create database test default character set utf-8;
建立数据库:
create database 库名;
查看数据库
show databases;
查看数据库编码:
select schema_name ,default_character_set_name from information_schema.schemata where schema_name='test';
drop database 数据库名;
1.2.1.1示例
drop database test;
须要在哪一个库中建立表须要先选择该数据库。
use 须要选择的库名;
建立一个名称为test的数据库,编码为utf-8;
create database test default character set utf8;
选择该数据库;
use test;
Mysql支持全部准备sql数值数据类型。
做为sql标准的扩展,Mysql也支持整数类型tinyint、mediumint和bigint。
Mysql数据类型 含义
tinyint(m) 一个字符 范围(-128- 127)
smallint(m) 2个字节 范围(-32768- 32767)
mediumint(m) 3个字节 范围(-8388608- 8388687)
int(m) 4个字节 范围(-2147483648- 2147483647)
bigint(m) 8个字节 范围(+-9.22*10的18次方)
数值类型中的长度m是指显示长度,并不显示存储长度,只有字段指定指定zerofill时有用
例如: int(3),若是实际值是2,若是列指定了zerofill,查询结果就是002,左边用0来填充。
float(m,d) 单精度浮点型 8位精度(4个字节) m总个数,d小数位
double(m,d) 双精度浮点型 16位精度(8位) m总个数,d个小数位
、
char(n) 固定长度,最多255个字符
varchar(n) 可变长度,最多65535个字符
tinytext 可变长度,最多255个字符
text 可变长度,最多65535个字符
mediumtext 可变长度,最多2的24次方-1个字符
longtext 可变长度,最多2的32次方-1个字符
1.char(n)若存入字符数小于n,则以空格补于其后,查询之时再将空格去掉。因此char类型存储的字符串末尾不能有空格,varchar不限制于此。
2.char类型的字符串检索要比varchar类型快。
1.varchar能够指定n,text不能指定,内部存储varchar是存入的实际字符数+1个字节(n<=255),text是实际字符数+2个字节。
2.text类型不能有默认值。
3.varchar可直接建立索引,text建立索引要指定前多个字符。varchar查询速度快于text,在都建立索引的状况下,text的索引彷佛不起做用。
mysql数据类型 含义
date 日期2008-12-2
time 时间‘12:25:36’
datetime 日期时间‘2008-12-2 22:06:44’
timestamp 自动存储记录修改时间
1.BLOB和TEXT存储方式不一样,text以文本方式存储,英文存储区分大小写,而Blob是以二进制方式存储,不分大小写。
2.BLOB存储的数据只能总体读出。
3.text能够指定字符集,BLOB不一样指定字符集。
4.1建立表
create table employees(employee_id int,last_name varchar(30),salary float(8,3))
4.2查看表
show tables;
4.3删除表
drop table employees;
alter table 旧表名 rename 新表名
将employees表名修改成emp。
alter table employees rename emp;
alter 表名 change column 旧列名 新列名 类型
将emp表中的last_name 修改成name
alter table employees change column last_name name varchar(30)
alter table 表名 modifity 列名 新类型
将emp当中的name长度指定为49;
alter table employees MODIFY name varchar(40);
alter table 表名 add column 新列名 类型
5.4.1示例
在emp表中添加一个新的lie为commission_pct
alter table employees add column commission_pct float(4,2)
5.5使用ddl来删除列
alter table 表名 drop column 列名
5.5.1示例
删除emp表中的commission_pct
alter table emp drop column commsission_pct;
查询表的约束信息
show keys from table;
查询表中的约束信息
show keys from 表名
建立employees表包含employees _id该列为主键且自动增加,last_name列不容许含有空值,email列不容许有重复不容许有空值,dept_id为外键参照departments表的主键。
create table employees(
employees_id int primary key auto_increment,
last_name varchar(30) not null,
email varhcar(40) not null unique,
dept_id int,
constraint emp_fk foreign key(dept_id)referenes departments(department_id);
)
6.3.1主键约束
6.3.1.1添加主键约束
alter table 表名 add primarykey(列名)
6.3.1.1.1示例
将emp表中的employee_id修改成主键自动增加
添加主键:alter table emp add primary key(employee_id);
添加自动增加:alter table emp modify_id auto_increment;
alter table 表名 drop primary key
注意:删除主键时,若是主键列具有自动增加能力,须要先去掉自动增加,而后在删除主键。
例子:
删除employee_Id的主键约束。
去掉自动增加:alter table emp modify employee_id int;
删除主键:alter table emp drop primary key;
6.3.2.1添加非空约束
alter table 表名 modify 列名 类型 not null;
向emp表中的salary添加非空约束
alter table emp modify salary float(8,2) not null,
alter table 表名 modify 列名 类型 null
向emp表中的name添加惟一约束
alter table add constraint emp_uk unique(name);
alter table 表名 drop key 表名。
alter table emp drop key emp_uk;
alter table 表名 add constraint 约束名 foreign key(列名)
refrences 参照的表名(操做的列名)
alter table add constraint e_fk foreign key(dept_id) refrences departments(department_Id);
删除外键:
alter table 表名 drop foreign key 约束名
删除外键索引(索引名与约束名同名)
alter table表名 drop index 索引名。
6.3.4.2.1示例
删除dept_id的外键约束
删除外键: alter table emp drop foreign key e_fk;
删除索引: alter table emp drop index e_fk;
7.1.1.1选择插入
insert into 表名(列名1,列名2....)values(值1,值2,值3...);
insert into 表名 values(值1,值2,值3.....)
insert into 表名 (...)values
(值1,值2,值3.....),
(值1,值2,值3.....),
(值1,值2,值3.....);
一个表中只能有一个列为自动增加。
自动增加的列的类型必须是整数类型。
自动增加只能添加到具有主键约束与惟一性约束的列上。
删除主键约束或者惟一约束,若是该列拥有自动增加能力,则须要去掉自动增加而后删除约束。
Create table emp2(id int primary key ,name varchar(30),seq_num int unique auto_increment);
在MySQL中可使用default为字段设定一个默认值。若是在插入数据时并未指定该列的值,那么MySQL会将默认值添加到该列中。
create table emp3(emp_id int primary key auto_increment ,name varhcar(30),address varchar(50) default 'unknown');
update 表名 set 列名=值,列名=值 where 条件
跟新的表不能在set和where中用于子查询;
update后面能够作任意的查询。
跟新emp3中id为2的数据,将地址修改成id为1用户相同
Oracle: update emp3 e set e.address=(select address from emp3 where emp_id=1)where e.emp_id=2;
mysql: update emp3 e,(select address from emp3 where emp_id=1)t set e.address=t where e.emp_id=2;
方式二:
update emp3 e set e.address=(select t1.address from(select * from emp3) t1 where t1.emp_id=1)
delete from 表名 where 条件
删除emp3表中emp_id为1的雇员信息。
truncate table 表名
删除emp3表中的全部数据
truncate table emp3;
在mysql中默认状况下,事务是自动提交的,也就是说,只要执行一条DML语句就开启了事务,而且提交了事务。
start transaction (此后的数据须要本身手动提交)
DML.....
commit|rollback
8.1.1示例
向emp3表中添加一条数据,要求手动提交事务。
select *|投影列from 表名
查询全部
select * from departments;
select *|投影列 from 表名 where 选择条件。
select department_name,location_Id from departments where department_id=4;
+:加法运算
-:减法运算
*:乘法运算
/:除法运算,返回商
%:求余运算,返回余数。
示例一
修改employees表添加salary。
alter table employees add column salary float(9,2);
示例二
select employees_id,last_name,email,12*salary from employees;
3.1大小写控制函数
LOWER(str) 转换大小写混合的字符串为小写
UPPER(str) 转换大小写混合的字符串为大写
CONCAT(str1,str2) 将str一、str2等字符串链接起来
SUBSTR(str,pos,len) 从str的第pos位(范围:1-str.length)开始,截取长度为len的字符串
length(str) 获取str的长度
instr(str,substr)
Lpad(str,len,padstr) 获取substr在str中的位置
trim(str) 从str中删除开头和结尾的空格(不会处理字符串中间含有的空格)
Ltrim(str) 从str中删除左侧开头的空格
Rtrim(str) 从str中删除右侧结尾的空格
REPLACE(str,from_str,to_str) 将str中的from_str替换为to_str(会替换掉全部符合from_str的字符串)
ROUND(arg1,arg2):四舍五入指定小数的值
ROUND(arg1):四舍五入保留整数
TRUNC(arg1,arg2):截断指定小数的值,不作四舍五入。
MOD(arg1,arg2):取余
3.4日期函数
SYSDATE()或者NOW() 返回当前系统时间,格式为YYYY-MM-DD-hh-mm-ss
CURDATE() 返回系统当前日期,不返回时间
CURTIME() 返回当前系统中的时间,不返回日期
DAYOFMONTH(date) 计算日期 d是本月的第几天。
DAYOFWEEK(date) 日期d今天是星期几 1星期日
dayofyear(date)
dayname(date)
LAST_DAY(date) 返回date日期当月的最后一天。
3.5转换函数
date_format(date,format) 将日期转换成字符串(相似oracle中的to_char())
str_to_date(str,format) 将字符串转换成日期(相似oralce中的to_date())
format的格式都列出来:
%M 月名字(January……December)
%W 星期名字(Sunday……Saturday)
%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)
%Y 年, 数字, 4 位
%y 年, 数字, 2 位
%a 缩写的星期名字(Sun……Sat)
%d 月份中的天数, 数字(00……31)
%e 月份中的天数, 数字(0……31)
%m 月, 数字(01……12)
%c 月, 数字(1……12)
%b 缩写的月份名字(Jan……Dec)
%j 一年中的天数(001……366)
%H 小时(00……23)
%k 小时(0……23)
%h 小时(01……12)
%I 小时(01……12)
%l 小时(1……12)
%i 分钟, 数字(00……59)
%r 时间,12 小时(hh:mm:ss [AP]M)
%T 时间,24 小时(hh:mm:ss)
%S 秒(00……59)
%s 秒(00……59)
%p AM或PM
%w 一个星期中的天数(0=Sunday ……6=Saturday )
%U 星期(0……52), 这里星期天是星期的第一天
%u 星期(0……52), 这里星期一是星期的第一天
%% 字符% )
https://www.jb51.net/article/135803.htm
select date_format(sysdate,'%Y年%月%d日')
select str_to_date('2019年03月23日','%Y年%m月%d日');
3.6示例一
insert into empoyess values(default,'King','King@sxt.cn',190000,0.6,str_to_date('2018年5月1日','%Y年%m月%d日'))
ifnull(expr1,expr2)
if(expr1,expr2,expr3)
coalesce(value...)判断value的值是否为null,若是不为null,则返回value;若是为空,则判断下一个value是否为空..直到出现不为空的value并返回或者返回最后一个为null的value。
示例
查看雇员king所在部门名称
select department_name from employees e,departments d where e.dept=d.department_id and e.last_name='king'
4.2.1示例一
建立sal_level表,包含lowest_sal,highest_sal ,level.
create table sal_level(lowest_sal int ,highest_sal int ,level VARCHAR(30));
插入多条数据
insert into sal_level values(1000,2999,'A')
select e.last_name from employee e,sal_level s where e.salary between s.lowest_sal and highest_sal;
select emp.last_name from employees emp ,employees man where emp.manager_id=man.employees_id;
Mysql5.7支持SQL99标准。
使用交叉链接查询employees表与department表
select * from employees cross join departments
使用天然链接查询全部部门的雇员的名字以及部门名称。
select e.last_name,d.department_name from employees natural join departments d where e.last_name='oldlu';
若两个表有多个列相同,则都作链接条件。
6.3.1示例
查询雇员名字为oldlu的雇员id,薪水与部门名称。
select e.employees_id,e.salary,d.department_name from employees e inner join departments d on e.department_id=d.department_id where e.last_name='Oldlu';
对分组数据作平均值运算
arg:参数类型只能是数字类型
select avg(e.salary) from employees e;
对分组数据求和
arg:参数类型只能是数字类型
select sum(salary) from employees;
求分组中最小数据。
arg:参数类型能够是字符、数字、日期
select imn(salary) from employees;
求分组中最大的数据。
arg:参数类型能够是字符、数字、日期。
返回一个表中的行数
COUNT 函数有三种格式:
count(*)
count(expr)
count(distinct expr)
计算每一个部门的平均薪水
select avg(e.salary) from employees e group by e.department_id;
显示那些最高薪水大于5000的部门的部门号和最高薪水。
select e.department_id,max(e.salary) from employees e group by e.department_id having max(e.salary)>5000;
能够将子查询放在许多的sql子句中,包括:
谁的薪水比oldru高
select em.last_name ,em.salary from empoyees em where em.salary>(select e .salary from employees e where e.last_name='Oldlu');
= 等于
> 大于
>= 大于或等于
< 小于
<= 小于或者等于
<> 不等于
查询oldlu的同事,可是不包含他本身。
select empl.last_name from employees empl
where empl.department_id=
(select e.department_id from employees e where e.last_name='oldru')
and empl.last_name<>'Oldlu';
操做 含义
in 等于列表中的任何成员
any 比较子查询返回的每一个值
all 比较子查询返回的所有值
示例:
查找各个部门收入最低的那些雇员。显示他们的名字,薪水以及部门id。
select em.last_name ,em.salary,em.department_Id from employees em where em.salary in(select min(e.salary) from employees group by e.department_id);
mysql中容许使用正则表达式定义字符串搜索条件,性能高于like。
mysql中的正则表达式能够对整数类型或者字符类型检索。
使用REGEXP关键字表示正则匹配。
默认忽略大小写,若是要区分大小写,使用BINARY关键字
模式 | 什么模式匹配 |
^ | 字符串的开始 |
$ | 字符串的结尾 |
. | 任何单个字符 |
[...] | 在方括号内的任何字符列表 |
[^...] | 非列在方括号内的任何字符 |
p1|p2|p3 | 交替匹配任何模式p1,p2或者p3 |
* | 零个或者多个前面的元素 |
+ | 前面的元素的一个或多个实例 |
{n} | 前面的元素的n个实例 |
{m,n} | m到n个实例前面的元素 |
^在正则表达式中表示开始
查询以x开头的数据(忽略大小写)
select 列名 from 表名 where 列名 REGEXP '^X';
查询雇员表中名字以k开头的雇员名字与薪水
10.3.1语法
查询以x结尾的数据(忽略大小写)
select 列名 from 表名 where 列名 REGEXP 'x$';
10.3.2示例
查询雇员表中名字以n结尾的雇员名字与薪水。
select last_name ,salary from employees where last_name REGEXP binary 'n$';
英文的点,它匹配任何一个字符,包括回车、换行等。
select 列名 from 表名 where 列名REGEXP 'x';
查询雇员表中名字含有o的 雇员的姓名与薪水。
select last_name,salary from employees where last_name REGEXP'O.';
“*”:星号匹配0个或者多个字符,在它以前必须有内容。
“+”:加号匹配1个或者多个字符,在它以前也必须有内容。
select 列名 from 表名 where 列名 REGEXP 'x+'; 匹配大于1个的任意字符。
“?”:问号匹配0次或者1次
select 列名 from 表名 where 列名 REGEXP 'x?'; 匹配0个或者1个字符
"|":表示或者含义
select 列名 from 表名 where 列名 REGEXP ‘abc|bcd’ ; 匹配包含abc或者bcd
查询雇员表中名字含有ke或者lu的雇员的名字与薪水。
select last_name,salary form employees where last_name REGEXP'ke|lu';
“[a-z]”:字符范围
“^[...]”:以什么字符开头的
"[^...]":匹配不包括在[]的字符
select 列名 from 表名 where 列名 REGEXP '[a-z]'; 匹配内容包含a-z范围的数据。
查询雇员表中名字包含x、y、z字符的雇员的名字和薪水。
select last_name ,salary from employees where last_name regexp '[x-z]';
select last_name ,salary from employees where last_name regexp 'x|y|z';
查询雇员名字是t、f开头的雇员名字与薪水。
select last_name ,salary from employees where last_name regexp '^[t|f]';
查询雇员的名字与薪水,不包括oldlu.
select last_name ,salary from employees where last_name regexp '[^oldlu]';
“{n}”:固定次数
select * from student where name REGEXP's{2}';----匹配以s连续出现2次的全部的数据
10.10.2示例一
查询雇员名字含有连续两个e的雇员的姓名与薪水
select last_name,salary from employees where last_name REGEXP'e{2}';
10.10.3示例二
查询名字含有两个o的雇员的名字与薪水。
select last_name,salary from employees where last_name REGEXP'o.{2}';
“{n,m}":范围次数
select * from student where name REGEXP '^s{2,5}';---匹配以s开头且重复2到5次的全部数据
10.11.2示例
查询雇员名字中包含1个或者两个o的雇员姓名与薪水。
select last_name ,salary from employees where last_name REGEXP 'o.{1,2}';
是最基本的索引,它没有任何限制。
在建立索引时,能够指定索引长度。length为可选参数,表示索引的长度,只有字符串类型的字段才能指定索引长度,若是是BLOB和TEXT类型,必须指定length。
建立索引时须要注意:
若是指定单列索引长度,length必须小于这个字段所容许的最大字符个数。
查询索引: show index from table_name;
create index index_name on table(column(length))
为emp3表中的name建立一个索引,索引名为emp3_name_index;
create index emp3_index on emp3(name);
alter table table_name add index index_name (column(length))
修改emp3表,为address列添加索引,索引名为emp3_address_index
alter table emp3 add index emp3_address_index(address)
create table 'table'(
column type,
primary key(id);
index index_name(column(length))
)
建立emp4表,包含emp_id,name,address列,同时为name列建立索引。索引名为emp4_name_index
create table emp4(
emp_id int primary key auto_increment,
name varchar(30),
address varchar(50),
index emp4_name(name)
)
drop index inde_name on table
删除mep3表中索引名为emp3_address_index的索引。
drop index emp3_address_index on table;
create unique index indexname on table(column(length))
为emp表中的name建立一个惟一索引,索引名为emp_name_index
create unique index emp_name_index on emp(name);
alter table table_name add unique indexName(column(length))
修改emp表,为address列添加惟一索引,索引名为emp_address_index
alter table emp add unique emp_salary(salary);
create table table(
column type,
primary key(id),
unique index_name(column(length))
)
主键索引是一种特殊的惟一索引,一个表只能有一个主键,不容许有空值。通常是在建表的时候同时建立主键索引。
1.4.1修改表添加主键索引
alter table 表名 add primary key(列名)
1.4.1.1示例
修改emp表为employee_id添加主键索引
alter table emp add primary key(employee_id)
1.4.2建立表时指定主键索引
组合索引是指使用多个字段建立的索引,只有在查询条件中使用了建立索引时的第一个字段,索引才会被使用(最左前缀原则)
就是最左优先。
如:咱们使用表中的name,address,salary建立组合索引,那么想要组合索引生效,咱们只能使用以下组合:
name/address/salary
name/address
name/
若是使用address/salary或者是salary则索引不会生效。
alter table table_name add index index_name(column(length),column(length))
修改emp6表,为name,address列建立组合索引
alter table emp6 add index emp6_index_n_a (name,address);
create table table(
column type,
index index_name(column(length),column(length))
)
1.5.3.1示例
建立emp7表,包含emp_id,name,address列,同时为name,address列建立组合索引。
create table emp7(emp_id int primary key auto_increment ,name varchar(20),address varchar(30),index emp_index7_n_a(name,address))
全文索引(FULLTEXT INDEX)主要用来查找文本中的关键字,而不是直接与索引中的值相比较。fulltext索引跟其余索引不大相同,它更像是一个搜索引擎,而不是简单的where语句的参数匹配。fulltext索引配合match against 操做使用,而不是通常的where语句加like。
全文索引能够从char、varchar或者text列中做为create table语句的一部分被建立,或是随后使用alter table 添加。不过切记对于大容量的数据表,生成全文索引是一个很是消耗时间很是消耗硬盘空间的作法。
alter table table_name add fulltext index_content(content)
修改emp7表添加content列类型为text
alter table emp7 add column contemt text;
修改emp7,为content列建立全文索引
alter table emp7 add fulltext emp_content_fullindex(content)
create table(
column type,
fulltext index_name(column)
)
建立emp8包含emp_id列,content列该列类型为text,并为该列添加名为emp8_content_fulltext的全文索引。
create table emp8(emp_Id int primary key auto_increment,
content text ,
fulltext emp8_content_fullindex(content))
drop index index_name on table
alter table table_name drop index index_name;
1.6.3示例
删除emp8表中名为emp8_content_full的索引
drop index emp8_cotent_fullindex on emp8
全文索引的使用与其余索引不一样。在查询语句中须要使用match(column)against('content')来检索数据。
1.7.1全文解析器
全文索引中基本单位是“词”。分词,全文索引是以词为基础的,mysql默认的分词是全部非字母和数字的特殊符号都是分词符。在检索数据咱们给定的检索条件也是词。
mysql中默认的全文解析器不支持中文分词。若是数据含有中文须要更换全文解析器NGRAM。
1.7.2使用全文索引
select 投影列 from表名 where match(全文列名) against('搜索内容')
示例二
向emp8表中插入一条数据content的值为"hello,bjsxt";
insert into emp8 values(default,"hello bjsxt");
示例三
查询emp8表中内容包含bjsxt的数据
select * from emp8 where match(content)AGAINST("bjsxt");
在建立全文索引时能够指定ngram解析器
alter table table_name add fulltext index_content(content) with parser ngram
1.7.3.1示例一
删除emp8表中的emp8_content_full全文索引
drop index emp8_content_Full on emp8
1.7.3.2示例二
修改emp8表,为content列添加名称emp8_content_full的全文索引,并指定ngram全文解析器。
alter table emp8 add fulltext emp8_content_full(content) with parser ngram
1.7.3.3示例三
向emp8表中添加一条数据content 值为“ 你好,诗圣杜甫”
insert into emp8 values(default,'你好,诗圣杜甫');
1.7.3.4示例四
查询emp8表中内容包含“诗圣杜甫”
select * from emp8 where match(content) against('诗圣杜甫');
mysql分页查询原则
1.1语法格式
select 投影列from 表名 where 条件 order by limit 开始位置,查询数量。
1.1.1示例
查询雇员表中全部数据按id排序,实现分页查询,每次返回两条结果。
select * from employees order by employees_id limit 0,2;
select 投影列 from 表名 where 条件 ordfer by limit 查询数量 offset 开始位置。
2.1.1示例
查询雇员
select * from employees order by employee_id limit 2 offset 0;
在mysql中能够经过explain关键字模拟优化器执行sql语句,从而知道mysql是如何处理sql语句的。
explain select * from employees;
3.启动执行计划