创建数据库相关

- 显示数据库

  1. show databases;

- 创建数据库

  1. creat database 数据库名;

- 删除数据库

  1. drop database 数据库名;

表的相关内容

- 创建表

  1. creat table 表明(属性名 数据类型 [完整性约束条件],<br /> 属性名 数据类型 [完整性约束条件],<br /> 属性名 数据类型 [完整性约束条件],);<br /> <br /> primary key 标识主键,唯一表示<br /> foreign key 标识外键,与某表主键关联<br /> not null 标识不能为空<br /> unique 标识属性值唯一<br /> auto_increment 标识自增<br /> default 设置默认值<br /> <br /> eg:<br /> create table t_booktype(<br /> id int primary key auto_increment,<br /> bookTypeName varchar(20),<br /> bookTypeDesc varchar(200),);<br /> <br /> creat table t_book(<br /> id int primary key auto_increment,<br /> bookName varchar(20),<br /> author varchar(10),<br /> price decimal(6,2),<br /> bookTypeId int,<br /> constraint `fk` foreign key(`bookTypeId`) references `t_booktype`(`id`),);<br />

- 查看表

describe(desc) 表名;
show create table 表名;

- 修改表

  • 修改表名 alter table 旧表名 rename 新表名;
    - 修改字段 alter table 表名 change 旧属性名 新属性名 新数据类型
    - 增加字段 alter table 表名 add 属性名1 数据类型 [完整性约束][first|after 属性名2](在某个属性名第一个|后添加)
    - 删除字段 alter table 表名 drop 属性名

    - 删除表

    drop table 表名

    单表查询

    - 基础查询

    select 字段名 from 表名
    select * from 表名

    - where 条件查询

    select 字段 from 表名 where 条件表达式;

    eg:
    select * from t_studern where age>22;

    - 带in关键字查询

    select 字段 from 表名 where 字段 (not) in (元素1,元素2)

    eg:
    select * from t_studern where age in(22,50);

    - 带between and 范围查询

    select 字段 from 表名 where 字段 between 取值1 and 取值2;

    - 带like的模糊查询

    select 字段 from 表名 where 字段 like “字符串”;
    “%”代表任意字符;
    “代表单个字符;

    eg:
    select from t_studern where stuName like “张三”;
    select
    from t_studern where stuName like “张三%”(查询2个以上字符);
    select * from t_studern where stuName like “张三
    “(查询3个字符);

    - 空值查询

    1. select 字段 from 表名 where 字段 is null;<br />

    - and 查询

    1. select 字段 from 表名 where 条件表达式1 and 条件表达式2<br />

    - distinct 去重复查询

    1. select distinct 字段名 from 表名

    - 对查询条件进行排序

    1. select 字段 from 表名 order by 属性 [asc|desc]

    - group by 分组查询

    group by 属性 [having 条件表达式][with rollup]

    1、单独使用(毫无意义)
    2、与 group_concat 函数一起使用
    3、与聚合函数一起使用
    4、与 having 一起使用(限制输出结果)
    5、与 with rollup 一起使用(最后加入一个总和行)

    - limit 分页查询

    1. select 字段 from 表名 limit 初始位置, 记录数<br />

    聚合函数

    - count 函数

    1. select count(*) as total from 表名<br /> <br /> group by 一起使用<br /> select stuName,count(*) from t_grade where stuName="张三";(查询张三的总分<br /> select stuName,count(*) from t_grade group by stuName;(查询所有人的总分<br /> <br /> - sum<br /> - avg<br /> - max<br /> - min

    连接查询

    - 内连接查询

    select * from t_book,t_booktype where t_book.bookTypeId=t_booktype.id

    - 外连接查询

    外连接可以查出每一张表的所有信息
    select 属性名列表 from 表名1 left|right join 表名2 on 表名1.属性1=表名2.属性2

    eg:
    select * from t_book left join t_booktype on t_book.bookTypeId=t_booktype.t_booktype.id

    - 左连接查询

    可以查出表1的所有记录,

    - 右链接查询

    可以查出表2的所有记录,
    - 多条件查询
    条件后面用and链接

    子查询

    - 带 in 关键字的子查询

    select * from t_book where bookTypeId in (select id from t_booktype)

    - 带比较运算符的子查询

    select * from t_book where price>=(select price from t_pricelevel where priceLevel=1)

    - 带 exists 关键字的子查询

    select from t_book where exists (select from t_booktype) #如果内层不为空,则执行外层查询

    - 带 any 关键字的子查询

    表示满足其中任意条件
    select * from t_book where price>= any ( select price from t_pricelevel)

    - 带 all 关键字的子查询

    表示满足所有条件
    select * from t_book where price>= all ( select price from t_pricelevel)

    合并查询结果

    - union

    数据库会将所有的查询结果合并到一起,然后去除掉相同记录
    select id from t_book union select id from t_booktype

    - union all

    不会去除掉相同记录