• mysql 常用操作


    忘记密码
    1 启动免授权服务端
    2 编辑mysqld配置文件 添加一行 --skip-grant-tables
    3 此时客户端在登陆服务端不需要密码
    4 修改用户名密码
    5 update mysql.user set authentication_string=password('password') where user='root';
    6 flush privileges;
    创建用户和授权
    1 创建用户
     2 create user "test"@"localhost" identified by "password"; 
     3 删除用户
     4 drop user 'test'@'localhost';
     5 授权用户
     6 grant 权限 on 数据库.表名 to '用户名'@'IP';
     7 grant all privileges on test.* to 'test'@'localhost';
     8 取消权限
     9 revoke 权限 on 数据库.表名 from '用户名'@'IP';
     10 revoke all privileges on test.* from 'test'@'localhost';
     11 修改密码
     12 use mysql
     13 update user set password="newpassword" where user="test" and host = "localhost";
     14 flush privileges;
     15 查看用户权限
     16 show grants for simple@localhost;
     1 all privileges  除grant外的所有权限
     2             select          仅查权限
     3             select,insert   查和插入权限
     4             ...
     5             usage                   无访问权限
     6             alter                   使用alter table
     7             alter routine           使用alter procedure和drop procedure
     8             create                  使用create table
     9             create routine          使用create procedure
    10             create temporary tables 使用create temporary tables
    11             create user             使用create user、drop user、rename user和revoke  all privileges
    12             create view             使用create view
    13             delete                  使用delete
    14             drop                    使用drop table
    15             execute                 使用call和存储过程
    16             file                    使用select into outfile 和 load data infile
    17             grant option            使用grant 和 revoke
    18             index                   使用index
    19             insert                  使用insert
    20             lock tables             使用lock table
    21             process                 使用show full processlist
    22             select                  使用select
    23             show databases          使用show databases
    24             show view               使用show view
    25             update                  使用update
    26             reload                  使用flush
    27             shutdown                使用mysqladmin shutdown(关闭MySQL)
    28             super                   使用change master、kill、logs、purge、master和set global。还允许mysqladmin调试登陆
    29             replication client      服务器位置的访问
    30             replication slave       由复制从属使用
    权限细化
    基本语法
     1 create table 表名 (字段名 字段类型 是否为空,
     2                           字段名 字段类型 是否为空,
     3                            ......
     4 )engine=Innodb default charset=utf8;
     5 
     6 
     7 
     8 #示例
     9 create table test( 
    10     id int not null auto_increment, 
    11     name char(32) not null,
    12     age int not null,
    13     primary key(id)
    14 )engine=innodb default charset=utf8;
    15 
    16 
    17         
    创建表
    1 #主键,一种特殊的唯一索引,不允许为空,如果主键使用单个列,则它的值必须唯一,如果是多列,则是组合必须唯一。设置主键   primary key (字段) 
     2 
     3 create table test( 
     4     id int not null auto_increment, 
     5     name char(32) not null, 
     6     age int not null,
     7     primary key(id)
     8 )engine=innodb default charset=utf8;
     9 
    10 
    11 #自增, 如果为某列设置自增列,插入数据时无需设置此列,默认蒋自增(表中只能有一个自增列) 设置自增  auto_increment
    12 注意:
    13     1.对于自增列,必须是索引(含主键)
    14     2.对于自增可以设置步长和起始值
    15 
    16                      show session variables like 'auto_inc%';
    17                      set session auto_increment_increment=2;
    18                      set session auto_increment_offset=10;
    19         
    20                      shwo global  variables like 'auto_inc%';
    21                      set global auto_increment_increment=2;
    22                      set global auto_increment_offset=10; 
    主键&自曾建
     1 添加列:alter table 表名 add 列名 类型
     2 删除列:alter table 表名 drop column 列名
     3 修改列:
     4         alter table 表名 modify column 列名 类型;  -- 类型
     5         alter table 表名 change 原列名 新列名 类型; -- 列名,类型
     6   
     7 添加主键:
     8         alter table 表名 add primary key(列名);
     9 删除主键:
    10         alter table 表名 drop primary key;
    11         alter table 表名  modify  列名 int, drop primary key;
    12   
    13 添加外键:alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);
    14 删除外键:alter table 表名 drop foreign key 外键名称
    15   
    16 修改默认值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;
    17 删除默认值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;
    修改表
    1 #删除表
    2 drop table 表名
    3 
    4 
    5 #清空表
    6 
    7 delete from 表名
    8 
    9 turncate  table 表名    # 同时清空自增ID。效率高
    删除表&清空表
    增、删、查、改

     

    1 #
    2 insert into tablename (字段1,字段2,字段3.....) values ("值1""值2","值3"....);
    3 
    4 insert into tablename (字段1,字段2,字段3.....) values ("值1""值2","值3"....),("值1""值2","值3"....),("值1""值2","值3"....)....;
    5 
    6 
    7 insert into tablename (字段1,字段2,字段3.....) select 字段1,字段2,字段3..... from
    1 #
    2 delete from 表名  清空表
    3 truncate table 表名 
    4 delete from user where user='test' and host = "localhost";
    1 #
    2 select * from3 select * from 表 where id > 1
    4 select nid,name,gender as gg from 表 where id > 1    # gender as gg  取一个别名,打印结果的时候把gender 替换成 gg 显示

    1 #改
    2 update test set user='exsample' where id =1;
    扩展

     

     1 a、条件
     2     select * from 表 where id > 1 and name != 'alex' and num = 12;
     3  
     4     select * from 表 where id between 5 and 16;
     5  
     6     select * from 表 where id in (11,22,33)
     7     select * from 表 where id not in (11,22,33)
     8     select * from 表 where id in (select nid from 表)
     9  
    10 b、通配符
    11     select * from 表 where name like 'ale%'  - ale开头的所有(多个字符串)
    12     select * from 表 where name like 'ale_'  - ale开头的所有(一个字符)
    13  
    14 c、限制
    15     select * from 表 limit 5;            - 前5行
    16     select * from 表 limit 4,5;          - 从第4行开始的5行
    17     select * from 表 limit 5 offset 4    - 从第4行开始的5行
    18  
    19 d、排序
    20     select * from 表 order by 列 asc              - 根据 “列” 从小到大排列
    21     select * from 表 order by 列 desc             - 根据 “列” 从大到小排列
    22     select * from 表 order by 列1 desc,列2 asc    - 根据 “列1” 从大到小排列,如果相同则按列2从小到大排序
    23  
    24 e、分组         #默认去重
    25     select num from 表 group by num
    26     select num,nid from 表 group by num,nid
    27     select num,nid from 表  where nid > 10 group by num,nid order nid desc
    28     select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid
    29  
    30     select num from 表 group by num having max(id) > 10
    31  
    32     特别的:group by 必须在where之后,order by之前
    33  
    34 f、连表
    35     无对应关系则不显示
    36     select A.num, A.name, B.name
    37     from A,B
    38     Where A.nid = B.nid
    39  
    40     无对应关系则不显示
    41     select A.num, A.name, B.name
    42     from A inner join B
    43     on A.nid = B.nid
    44  
    45     A表所有显示,如果B中无对应关系,则值为null
    46     select A.num, A.name, B.name
    47     from A left join B
    48     on A.nid = B.nid
    49  
    50     B表所有显示,如果B中无对应关系,则值为null
    51     select A.num, A.name, B.name
    52     from A right join B
    53     on A.nid = B.nid
    54  
    55 g、组合
    56     组合,自动处理重合
    57     select nickname
    58     from A
    59     union
    60     select name
    61     from B
    62  
    63     组合,不处理重合
    64     select nickname
    65     from A
    66     union all
    67     select name
    68     from B
    69 
    70 #特殊函数 
    71 count  (计数)
    72 sum    (求和)
    73 avg    (平均值)
    74 max    (最大值)
    75 min    (最小值)
    76 distinct  (去重 跟分组效果一样 group by)
    77 
    78 使用特殊函数之后不支持where条件,如果使用条件需要使用having
    79 
    80 示例
    81 SELECT Employee, SUM (Hours)
    82 FROM EmployeeHours
    83 GROUP BY Employee
    84 HAVING SUM (Hours) > 24
  • 相关阅读:
    Ajax中XML和JSON格式的优劣比较
    多语言的网站怎么做呀
    asp.net 2.0多语言网站解决方案
    FrameSet 与 IFrame 彻底剖析
    概要设计说明书
    用C#如何实现大文件的断点上传!
    PowerDesigner生成sql生成不了
    TransactionScope的正确用法
    iframe
    动网转型网游一年获得成功:每月盈利超500万
  • 原文地址:https://www.cnblogs.com/simple001/p/7560941.html
Copyright © 2020-2023  润新知