MySQL简单指令(CMD)

---------------------------------------------------------------------------

创建表

CREATE TABLE 表名 (属性名 数据类型 [完整性约束条件].

           属性名 数据类型 [完整性约束条件].

           .

           .

           属性名 数据类型 [完整性约束条件]

          );

约束条件 说明
PRIMARY KEY 标识该属性为该表的主键,可以唯一标识的对应的记录
FOREIGN KEY 标识该属性为该表的外键,与某表的主键关联
NOT NULL 标识该属性不能为空
UNIQUE 标识该属性的值是唯一的
AUTO_INCREMENT 标识该属性的值自动增加
DEFAULT 为属性设置默认值

---------------------------------------------------------------------------

查看表

show tables;    ----查看当前数据库中的表

插入数据

insert into `表名` (`字段1`,`字段2`,`字段3`...) values ('字段1','字段2','字段3'...);

添加新属性

alter table 表名 add 字段 字段类型;

---------------------------------------------------------------------------

查看表结构

desc 表名;    ----查看基本表结构

show create table 表名;     ----查看表详细结构

---------------------------------------------------------------------------

修改表

alter table 旧表名 rename 新表名;    ----修改表名

alter table 表名 change 旧属性名 新属性名 新数据类型;    ----修改字段

alter table 表名 add 属性名1 数据类型 [完整性约束条件] [FIRST|AFTER 属性名2];    ----增加字段

alter table 表名 drop 属性名;    ----删除字段

---------------------------------------------------------------------------

删除表

drop table 表名;    ----删除表

---------------------------------------------------------------------------

单表查询

select 字段1,字段2,字段3,...from 表名;    ----查询所有字段

select * from 表名;    ----查询所有字段

select 字段1,字段2,... from 表名;    ----查询指定字段

select 字段1,字段2,... from 表名 where 条件表达式;    ----where条件查询

select 字段1,字段2,... from 表名 where 字段 [not] in (元素1,元素2,元素3);    ----选择[不]在括号内元素(部分字段)

select 字段1,字段2,... from 表名 where 字段 [not] between a and b;    ----选择[不]在ab范围内元素(部分字段)

select 字段1,字段2,... from 表名 where 字段 is NULL;    ----空值查询 

select 字段1,字段2,... from 表名 where 字段 like '%字符a%';    ----模糊查询字段中包含字符a元素

select 字段1,字段2,... from 表名 where 字段 like '字符a_';    ----模糊查询字段中字符a后还有1个字的元素(1个_表示一个字符)

select 字段1,字段2,... from 表名 where 条件表达式 and 条件表达式;    ----与条件查询

select 字段1,字段2,... from 表名 where 条件表达式 or 条件表达式;    ----或条件查询

select distinct 字段 from 表名;    ----去重复查询

select 字段1,字段2,... from 表名 order by 字段 (desc);    ----按字段升序(降序)排序

select 字段1,group_concat(字段2) from 表名 group by 字段1;    ----按字段1分组输出字段2

select 字段1,count(字段2) from 表名 group by 字段1;    ----按字段1分组输出字段2个数

select 字段1,count(字段2) from 表名 group by 字段1 having count(字段2)+条件表达式;    ----按字段1分组输出条件内字段2个数(having对查询进行筛选)

select 字段1,count(字段2) from 表名 group by 字段1 with rollup;    ----with rollup将结果综合

select 字段1,字段2... from 表名 limit a,b;    ----输出从a开始b条数据

select 字段1,count(字段2) as 命名 from 表名 group by 字段1;    ----按字段1分组重命名输出字段2个数

---------------------------------------------------------------------------

 使用聚合函数查询

select count(*) from 表名;    ----计算表中有几组数据

select 字段1,count(*) from 表名 group by 字段1;    ----统计字段数量

select stuName,sum(字段) from 表名 where 条件表达式;    ----求和函数

select stuName,sum(字段) from 表名 group by 字段;    ----求和函数

select 字段,avg(字段) from 表名 where 条件表达式;    ----平均函数

select 字段,avg(字段) from 表名 group by 字段;    ----平均函数

select 字段,sum(字段) from 表名 where 条件表达式;    ----最大值函数

select 字段,sum(字段) from 表名 group by 字段;    ----最大值函数

select 字段,sum(字段) from 表名 where 条件表达式;    ----最大值函数

select 字段,min(字段) from 表名 where 条件表达式;    ----最小值函数

---------------------------------------------------------------------------

连接查询

内连接查询

select * from 表1,表2...;    ----连接两个表

select * from 表1,表2... where 条件表达式;    ----符合条件的两个表属性

select 字段1,字段2... from 表1,表2... where 条件表达式;    ----符合条件的两个表属性

外连接查询

select 字段1,字段2... from 表名 left join 表名 on 条件表达式;    ----左连接

select 重命名1.字段1,重命名2.字段2... from 表1 重命名1 left join 表2 重命名2 on 条件表达式;    ----左连接

select 重命名1.字段1,重命名2.字段2... from 表1 重命名1 right join 表2 重命名2 on 条件表达式;    ----右连接

多条件查询

select 字段1,字段2... from 表1,表2... where 条件表达式 and 条件表达式;    ----多条件查询

---------------------------------------------------------------------------

子查询

select * from 表名 where 字段 not in (select 字段 from 表名);    ----带in关键字查询(条件落在另一个select查询结果中)

select * from 表名 where 字段 条件表达式 (select查询);    ----条件查询判断select结果

select * from 表名 where 字段>= any (select查询);    ----条件判断符合任意select查询结果

select * from 表名 where 字段>= all (select查询);    ----条件判断符合所有select查询结果

---------------------------------------------------------------------------

合并查询

select 字段 from 表名 union select 字段 from 表名;    ----将查询到的记录合并(去重复)

select 字段 from 表名 union all select 字段 from 表名;    ----将查询到的记录合并(不去重复)

---------------------------------------------------------------------------

取别名

select a.字段 from 表名 重命名a where 条件表达式;    ----表重命名

select a.字段 (as) 重命名b from 表名 重命名a where 条件表达式;    ----字段重命名

---------------------------------------------------------------------------

 插入数据

insert into 表名 values(数据1,数据2,数据3...);    ----插入数据(没有的用NULL)

insert into 表名(字段1,字段2...) values (数据1,数据2...);    ----给指定字段插入数据

---------------------------------------------------------------------------

更新数据库

update 表名 set 字段1=新数据1,字段2=新数据2... where 条件表达式;    ----更新数据

update 表名 set 字段1=新数据1,字段2=新数据2... where 字段 like 数据;    ----like查询并更新数据

delete from 表名 where 条件表达式;    ----删除指定条件的数据

---------------------------------------------------------------------------

数据索引

create table 表名(属性名 数据类型[约束条件],

          属性名 数据类型[约束条件],

        ...

        unique|fulltext|spatial index|key 别名 (属性名(长度) asc|desc)

         );    ----在创建表的时候创建索引

unique|fulltext|spatial index|key 别名 (属性名1,属性名2... asc|desc)    ----创建表时的多列索引

create unique|fulltext|spatial index 别名 on 表名(属性名);    ----在已经存在的表中创建索引

alter table 表名 add unique|fulltext|spatial index 别名(属性名);

create index 别名 on 表名(属性名1,属性名2...);    ----在已经存在的表中创建多列索引

alter table 表名 add index 别名(属性名1,属性名2...);

drop index 索引名 in 表名;    ----删除索引

---------------------------------------------------------------------------

视图

create view 视图名 as select 别名1.字段1,别名2.字段2... from 表1 别名1,表2 别名2... where 条件表达式;    ----创建视图

insert into 视图名 values(字段1,字段2...);    ----视图中插入数据

update 视图名 set 字段1=新数据,字段2=新数据... where 条件表达式;    ----视图中更新数据

delete from 视图名 where 条件表达式;    ----删除符合条件的数据

drop view (if exists) 视图名 (restrict|cascade);    ----删除视图

 
---------------------------------------------------------------------------

触发器

create trigger 触发器名 after insert
  on 表名 for each row
    update 表名 set 字段表达式 where 条件表达式;

delimiter |
  create trigger 触发器名 after delete
    on 表名 for each row
    begin
      update 表名 set 字段表达式 where 条件表达式;
      insert into 表名 values(字段1,字段2...);
      delete form 表名 where 条件表达式;

      ......

    end
|
delimiter;    ----多执行语句触发器

drop trigger 触发器名;

---------------------------------------------------------------------------

常用函数

curdate()    ----返回当前日期(年月日)

curtime()    ----返回当前时间

now()    ----返回当前精确时间(年月日时分秒)

year()    ----返回date数据类型的年

month()    ----返回date数据类型的月

day()    ----返回数据date数据类型的天

char_length()    ----计算字符个数

upper()    ----将字符大写

lower()    ----将字符小写

abs()    ----求绝对值

sqrt()    ----求平方根

mod(a,b)    ----a除b求余

md5('字符串')    ----密码加密(不可逆)

aes_encrypt('密码','秘钥')    ----秘钥加密

aes_encrypt('密文','秘钥')    ----秘钥解密

    ----在使用aes_encrypt加密时,如果要将加密的密码存入varchar类的列中需要hex()函数将密文转化成十六进制,解密时将密文用unhex()取出

---------------------------------------------------------------------------

创建存储函数和存储过程

delimiter &&
  create procedure 存储过程名 (in 变量 数据类型,out 变量 数据类型,inout 变量 数据类型)
  reads sql data
  begin
    select count(*) from 表名 where 条件表达式;
  end
  &&
delimiter;

delimiter &&
create procedure 存储过程名()
  begin
    declare 变量 数据类型;
    set 变量='变量值';
    insert into 表名 values(变量1,变量2....);
  end
&&
delimiter ;

delimiter &&
create procedure 存储过程名()
  begin
    declare a,b 数据类型;
    select 字段1,字段2 into a,b from  where id=1;
    insert into 表名 values(null,a,b);
  end
&&
delimiter;

delimiter &&

create procedure 存储过程名()

  begin

    declare a,b 数据类型;

    declare 游标名 cursor for select 字段1,字段2... from 表名;

    open 游标名;

    fetch 游标名 into a,b;

    insert into 表名 values(a,b);

    close 游标名;

  end

&&

delimiter;

declare cursor_name cursor for select_statement;    ----定义游标

open cursor_name;    ----打开游标

close cursor_name;    ----关闭游标

fetch cursor_name into varname;    ----使用游标

delimiter &&
  create procedure 存储过程名(in 字段 数据类型)
  begin
   select count(*) into @num from 表名 where 条件表达式
   if @num>0 then update 表名 set 字段='字符串' where 条件表达式;
   else
      insert into 表名 values(字段1,字段2...);
   end if;
  end
&&
delimiter;

delimiter &&
create procedure 存储过程名(in 字段 数据类型)
  begin
    select count(*) into @num from 表名 where 条件表达式;
    case @num
        when 1 then update t_user1 set 字段1='字符'... where 条件表达式;
        when 2 then insert into t_user1 values();
        else insert into t_user1 values();
    end case;
  end
&&
delimiter ;

delimiter &&
create procedure 存储过程名(in totalNum(变量名) int)
  begin
    字段:loop
      set totalNum = totalNum-1;
      if totalNum = 0 then leave aaa;
      elseif totalNum = 3 then iterate aaa;    ----iterate 当totalNum=3时候跳过(类似continue);
      end if;
      insert into 表名 values(totalNum,'23123123','123125123');
    end loop 字段;
  end
&&
delimiter ;


delimiter &&
create procedure 存储过程名(in totalNum(变量名) int)
  begin
    repeat
      set totalNum=totalNum-1;
      insert into t_user3 values(totalNum,'2123123','1235135');
      until totalNum=2
    end repeat;
  end
&&
delimiter

delimiter &&
create procedure 存储过程名(in totalNum(变量名) int)
  begin
    while totalNum>3 do
      insert into t_user3 values(totalNum,'1231576y24','12312315');
      set totalNum=totalNum-1;
    end while;
   end
&&
delimiter

show procedure|function status like '存储过程名|函数名';

show create procecdure|function sp_name;

alter procedure | function sp_name characteristic;    ----修改存储过程和函数(sp_name表示函数名或过程名)
characteristic{

{contains sql} no sql|reads sql data|modifies sql datta}
|sql security{definer|invoker}
|comment 'sting'    ----后面加注释语句

drop procedure|function sp_name;


---------------------------------------------------------------------------

数据库备份与还原

mysqldump -u  username -p dbname table1 table2 ...>BackupName.sql    ----备份数据库(将数据库导出为.sql文件)

dbname----数据库名

table----表名可以不加

BackupName----导出数据库名(>后面可以用绝对路径)

mysql -u root -p (dbname) <backup.sql    ----dbname表示数据库名称(可选),输入时是新建一个数据库导入已有的表,不输入则是导入已有的库(>后面可以用绝对路径)

原文地址:https://www.cnblogs.com/mengxinteriri/p/10686400.html