• MySQL常用语法命令及函数


    #创建数据库#   create  database 数据库名;  
    #查看数据库#     show databases;  
      
    #选择数据库#      use 数据库名;  
      
    #删除数据库#      drop database 数据库名;  
      
      
      
    #创建表#  create table 表名(属性名1  数据类型 ,属性名2  数据类型。。。。);  
      
    #查看表结构# desc 表名;  
      
    #查看建表语言#  show create table 表名;  
      
    #表中新增字段#  alter table 表名 add( 属性名1 数据类型,属性名2 数据类型.....);  
      
    #在表的第一个位置增加字段#  alter table 表名 add 属性名 数据类型 first;  
      
    #在指定字段后面增加字段#   alter table  表名 add  新增属性名  数据类型  after  属性名;  
      
    #删除表中字段#  alter table 表名 drop  属性名;  
      
    #删除表#   drop table 表名;  
      
      
      
    #修改表名# alter table 旧表名 rename  新表名; 或alter table 旧表名 rename to 新表名;  
      
    #修改表中字段的数据类型#  alter table 表名 modify  需要修改的属性名   想要修改成的数据类型;  
      
    #修改表中字段名称 alter  table 表名 change 旧属性名  新属性名 旧数据类型;  
      
    #修改表中字段名称和数据类型  alter  table 表名 change 旧属性名 新属性名 新数据类型;  
      
    #修改表中字段为头字段#  alter  table  表名 modify  属性名 数据类型   first;   
      
    #修改字段1为顺序,在字段2后面#   alter table 表名 modify  属性名1  数据类型 after 属性名2;  
      
      
      
    #插入数据记录#  insert into 表名(字段1,字段2,.....或不写)   values(一条数据),(一条数据);  
      
    #插入查询结果# insert  into  要插入表表名 (插入表中字段1,插入表中字段2,.....)   select (查询表中字段1,查询表中字段2......)  from 要查询表表名    查询语句;  
      
    #更新数据记录#  update 表名  set 字段1=新值, 字段2=新值.........where  查询语句;  
      
    #删除数据记录#   delete from表名 where  查询语句;  
      
    #查询部分数据记录#  select    字段1,字段2。。。。 from   要查询的表;  
      
    #避免重复查询#  select   distinct    字段1,字段2。。。。 from   要查询的表;  
      
    #为查询结果起别名# select    字段1  as或不写    别名1,字段2  as或不写  别名2。。。。 from   要查询的表;  
      
    #设置显示格式数据查询#  select   concat(字段1,‘提示语句’,字段2。。。。) from  要查询的表;  
      
    #where条件查询#  select  * from   表名  where 字段=值;  
      
    #between and 关键字范围查询#   select *  from  表名  where  字段   between  值1  and  值2;  
      
    #between and  关键字不再范围内查询#  select  *  from   表名 where 字段 not  between 值1  and  值2 ;  
      
    #带null关键字空查询#    select  *  from 表名   where  字段  is  not   null;    或   select  *  from 表名   where  字段  is  not   null;  
      
    #带in关键字的集合查询#   select  字段1  ,字段2.。。。。。from  表名    where  字段n     in(值1,指2。。。。。);  
      
    #带like关键字的模糊查询#   select *  from 表名  where   字段 like  ‘字段串%’ ;       或select *  from 表名  where   字段 like  ‘字段串_’ ; 或    select *  from 表名  where    not  字段 like  ‘字段串_’ ;  
      
    #排序数据查询#   select *  from 表名 order  by  依照排序的字段名   asc(升序)  或 desc (降序);  
      
    #多字段排序#   select *  from 表名 order by  字段1 asc ,字段2  desc 。。。。;  
      
    #带limit关键字的限制查询数量#  select *  from 表名  where     limit  开始条数位置 ,显示条数;  
      
    #分组数据查询#   select   *   from    表名  group  by  字段;  #随机显示出每个分组的一条数据,一般讲分组与统计合起来使用才有意义#  
      
    #带having的分组限定查询#   select *  from 表名 group   by   字段  having   条件;  
      
      
    #inner  join  on内连接#  select * from   表1   inner   join   表2   on 条件;  
      
    #自连接# select  e.字段   as  别名1,f.字段 as 别名2.。。。。from     表名    as e  inner join 表  as f   on  条件;  
      
    #from多表连接#  select *  from 表1 ,  表 2 。。。    where   条件;  
      
    #左外连接# select *  from   表1   left    join    表2   on 条件;  
      
    #右外连接#   select *  from    表1  right    join  表2    on    条件;  
      
    #允许重复值的合并查询#  select * from    表1   union all    表2    on   条件;  
      
    #不含重复值的合并查询#  select *  from  表1   union  表2  on  条件;  
      
    #where型子查询#   select *  from 表名   where (字段1,字段2)=(select   字段1,  字段2  from  表名  where 。。。。);  
      
    #in关键字的子查询#   select  *  from  表名  where  字段  in (select 。。。。查询语句);  
      
    #any关键字的子查询#   select  *  from  表名  where  字段  >=any (select 。。。。查询语句);  
      
    #all关键字的子查询#   select  *  from  表名  where  字段   <=  all(select 。。。。查询语句);  
      
    #exists关键字的子查询#   select  *  from  表名  where   not exists (select 。。。。查询语句);  
      
    #regexp正则表达式运算符#  select  ‘chshs’   rehexp  ‘c.’  ;  
      
      
       
      
      
      
    #计数函数#  count();  
      
    #求平均函数#  avg();  
      
    #求和函数#   sum();  
      
    #取最大函数#   max();  
      
    #取最小函数# min();  
      
    #取绝对值# abs();  
      
    #取大于x的最大整数# cell(x);  
      
    #取小于x的最大整数# floor(x);  
      
    #取数值x的四舍五入后有y为小数# round(x,y);  
      
    #直接截取x为有y位的小数# truncate(x,y);  
      
    #返回0~1之间的一个随机数#rand();  
      
    #获取第n组返回相同值得随机数#  rand(n);  
      
      
    #对字符串进行加密#   password();  
      
    #字符串连接函数#  concat(字符串1,字符串2.。。);  
      
    #带分隔符字符串合并#   concat(分隔符,字符串1,字符串2.。。。);  
      
    #返回在字符串str2。。。中与str1相匹配的字符串位置#   find_in_set(‘str1’,‘str2,str3.。。。。’);  
      
    #返回字符串str1第一次出现位置#   field(‘str1’,‘str2’,‘str3’。。。。);  
      
    #返回子字符串str在字符串str1中匹配开始的位置#  locate(str,str1);或   position(str  in str1);  或   instr(str1  ,str);  
      
    #返回第n'个字符串#  elt(n,str1,str2.。。strn);  
      
    #截取指定位置和长度的字符串#  substring(str,num,length);  或  mid(str,num,length);  
      
    #将字符串str从第x位置开始,y个字符长的字串替换为字符串str2#     insert(str , x ,y,str2);  
      
    #将字符变为小写#  lower(str);或        lcase(str)  
      
    #将字符变为大写#   upper(str);或       ucase(str)  
      
    #获取字符串长度# length(str);  
      
    #获取字符数函数# char_length(str);  
      
    #返回字符串str中最左边的x个字符#   left(str,x);  
      
    #返回字符串str中最右边的x个字符#    right(str,x);  
      
    #使用字符串pad对字符串str最左边进行填充,知道长度为n个字符长度#    lpad(str,n,pad);  
      
    #使用字符串pad对字符串str最右边进行填充,知道长度为n个字符长度#    rpad(str,n,pad);  
      
    #去掉字符串str左边的空格#   ltrim(str);  
      
    #去掉字符串str右边的空格#   rtrim(str);  
      
    #返回字符串str重复x次的结果#   repeat(str,x);  
      
    #使用字符串b代替字符串str中的所有字符串a#    replace(str,a,b);  
      
    #比较字符串str1和str2#  strcmp(str1,str2);  
      
    #去掉字符串str行头和行尾的空格#   trim(str);  
      
    #返回字符串str中从x位置起y个长度的字符串#   substring(str,x,y);  
      
      
    #获取当前日期# curdate(); 或  current_date();  
      
    #获取当前时间# curtime(); 或  current_time();  
      
    #获取当前日期和时间# now();或 current_timestamp()  或  localtime()  或  systemdate();  
      
    #获取日期date的UNIX时间戳#   unix_timestamp(date);  
      
    #获取unix时间戳的日期值#   from_unixtime();  
      
    #返回日期date为一年中的第几周#    week(date); 或 weekofyear(time);  
      
    #返回日期的英文周几#  dayname(time);  
      
    #返回日期和时间中周几(1:周日,2:周一)#  dayofweek();  
      
    #返回日期和时间中周几(0:周一,1:周二)#  weekday();  
      
    #返回年中第几天# dayofyear(time);  
      
    #返回月中第几天# dayofmonth(time);  
      
    #返回日期date的年份#  year(date);  
      
    #返回时间time的小时值#   hour(time);  
      
    #返回时间time的分钟值#  minute(time);  
      
    #返回时间time的月份值#  monthname(date); 或 month(time)  
      
    #截取日期中的各部分值# extrcat(年或月或日或时或分或秒      from  time);  
      
    #计算date1与date2之间相隔天数#   datediff(date1,date2);  
      
    #计算date加上n天后的日期#    adddate(date,n);  
      
    #计算date减去n天后的日期#   subdate(date,n);  
      
    #计算time加上n秒后的时间#    adddate(time,n);  
      
    #计算time减去n秒后的时间#    subdate(time,n);  
      
      
    #返回数据库版本号#  version();  
      
    #返回当前数据库名#  database();  
      
    #返回当前用户#  user();  
      
    #将IP地址转化为数字#  inet_aton(ip);  
      
    #将数字转化为IP地址#   inet_ntoa(x);  
      
    #创建一个持续时间为time的名为name的锁#     cet_loct(name,time);  
      
    #为名为name的锁解锁# release_loct(name);  
      
    #将表达式重复执行count次#  benchmark(count,表达式);  
      
    #将x变为type形式#  convert(x,type);  
      
      
      
      
      
      
    #设置字段的非空约束# create table 表名 (属性名 数据类型  not null);  
      
    #设置字段的默认值#  create table 表名 (属性名  数据类型  default  默认值);  
      
    #设置字段的唯一约束# create table 表名(属性名  数据类型  unique );  
      
    #设置字段的唯一约束并未约束命名#    create  table 表名(属性名1 数据类型 , 属性名2     数据类型  ..........        constraint   约束名  unique (属性名1,属性名2......));  
      
    #设置单字段为主键约束#  create  table 表名(属性名1  数据类型   primary  key....);  
      
    #设置多字段为主键约束#   create  table  表名(属性名1   数据类型  ,  属性名2  数据类型........constraint   约束名  primary  key (属性名1,属性名2......));  
      
    #设置字段自动增加值#      create table 表名 (属性名   数据类型    auto_increment.........);  
      
    #设置外键约束#    create table 表名 (属性名1    数据类型  ,   属性名2   数据类型........      constraint   外键约束名  foreing  key (外键属性名1)    references    表名 (主键属性名2));  
      
      
      
    #创建普通索引#  create table 表名(属性名  数据类型 ,属性名  数据类型.....   index或 key 索引名(可省略)(属性名  (长度(可省略))    asc或desc);  
      
    #在已存在表创建普通索引# create index 索引名  on 表名  ( 属性名 (长度或不写)   asc或desc或不写);  或   alter   table  表名  add  index或key   索引名(属性名 (长度或不写)   asc或desc或不写);   
      
    #创建唯一索引#     create table 表名(属性名  数据类型 ,属性名  数据类型..... unique    index或 key 索引名(可省略)(属性名  (长度(可省略))    asc或desc);  
      
    #在已存在表创建唯一索引# create  unique   index 索引名  on 表名  ( 属性名 (长度或不写)   asc或desc或不写);  或   alter   table  表名  add    unique  index或key   索引名(属性名 (长度或不写)   asc或desc或不写);  
      
    #创建全文索引#  create table 表名(属性名  数据类型 ,属性名  数据类型..... fulltext    index或 key 索引名(可省略)(属性名  (长度(可省略))    asc或desc);  
      
    #在已存在表创建全文索引# create  fulltext   index 索引名  on 表名  ( 属性名 (长度或不写)   asc或desc或不写);  或   alter   table  表名  add    fulltext  index或key   索引名(属性名 (长度或不写)   asc或desc或不写);  
      
    #创建多列索引#  create table 表名(属性名  数据类型 ,属性名  数据类型.....   index或 key 索引名(可省略)(属性名1  (长度(可省略))    asc或desc ,属性名2  (长度(可省略))    asc或desc.........);  
      
    #在已存在表创建多列索引# create   index 索引名  on 表名 (属性名1  (长度(可省略))    asc或desc ,属性名2  (长度(可省略))    asc或desc.........);  或   alter   table  表名  add     index或key   索引名(属性名1  (长度(可省略))    asc或desc ,属性名2  (长度(可省略))    asc或desc.........);;  
      
    #查看索引是否用到#  explain  select * from 表名  where  .......;  
      
    #删除索引#   drop index   索引名 on 表名;  
      
      
      
    #创建视图#  create view  视图名 as   查询语句;  
      
    #查看视图详细信息#  show    table   status   from  数据库名  like '视图名';       或    show  table   status  
      
    #查看视图定义信息#   show  create  view  视图名;  
      
    #查看视图设计信息#   desc  视图名;  
      
    #通过系统表查看视图信息#   use  information_schema ;    select * from views where table_name='视图名'G;  
      
    #删除视图#  drop view  视图名;  
      
    #修改视图#  create or replace view 视图名 as 查询语句;   或    alter  view   视图名  as  查询语句 ;  
      
      
      
    #创建触发器# create trigger   触发器名 before或after   触发条件(delete、insert、update) on  触发条件的操作表表名  for   each   row  触发语句;  
      
    #创建含多条语句的触发器# delimiter $$ create trigger   触发器名 before或after   触发条件(delete、insert、update) on  触发条件的操作表表名  for   each   row  begin 触发语句1; 触发语句2;......;end $$  delimiter;  
      
    #查看触发器#  show triggersG  
      
    #通过查看系统表查看触发器信息# use information_schema;  select * from triggersG  
      
    #删除触发器# drop trigger 触发器名字  
      
      
      
    #查看错误信息#   show warnings;  
      
    #查看支持的存储引擎#   show engines;   或 show    variables   like 'have%';  
      
    #查看默认存储引擎#    show      variables    like   'storage_engine% ';  
      
    #查看MySQL的帮助文档目录列表# help  contents;      
      
      
      
    #查看数据类型# help data  types;  
      
    #显示当前年月日# select curdate();     
      
    #显示当前年月日和时间#  select now();  
      
    #显示当前时间# select time(now());  
      
    #显示当前年月日#  select year (now()) ;    
      
      
      
    #创建存储过程#  create procedure   存储过程名字  (存储过程参数:输入/输出类型,参数名,参数类型)  存储过程特性或不写     存储过程的语句;  
      
    #创建函数过程#   create function 函数名字 (函数的参数:参数名,参数类型)   函数特性或不写   函数过程语句;  
      
    #查看存储过程#  show procedure status like '存储过程名' G   或  use information_schema;  select * from routines where specific_name='存储过程名'G  
      
    #查看函数过程#    show function status like '函数过程' G或  use information_schema;  select * from routines where specific_name='函数过程名'G  
      
    #查看存储过程定义信息#  show   create    procedure  存储过程名G  
      
    #查看函数过程定义信息#   show crate  function  函数名G  
      
    #删除存储过程#   drop  procedure    存储过程名G  
      
    # 删除函数#   drop   function  函数名G  
      
    #创建普通用户账户#  create user 用户名 identified    by  ‘密码’;  
      
    #创建带权限的普通用户账户#   grant  权限参数:select、create、drop等  on  库.表(权限范围)   to  用户名 identified    by  ‘密码’;  
      
    #更改超级用户root的密码#    set  password=password(“新密码”);  
      
    #利用root用户修改带权限的普通用户密码# grant  权限参数:select、create、drop等  on  库.表(权限范围)   to  用户名 identified    by  ‘新密码’;  
      
    #利用root用户修改普通用户密码#set  password    for   用户名=password(“新密码”);  
      
    #普通用户更改自身密码#set  password=password(“新密码”);  
      
    #删除普通用户#   drop  user 用户名;  或  delete from  user   where  user=“用户名”;  
      
    #对普通用户进行授权#  grant  权限参数:select、create、drop等  on  库.表(权限范围)   to  用户名;  
      
    #收回权限#   revoke  权限参数:select、create、drop等  on  库.表(权限范围)   from  用户名;  
      
    #收回所有权限#   revoke all privileges,grant option from  用户名;  
    

      

  • 相关阅读:
    mysql修改加密方式
    信息安全学习路线
    DNS域传送漏洞
    CSRF漏洞
    反序列化漏洞
    计算机通讯基础
    gorm gen使用
    golang makefile使用
    linux命令行录制
    go代码自动生成注释
  • 原文地址:https://www.cnblogs.com/zybcn/p/8645145.html
Copyright © 2020-2023  润新知