MySQL

来源:互联网 发布:redflag linux 8 编辑:程序博客网 时间:2024/06/01 10:01

1.数据库类型

  • 整形:

bit.bool/tinyint      有符号-127到127,无符号0到255          1字节

smallint          小型整数     2字节 

mediumint      中型整数     3字节

int                   标准整数     4字节

bigint              大型整数     8字节

  • 浮点型:float,double    decimal(一般整数,可变即自定义长度)
  • 字符型:

char,varchar(比char多一功能,即长度可变)

....

  • 日期和时间

date                  日期,yyyy-mm-dd

time                  时间,hh:mm:ss

datetime           日期和时间,yyyy-mm-dd  hh:mm:ss

year                   年份yyyy

2.数据库和数据库表

创建数据库  :create   database   库名;

查看数据库  :show  databases;

删除数据库   : drop  database 库名;

选择要使用的数据库  : use  库名;


创建表  :create table  表名;

查看表  :show tables;  数据库中所有的表

               查看某一表中所有内容

               show  columns  from 表名  from 库名;

               show  columns  from  表名.库名;

              查看某一表中的一个至多个列

               describe  表名;

               describe  表名.列名;

修改表  :alter   table 表名   alter_spec,alter_spec...

  • add               添加新字段
  • alter              修改字段名称
  • change         修改字段类型
  • modify         修改字段数值
  • add   index        添加索引
  • add   unique     添加唯一索引
  • add   primapry  key     添加主键
  • drop       删除字段(同样也可以删除索引、主键等)

         alter  table  表名  add   email  varchar(30)  not null,modify  name  varchar(40);

表重命名  :  rename  table 表名  to  新表名;

删除表  :drop table  表名;        drop table if exists  表名;  //判断是否存在,存在就删除

插入记录  : insert into 表名(列名,id,name,password) value('值','3','jack','abc123')


3.数据库记录

查询:sclect  *  from  表名   where   条件

         *表示所有列名,select  列名,列名

修改:update   表名  set  列名1=值,列名2=值....   where  条件

删除:delete   from  表名  where   条件


4.where  条件

  • type   类型
  • group  by    对结果进行分组
  • distinct      在结果中去除重复行      select distinct  type  from  表名
  • order   by   对结果进行排序,默认升序asc,降序desc实现
  • like 模糊查询

select  *  from  表名 whereorder  by  id  desc limit 5;

查询表中按ID降序排名的五行

select  *  from  表名 where bookname like('_h%');

%匹配一个至多个字符,_下划线匹配一个字符

  • 带in关键字的查询
  • between...and...
  • is null
  • count()   返回行

sum()       返回某字段的总和

avg()        返回某字段的平均值

max()      返回某字段的最大值

min()      返回某字段的最小值

select  *  from  表名 where id  in  ('1','2');

select  *  from  表名 where id between 5  and  7;

//返回id在5到7范围

select  *  from  表名 where id  is null

select  sum(列名)  from  表名;

  • 多条件查询and,两个都满足    where  条件1  and  条件2
  • 多条件查询or,只要有一个满足即可      where  条件1  or  条件2
  • 去除重复行distinct     select distinct  type  from  表名

5.连接查询

  1. 内连接      select  *  from  表1,表2   where  表1.id=表2.id;
  2. 外连接     select  *  from  表1  left|right  join  表2   on   表1.id=表2.id;

6.符合条件查询

       where 表1.id=表2.id  and  id>5;

select  *  from  表1   where  id=( select  id  from  表2   where  name like('%an'));




1 0
原创粉丝点击