MySQL基础

发布时间:2022-07-05 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了MySQL基础脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

数据库的操作

1、创建数据库
create database 数据库名;
create database 数据库名 character set 字符集;
2、查看全部的数据库
show databases;   

3、查看某个数据库的定义的信息:

show create database 数据库名;         
4、删除数据库
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、数据类型与约束

    • int :整数 有符号和无符号,长度不受限制
    • vArchar:字符串 中文/数字/字母都是一字字符,受长度限制
    • decimal :小数 decimal(5,2)总长度5位,整数3位,小数2位
    • 主键 id :int unsigned Primary key auto_increment
    • 非空 :not null
    • 唯一 :unique
    • 默认 :default

SQL中表的操作

1、创建表

create table 表名(字段名 类型 约束,.....)

2、删除表

drop table 表名
drop table if exists 表名

3、表数据操作

3.1、添加数据
insert into 表名 values(值1,值2...)给表中所有字段插入数据
insert into 表名(字段2,字段1)values(值2,值1)
3.2、修改数据
update 表名 set 字段1=值1,字段2=值2 where 条件
isdelete 逻辑删除
3.3、删除数据
delete From 表名 where 条件
3.4、查询数据
  • 3.4.1、查询表中所有信息
    select * from 表名 where 条件
    
  • 3.4.2、查询表中指定字段
    select 字段1,字段2,字段3...from 表名 where 条件
    
  • 3.4.3、给字段取别名
    select 字段1 (as) 别名,字段2 别名 from 表名 where 条件
    
  • 3.4.4、去重
    select distinct 字段 from 表名 where 条件
    
  • 3.4.5、比较运算
    select * from 表名 where age>20
    
  • 3.4.6、逻辑运算符
    • and 满足所有条件

    • or 满足其中任意一个条件

    • not 不满足条件

  • 3.4.7、模糊查询
    • like
    • % 0到任意多个字符
    • _表示任意一个字符
  • 3.4.8、范围查询
    • in 在一个非连续的范围内
    • between and 在一个连续的范围内 闭区间 小的数据在前
  • 3.4.9、空查询
    • is null
    • ‘’空字符串
3.5、多表查询
  • 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
    
  • 3.5.9、子查询
    • 标量子查询

    子查询结果输出的是一行一列

     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
    
3.6、数据分表
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,请注明来意。