MYSQLSQL语句知识点总结
一、SQL语句篇
*特别说明:FILED代表数据表字段,CONDITIONS代表where之后的条件,TABLENAME代表数据表名 []中括号内的内容代表 可有可无。
创建数据库
create database DATABASE;
删除数据库
drop database DATABASE
创建数据表
create table TABLENAME(
`id` int unsigned auto_increment primary key, //把id设置为主键,数据类型是无符号int型,自动递增
//注:unsigned 只能紧跟在数据类型 后,顺序不能乱 unsigned相当于去掉数字负数部分 例:tinyint 默认取值范围 -128到127 unsigned 后 取值范围变 0到 255
`userid` int unsigned references FOREIGNTABLE(FOREIGNFILED), //把userid设置为无符号整型,并设置成外键。关联主键表(FOREIGNTABLE)中的主键(FOREIGNFILED)
`imgid` int unsigned not null default 1 comment '图片ID', //把imgid 设置成无符号整数 且不为空 默认值为1 注释说明为 “图片ID”
foreign key(imgid) references FOREIGNTABLE(FOREIGNFILED) //设置 imgid 为外键。 关联主键表(FOREIGN_TABLE)中的主键(FOREIGN_FILED) (下同)
)engine=MYISAM default charset=utf8 collate=utf8_general_ci //设置数据表引擎为MYISAM ,默认字符集为utf8 且字符集排序规则为 utf8_general_ci
删除数据表
drop table if exists TABLENAME;
添加字段
alter table TABLENAME add colum FIELD DATATYPE ATTRIBUTES //DATATYPE :字段数据类型 ATTRIBUTES字段属性
eg: alter table test ADD COLUMN ceshi varchar(50) not null default '' comment '测试字段'
删除字段
alter table TABLENAME drop colum FIELD ;
eg:alter table test DROP COLUMN ceshi;
修改字段属性
alter table TABENAME modify FIELD char(10) default 0 COMMENT "这是整数";
修改字段名称
alter table TABENAME change FIELD mytest int unsigned not null default 1;
添加默认值
eg: alter table test alter column user_id set default 1;
删除默认值
alter table TABENAME alter column FIELD drop default;
设置主键
alter table TABENAME add primary key(FIELD1 [ ,FIELD2,.. ] );
删除主键
alter table TABLENAME drop primary key; //注意:如果表里的主键是具有自增长属性的;那么直接删除是会报错的
添加唯一索引
alter table TABLENAME add constraint KEY_NAME unique(FIELD); //KEY_NAME :索引名称 (下同)
添加外键索引
alter table TABLENAME add constraint KEY_NAME foreign key(FIELD) references FOREIGN_TABLE(FOREIGN_FIELD) ;
删除索引(外键、唯一)
alter table TABLENAME drop index KEY_NAME
或
alter table TABLENAME drop key KEY_NAME
检查约束 :注意,MYSQL 目前并不支持check检查约束。可以右一下方法,设置字段为枚举值
eg: alter table TABENAME modify sex ENUM("男","女");
修改表的存储引擎
alter table TABENAME engine=INNODB
修改数据表的自增长值
alter table TABENAME AUTO_INCREMENT=100;
添加数据
insert [ into ] TABLENAME (FIELD1,FELD2,...) values(值1,值2,值3,...) ,(值1,值2,值3,...),...
修改数据
update TABLENAME set FILED1=值1,FEILD2=值2,.... where CONDITISONS
删除数据
delete from TABLENAME [ where CONDITIONS ];
清空数据
truncate table TABLENAME; 自增ID会重置。
查询数据表所有
select * from TABLENAME
统计查询(查询结果集中的数据条数)
select count(FIELD) from TABLENAME [ where CONDITIONS]
查询某个字段的最大、最小、平均、求和的值
select max(FIELD) from TABLENAME [where CONDITIONS];
select min(FIELD) from TABLENAME [where CONDITIONS];
select avg(FIELD) from TABLENAME [where CONDITIONS];
select sum(FIELD) from TABLENAME [where CONDITIONS];
排序查询
select * from TABLENAME [ where CONDITIONS] order by FIELD ; 从小到大排
select * from TABLENAME [ where CONDITIONS] order by FIELD desc; 从大到小排
限制查询
select * from TABLENAME [ where CONDITIONS] [ order by FIELD ] limit START,NUM //START代表数据集的开始位置,0为第一条数据的位置,依次往后为数据的位置。NUM代表限制数据集的数据条数。至少1条。
去重查询
select distinct FILED1,FIELD2,... from TABLENAME [ where CONDITIONS] [ order by FIELD ] [ limit START,NUM ] //代表FIELD1,FIELD2,....所有字段都重复时,才去除重复的数据条。
分组查询 ( group by FIELD)
示例::select name, first_letter,sum(parent_id) as cid from mm_city where `parent_id` = 18 group by first_letter having cid>50 limit 0,1 //顺序不能乱。当有统计字段别名做条件时,不能用where,只能用 having。
模糊查询
select * from TABLENAME where FIELD like '%值%'; // % 代表任意多个任意字符
内连接查询
select TABLENAME1.FIELD1,TABLENAME2.FIELD2,TABLENAME3.FIELD3,... from TABLENAME1
join TBALENAME2 on TABLENAME1.FIELD = TABLENAME2.FIELD
( join TBALENAME3 on TABLENAME1.FIELD = TABLENAME3.FIELD )
或
( join TBALENAME3 on TABLENAME2.FIELD = TABLENAME3.FIELD )
[ where TABLENAME1.FIELD = 值 ... ] //查询或作为条件的字段中,若所有表中任意两张表都有该字段,则必须要指明数据表。即在字段前用 表名连上点(.)
外连接
左联接:left join ;查询出来的数据,若有空值,则以在left join前面的表中的数据条数为准
select TABLENAME1.FIELD1,TABLENAME2.FIELD2,TABLENAME3.FIELD3,... from TABLENAME1
left join TBALENAME2 on TABLENAME1.FIELD = TABLENAME2.FIELD
(left join TBALENAME3 on TABLENAME1.FIELD = TABLENAME3.FIELD )
或
(left join TBALENAME3 on TABLENAME2.FIELD = TABLENAME3.FIELD )
[ where TABLENAME1.FIELD = 值 ... ] //查询或作为条件的字段中,若所有表中任意两张表都有该字段,则必须要指明数据表。即在字段前用 表名连上点(.)
右联接:rightjoin ;查询出来的数据,若有空值,则以在left join后面的表中的数据条数为准
select TABLENAME1.FIELD1,TABLENAME2.FIELD2,TABLENAME3.FIELD3,... from TABLENAME1
right join TBALENAME2 on TABLENAME1.FIELD = TABLENAME2.FIELD
(right join TBALENAME3 on TABLENAME1.FIELD = TABLENAME3.FIELD )
或
(right join TBALENAME3 on TABLENAME2.FIELD = TABLENAME3.FIELD )
[ where TABLENAME1.FIELD = 值 ... ] //查询或作为条件的字段中,若所有表中任意两张表都有该字段,则必须要指明数据表。即在字段前用 表名连上点(.)
常用数据库函数
LENGTH :返回字符串或列的数据的长度
eg: select length(city) as citylen from Demo
lower/upper:返回字符串的小写/大写
eg: select UPPER(account) from admininfo;
REPLACE :替换字符串
eg: select REPLACE('SQL SERVER','SQL','sql') 结果是'sql SERVER'
POWER() 取数值的幂值
eg:select POWER(5,3) 结果是125
ABS 返回绝对值
eg:select ABS(-99) 结果是99
ROUND 根据指定精度返回数值的四舍五入
eg: select ROUND(3.1415926,3) 结果是3.142
数据库对象
创建存储过程
drop procedure if exists pr_multi; //如果存在名为pr_multi的存储过程,则删掉
create procedure pr_multi( out c int, a int, b int ) //创建名为pr_multi的存储过程(函数),第一个为数据类型int的输出参数,第二、三个分别为数据类型int的输入参数
begin //过程体开始标记
if a is null THEN //判断 a 参数是否为空,若果为空,
set a=10; //给 a赋值10
end if;
if b is null THEN //判断 b 参数是否为空,若果为空,
set b=20; //给 b 赋值20
end if;
set c=a*b; //将参数c设置为 a 和 b 的乘积
end //过程体结束标记
call pr_multi(@name,5,3); //调用存储过程pr_multi 用变量 name 接收输出参数 c ,给参数 a 传值 5,b传值 3
select @name //查询变量name的值。。 上述结果为 15
- 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 实例讲解
- 一看就懂的Tensorflow实战(TensorBoard高级篇)
- 一看就懂的Tensorflow实战(LSTM)
- 给我半首歌的时间,给你说明白Immutable List
- 一看就懂的Tensorflow实战(GAN)
- 一看就懂的Tensorflow实战(DCGAN)
- VBA解压缩ZIP文件08——解压-没有压缩
- 一文教你搞懂C语言的Q格式
- 基础算法之排序算法
- C++核心准则E4,5:设计并构建不变量
- Selenium实际应用注入并执行Javascript语句
- 什么是Python的 “内存管理机制”
- 2020年手工webpack构建react项目,完美支持ssr,包括css和图片资源
- php中赋值、浅拷贝与深拷贝
- 2020最新:100道有答案的前端面试题(下)
- UI自动化测试之ddt实战