创建数据库
create database 数据库名称;
删除数据库
drop database 数据库名称;
创建数据表
create table 表名称(
字段名1 字段类型,
字段名2 字段类型
);
删除数据表
drop table 表名称;
插入单条数据
insert into 表名
values(数据1,数据1,数据1,数据1,数据1,数据1);
插入多条数据
insert into 表名
values(数据1,数据1,数据1,数据1,数据1,数据1),
(数据2,数据2,数据2,数据2,数据2,数据2),
(数据3,数据3,数据3,数据3,数据3,数据3);
修改更新数据
update 表名 set 字段名='修改数据' where 字段名='指定字段数据';
删除数据
delete from 表名 where 字段名称='指定需要的数据';
模糊查询
XXX:数据
select * from 表名 where 字段名称 like 'XXX%';
select * from 表名 where 字段名称 like '%XXX';
select * from 表名 where 字段名称 like '%XX%';
范围查询:查询一定范围的数据
select* from 表名 where 字段名称 between 数据开始 and 数据结束;
范围查询:查询指定范围的数据
select* from 表名 where 字段名称 in(数据1,数据2,数据3);
范围查询:查询非指定范围的其他数据
select* from 表名 where 字段名称 not in(数据1,数据2,数据3);
统计
max:最大 min:最小 avg:平均 sum:求和 count:总数
select max(字段名称) from 表名;
select min(字段名称) from 表名;
select avg(字段名称) from 表名;
select sum(字段名称) from 表名;
select count(*) from 表名 where 字段名称='数据';
倒序排序
倒序排序:desc
select * from 表名 order by 字段名 desc;
顺序排序
顺序排序:asc
select * from 表名 order by 字段名 asc;
去重
distinct:去重
select distinct(字段名称) from 表名;
查询开始前几条数据
select * from 表名 limit 需要指定查看前几条;
查询指定开始到结束指定范围数据
limit
select * from 表名 limit 开始范围,结束范围;
分组查询
group by
select 字段名称 from 表名 group by 字段名称;
分组过滤
select 字段名称 from 表名 group by 字段名称 having sum(字段名称)>数据;
执行顺序
from...where...groip by...having...select...order by...
内连接查询
inner join
select * from 表名1 inner join 表名2 on 表名1.字段1=表名2.字段2;
左连接查询
left join
select * from 表名1 left join 表名2 on 表名1.字段1=表名2.字段2;
右连接查询
right join
select * from 表名1 right join 表名2 on 表名1.字段1=表名2.字段2;
嵌套查询
select (select 字段名1 from 表名1 where 字段名2='数据'),字段名3 from 表名2;
UNION
union: 去除重复的数据
select 数据字段1 from 表名1
union
select 数据字段2 from 表名2;
UNION ALL
union all: 不会去除重复的数据
select 数据字段1 from 表名1
union all
select 数据字段2 from 表名2;
索引:普通索引
add index
alter table 表名 add index 索引名称(列名称);
索引:唯一索引
dd unique
alter table 表名 dd unique 索引名称(列名称);
索引:删除索引
drop index 索引名称 on 表名;
表复制
create table 复制新表名称 like 旧表名称;
数据复制
insert into 复制新表名称 select * from 旧表名称;
字符函数concat()
concat():两个或多个字符串组合成一个字符串
如:concat(str1,str2,...)
实例:select concat(str1,str2,...) from 表名;
字符函数length()
length():获取字符串的长度
实例:select length(str1) from 表名;
字符函数replace
replace():搜索并替换字符串中的子字符串
select replace(字段名,'需要替换数据','替换后数据') from 表名;
字符函数substring
substring():特定长度的位置开始提取一个字符串
select substring(字段名,x,y) from 表名;
注:x=从第几位取,y=取多少位
时间函数curdate()
curdate():返回当前时间
select curdate();
时间函数now()
now():返回当前日期和时间
select now();
时间函数year()
year():返回日期中的年份
select year(字段名) from 表名;
时间函数month()
month():返回日期中的月份
select month(字段名) from 表名;
时间函数day()
day():返回日期中的天数
select day(字段名) from 表名;
数据处理函数rand()
rand():返回0-1之间的随机小数
select rand();
数据处理函数floor()
floor():小数取整
select floor(小数);
主键
1、主键必须包含唯一值。如果主键由多个列组成,则这些列中的值的组合必须是唯一的
2、主键列不能包含NULL值。这意味着必须使用NOT NULL属性声明主键列。如果没有指定NOT NULL, MySQL将强制为主键列为NOT NULL
3、一张表只有一个主键,主键字段的数据类型必须为整数类型
主键:PRIMARY KEY
外键
1、外键表示一个表中的一个字段被另一个表中的一个字段引用,外键可以在数据库层面保证数据的完整性
外键:
本文全面介绍了SQL语言的基础操作,包括数据库和数据表的创建与删除,数据的插入、修改、删除,以及各种查询技巧,如模糊查询、范围查询、统计分析、排序、去重等。此外,还详细讲解了索引的创建与删除、表和数据的复制、字符和时间函数的使用,以及主键和外键的概念。
7万+

被折叠的 条评论
为什么被折叠?



