栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mysql语句包含DDL+DQL+DML

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mysql语句包含DDL+DQL+DML

Mysql语句包含DDL+DQL+DML 综合使用查询:
#----综合使用
书写顺序
select distinct * from '表名' where '限制条件'  group by '分组依据' having '过滤条件' order by  limit '展示条数'
执行顺序
from      -- 查询
where      -- 限制条件
group by   -- 分组
having     -- 过滤条件
order by   -- 排序
limit      -- 展示条数
distinct   -- 去重
select     -- 查询的结果
正则:select * from emp where name regexp '^j.*(n|y)$';
集合查询:max 、min 、avg 、sum 、count 、group_concat 。
内连接:inner join
左连接:left join
右连接:right join
全连接: 左连接 union 右连接
replace 替换

拼接:concat、concat_ws、group_concat

注:MySQL不区分大小写,所有符号必须使用英文格式下的 一、DDL(对数据库和表的操作) 1、创建数据库
create database dbname
2、删除数据库
drop database dbname
3、选择数据库
user dbname
4、创建新表
create table tablename(
字段名1 类型,
字段名2 类型,
字段名3 类型,…);
例:CREATE TABLE user(
id INT,
name VARCHAr(10),
sex VARCHAr(1),
stu_age INT;
5、根据已有的表创建新表
A:create table 新表名 like 旧表名(使用旧表创建新表)
B:create table 新表名 as select 列名1,列名2… from 旧表名 definition only
6、删除表 6.1 drop
drop是直接删除信息表,速度快,但是无法找回数据
drop table tablename
6.2 truncate
truncate 是删除表数据,不删除表的结构,速度拍第二,但不能与where一起用
truncate table tablename
6.3 delete
delete是删除表中的数据,不删除表结构,速度最慢,但是可以与where使用,
delete from 表名 where id = 1;
7、显示表的属性信息
desc 表名
8、增加一个列名
alter table 表名  add 列名 类型 ;
指定列名后面添加列名
alter table 表名 add 已有列名 after 列名 类型;
first:第一个; before:在之前; end:最后
9、删除一个列名
alter table 表名 drop 列名
10、修改一个列名
alter table 表名 change 旧列名 新列名 类型
11、修改类型
alter table 表名 modify 旧列名(已有的列名)   新类型 
12、添加主键
alter table 表名 add primary key (列名) references 表名 (主键)
13、添加外键
alter table 表名 add foreign key (列名) references 表名 (主键)
14、约束类型
①  not null:非空约束
②  unique:唯一约束
③  primary key:主键
④  foreign key:外键
⑤  auto_increment:自增长
⑥  default:设置默认值 例:default ‘值’
二、DQL(查询语句) 关键字:
1、or:或
2、and:和
3、having:分组后的附加条件
4、group by:分组
5、order by:排序 :asc :升序,desc:降序
6、in:存在于某个值中
7、not in:不存在与某个值中
8、inner join ……on 内连接多表
9、left join …… on 左连接多表(以左表为基础,匹配右表数据)
10、right join ……on 右连接多表(以右表为基础,匹配左表数据)
11、left (right,inner) out join …… on :去重
12、count:计数
13、AVG:平均值
14、sum:求和
15、max:最大值
16、min:最小值
17、like ‘关键字%’:取含有关键字的值(name like concat('%',#{name},'%'))
18、distinct:去重
19、round:四舍五入
20、where 1=1:条件成功,全匹配
21、where 1=2:条件失败,都不匹配
22、limit 1,5:从第二行,开始显示前5条数据
23、top 10:select top 10 * from 表名:显示前十条数据
24、newid(): 随机
查询语句示例:
1、选择查询
select * from 表名 where 条件
2、模糊查询
select * from 表名 where 列名 like ‘%value%’(查询包含value的值)
3、order by 排序查询(倒叙,默认为正序查询)
select * from  表名  order by  列名  desc
4、count  计数查询
select count(*)  as 别名 from  表名
5、 sum 求和查询
select sum(求和字段)  as  别名 from  表名
6、avg 平均值查询
select avg(平均值字段)  as  别名 from 表名
7、max  min 最大最小值查询
select max(字段)  as  别名   from 表名
8、round 四舍五入查询
select round(min (字段) , 要保留的小数位数)  as  别名  from  表名
9、distinct  去重查询
select distinct  列名 from 表名
10、分组查询:
select * from  表名  group by 列名
11、多条件查询
select *  from 表名  where 列名=‘数值’  and  列名2 = ‘数值2’  or  列名3 = ‘数值3’ 
12、多条件分组求职查询
select 列名, min(filed)  from  表名 where  列名=‘范围’  group by 列名  having  sum(列名)
13、子查询 (子查询的结果作为主查询的条件)
select * from 表名 where  id =  (select  id  from 表名 where 列名 = '数值')
14、连表查询(内连接):
select * from  表名1  inner join  表名2   on   表名1.主键 =表名2.外键 (必须有主外键)
15、连表查询(内连接 + 去重 )
select * from  表名1  inner  out  join  表名2  on  表名1.主键 = 表名2.主键
16、连表查询(左外连接)显示左表的全部信息和右表相关联的信息)
select *  from 表名1  left join  表名2  on  表名1.主键 = 表名2.主键
三、DML(数据操作语句:插入、修改、删除) 1、数据插入
完全插入: insert  into  表名 values(值1,值2,值3……)
选择插入:insert  into 表名  (列名1,列名3,列名9……) values(值1,值3,值9……)
批量插入:insert into 表名 values(值1,值2,值3,值4……),(值1,值2,值3,值4……),(值1,值2,值3,值4……)
2、修改语句
update 表名 set  列名 = '值'  where 条件
3、删除语句
delete  from 表名 where 条件
暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1016766.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号