MySQL

69 阅读5分钟
  • 存储引擎 InnoDB MyISM
  • mysql约束符
  1. auto increment 自增
  2. primary key 主键
  3. not null 不为空
  4. unique 唯一性
  5. default 默认
  6. foreign key 外键
  7. engine 设置引擎

外键

CREATE TABLE table1 (
    column1 datatype NOT NULL PRIMARY KEY,
    column2 datatype NOT NULL
);
CREATE TABLE table2 (
    column2 datatype NOT NULL PRIMARY KEY,
    column3 datatype NOT NULL,
    FOREIGN KEY (column3) REFERENCES table1(column2)
);

修改表

alter table 旧的表名 rename 新的表名

修改字段类型

alter table 表名 modify 字段 字段类型(个数)

修改字段名

alter table 表名 change 旧字段 新字段

添加字段

alter table 表名 add 字段 字段类型

删除字段

alter table 表名 drop 字段

修改字段顺序

alter table 表名 modeify 字段 字段类型 first|alter 数据2

修改存储引擎

alter table 表名 engine=存储引擎

添加约束

alter table 表名 add constraint 约束字段 约束符号

插入语句

insert into 表名(字段,字段,字段) values(值1,值2,值3)

insert into 表1(字段1,字段2,字段3)select 字段1,字段2 from 表2

删除语句

delete from 表 where 条件

修改

update 表名 set 字段=值

查询语句

select * from 表名
其中*不仅仅是对某张表的某个字段进行查询,还有对某个字段以某种格式作为格式进行查询输入,其中* 不是查询条件而是查询输出格式

执行顺序:where>group by>select>order by>limit

----单表查询

查询所有

select * from 表名

指定字段查询

select 数据1,数据2 from 表名

查询空值

select * from 表名 where 数据 is null

查询不重复

select distinct * from 表名

范围查询

select * from 表名 where 字段 between 数据1 and 数据2

字符匹配查询(模糊查询)

select * from 表名 like '%数据的第一个字'

查询前3条数据(分页查询)

select * from 表名 limit 数据=3

条件查询

select * from 表名 where 数据 运算符 需要的条件

多条件查询

  • and 符合所有条件
  • or 符合其中一个条件
  • in in关键字和or功能相同,但比or块
  • slect * from 表名 where 字段 in(数据,数据)

排序查询

select * from 表名 order by 排序条件

聚合查询

count() max() min() avg() sum()

分组查询

select * from 表名 group by 数据

分组查询条件

select * from 表名 group by 数据 having 条件 ---多表查询

内连接查询

select * from 表1 inner join 表2 on 连接条件

外连接查询

select * from 表1 outer join 表2 on 连接条件

左连接查询

select * from 表1 left join 表2 on 连接条件

右连接查询

select * from 表1 right join 表2 on 连接条件

复合条件连接查询

select * from 表1 inner join|right join|left join|outer join on 连接条件 where 查询条件

子查询

from查询

select * from(查询条件) as 给表起别名 where 查询条件

where 查询

in关键字

select * from 表1 where 数据 in (select * from 表2 where 查询条件)

on关键字

select * from 表1 where 数据 on(select * from 表2 where 查询条件)

any关键字

select * from 表1 where 数据 any(select * from 表2 where 查询条件)

some关键字

select * from 表1 where 数据 some(select * from 表2 where 查询条件)

all关键字

select * from 表1 where 数据 all(select * from 表2 where 查询条件)

exists

select * from 表1 where 数据 exists(select * from 表2 where 查询条件) 合并结果

union关键字(字段数量要保持一致,会删除重复)

select * from 表1 union select * from 表2

union all关键字(字段数量要保持一致,不会删除重复)

select * from 表1 union all select * from 表2

运算符 --算数运算符

  • 加 -减
  • 乘法 / DIV() 除法 % 求余MOD() --比较运算符 = 等于 <小于

大于
<=小于等于
=大于等于
<>(!=)不等于
between and 判断一个值是否在两个值中间
is null 判断一个值是否是空值
in 判断一个值是否在一个集合种
like 判断一个值是否包含某个字符
regexp() 正则表达式
--逻辑运算符
&& (AND)与 满足所有条件
||(or) 或 满足其中一个条件
!(not) 非 不满足所有条件
xor 异或

常用函数

数值函数

  • ABS(x) 绝对值
  • MOD(x,y)求余
  • cell(x) 返回大于X的最小整数
  • floor(x) 返回小于x的最大整数
  • round(x) 随机数
  • truncate(x,y) 对X截取,保留y位数字

字符串处理函数

  • concat() 合并字符串
  • insert() 替换字符串
  • insert(str,x,y,instr) 字符串str从x位置开始替换为instr个y个长度
  • replace(str,a,b)使用字符串b替换str中字符串a
  • lower()字符串转小写,upper()字符串转大写
  • lpad(str1,n,str2),rpad(str1,n,str2) 字符串str2对str1填充直到达到n的长度
  • ltrim(str),删除左边空白rtirm(str),删除右边空白trim(str)删除空白
  • repeat(str,n)返回str重复n次结果
  • locate(str1,str)返回字符串的开始位置
  • reverse(str)翻转字符串

日期处理函数

  • cordate() 获取当前日期
  • cortime() 获取当前时间
  • now() 获取当前日期与时间
  • unx_timestamp(date) 获取date的时间戳
  • year()年 week()周day()天hour()时minutm()分second()秒‎
  • data_format(d,format)按照format时间格式输出
  • adddate(date,interval 日期) date根据日期数据进行加法运算
  • subdate(date,interval 日期) date根据日期数据进行减法运算
  • datediff()计算相差天数
  • time_to_sec() 将时间换算位秒
  • sec_to_time() 将秒换算位时间

举例

select adddate('2009-01-01',interval 2 year) as date1 结果 date1: 2011-01-01

as语法

as 可以个表起一个别名,也可以给字段起一个别名,也可作为输出结果

条件判断函数

  • if(expr,v1,v2) 如果expr为真,返回v1,否则返回v2
  • ifnull(v1,v2) 如果v1 不为null,返回v1,否则返回v2
  • case when expr1 then r1 [when expr2 then r2][else rn] end 如果expr1为真,则返回r1,如果表达式expr2为真,则返回r2,如果expr2都不为真,则返回rn
  • case expr when v1 then r1 [when v2 then r2][else rn] end 如果expr等于v1则返回r1,如果expr等于v2则返回r2,都不等于则返回rn

数据库备份与恢复

导入文件 load data infile '本地的数据路径' into table 表名

导出文件 select * from 表 into outfile "本地数据路径"

未完待定