脚本宝典收集整理的这篇文章主要介绍了MySQL基础,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
create database 数据库名;
create database 数据库名 character set 字符集;
show databases;
3、查看某个数据库的定义的信息:
show create database 数据库名;
drop database 数据库名称;
5、切换数据库:
use 数据库名;
6、查看正在使用的数据库:
select database();
7、查看数据库中的所有表:
show tables;
8、查看表结构:
desc 表名;
9、修改表删除列.
ALTER TABLE 表名 drop 列名;
10、修改表名
rename table 表名 to 新表名;
11、修改表的字符集
alter table 表名 character set 字符集;
12、数据类型与约束
create table 表名(字段名 类型 约束,.....)
drop table 表名
drop table if exists 表名
insert into 表名 values(值1,值2...)给表中所有字段插入数据
insert into 表名(字段2,字段1)values(值2,值1)
update 表名 set 字段1=值1,字段2=值2 where 条件
isdelete 逻辑删除
delete From 表名 where 条件
select * from 表名 where 条件
select 字段1,字段2,字段3...from 表名 where 条件
select 字段1 (as) 别名,字段2 别名 from 表名 where 条件
select distinct 字段 from 表名 where 条件
select * from 表名 where age>20
and 满足所有条件
or 满足其中任意一个条件
not 不满足条件
3.5.1、排序
select * from 表名 where 条件 order by 列1 (asc)|desc,列2 asc|desc
select * from student where 条件 order by convert(字段 using gbk)asc|desc 纯中文转换后排序
3.5.2、聚合函数
count: 总数 select count(*/字段)from 表名 where 条件
max: 最大值 select max(age) from 表名 where 条件
min: 最小值 select min(age) from 表名 where 条件
avg: 平均值 select avg(grade) from 表名 where 条件
sum: 求和 select sum(grade) from 表名 where 条件
select avg(age)as 平均年龄,max(age)最大年龄,min(age)最小年龄 from 表名 where 条件
3.5.3、分组
select * from 表名 group by 字段,字段2 having 条件
3.5.4、分页
select * from 表名 limIT 0,5 从第一行数据开始,显示5行
select * from 表名 limit (n-1)*m,m n代表第几页,m代表每页显示多少条数据
3.5.5、等值连接
方式一 :
select * from 表1,表2,表3 where 表1.列=表2.列 and 表2.列=表3.列 where 条件
方式二 内连接 取交集:
select * from 表1 inner join 表2 on 表1.列=表2.列 inner join 表3 on 表2.列=表3.列 where 条件
3.5.6、左连接
左边的表全显示,右边表能匹配的上的数据连接显示,匹配不上(没有的)以null补充
select * from 表1 left join 表2 on 表1.列=表2.列 left join 表3 on 表2.列=表3.列 where 条件
3.5.7、右连接
右边的表全显示,左边表能匹配上的数据连接显示,匹配不上(没有的)以null补充
select * from 表1 right join 表2 on 表1.列=表2.列 right join 表3 on 表2.列=表3.列 where 条件
3.5.8、自关联
select * from 表 别名1 inner join 表 别名2 on 别名1.aid=别名2.pid
子查询结果输出的是一行一列
select * from student where age>(select avg(age) from student)
子查询的结果输出的是一列多行
in:
select * from student where age in
(select age from student where age=18)
any/some: >:大于最小的; <: 小于最大的
select * from student where age>/< any
(select age from student where age between 18 and 22)
all: >:大于最大的; <: 小于最小的
select * from student where age>/<all
(select age from student where age between 18 and 22)
子查询的结果输出的是一行多列
select * from student where(name,sex)=(select name,sex from student where sex='男' order by age desc limit 1)
子查询的输出结果是一个表
select * from scores inner join (select cno from courses where cname in('数据库',‘系统测试’) as c on scores.cno=c.cno
insert into goods_cate(cate_name) select distinct cate from goods;
create table goods_cate (id int unsigned PRimary key auto_incremen,cate VARchar(10)) select distinct cate from goods;
update goods inner join goods_cate on goods_cate.cate=goods.cate set goods.cate=goods_cate.id
以上是脚本宝典为你收集整理的MySQL基础全部内容,希望文章能够帮你解决MySQL基础所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。