MySql 常用语句

操作:

    0、登录:        mysql -u用户名 -p密码  (p与密码之间不能有空格) 
    1、查看库:    show databases;
    2、创建库:    create database 数据库名;
    3、删除库:    drop database 数据库名;
    4、展示库信息:  show create database 数据库名;
    5、修改库编码:  alter database 数据库名 default character set gbk collate gbk_bin;
                                     alter database 数据库名 default character set utf8mb4 collate utf8mb4_bin;
                             注意:该方法只对当前窗口有效,永久修改需要修改my.ini

    6、修改库名:    创建新库,复制库内容,改新库名,删旧库;

    7、创建表:(1)先进入数据库:ues 数据库名;
                      (2)创建表及字段:creat table 表名(字段名 数据类型(空间),……)
                      (3)创建约束条件:creat table 表名(字段名 数据类型(空间)[约束条件],……)

                      约束条件:主键、复合主键、外键、非空、唯一、默认

                     (以下顺序均为:建表时建立约束条件语句)
                           (为已存在的表添加约束条件的语句)
                                   (删除表中已存在的约束条件的语句)

               1     primary key     主键约束(建表时)
                             alter table 表名 modify 字段名 数据类型 primary key;(已存在表时)
                                     alter table 表名 drop primary key;(删除主键)
    
               2      primary key(字段1、字段二……):符合主键
                             alter table 表名 add primary key(字段1、字段二……);
                                    alter table 表名 drop primary key;

              3 constraint 外键名 foreign key (从表的外键字段) references 主表名 (主表的主键字段)
                            alter table从表 add constraint  外键名 foreign key (从表的外键字段) references 主表名 (主表的主键字段)
                                        alter table 从表 drop foreign key 外键名;


                        注意:外键名命名规范:fk_从表名_主表名
                        注意:两个外键字段需要加上()

              4      not null 非空约束
                             alter table 表名 modify 字段名 新数据类型 not null;
                                   alter table 表名 modify 字段名 数据类型
                        
                    
              5       unique 唯一约束
                             alter table 表名 modify 字段名 新数据类型 unique;
                                     alter table 表名 drop index 字段名;

              6      default 默认约束
                             alter table 表名 modify 字段名 新数据类型 default 默认值;
                                     alter table 表名 modify 字段名 数据类型;

              7      主键自增:字段名 数据类型 primary key auto_increment;

    8、查看表:
                (1)use 库名;
                (2)show tables;
    9、删除表:drop table 表名;
    10、展示表信息:show create table 表名;
                              describe 表名;
                              desc 表名;
    11、修改表名:    alter table 旧表名 rename [to] 新表名;
    12、修改字段:      alter table 表名 change 旧字段名 新字段名 数据类型;
    13、改列类型:      alter table 表名  modify 字段名 数据类型;
    14、添加字段:    alter table 表名 add 新字段名 数据类型 [约束条件] [first/after]
    15、删除字段:    alter table 表名 drop 字段名;
    16、字段自增:primary key auto_increment;
                                    alter table 表名 modify 字段名 新字段类型 auto_increment;
                                            alter table 表名 modify 字段名 新字段类型;

    17、插入记录:insert into 表(字段列表,...)value(值,...);
    18、插入多条:insert into 表 value(值,...),(值,...),(值,...);
    19、记录修改:update 表名 set字段名=value [where 字段名=value];
    20、删除数据:delete [from] 表名 [where 字段名=value];
                                    delete是dml语言,删除表中的记录但不会自增。

    21、清空数据:truncate [table] 表名;
                                    truncate是ddl语言,清空数据表同时清除自增。
    22、查看数据:select *1from 表名;
    23、导入数据: source 路径

    24、查询语句:select [all/distinct]
                            from 表名列表
                            where 条件表达式
                            group by 字段列表 [having 逻辑表达式]
                            order by 字段列表  [asc/desc]
                            limit [offset],n
    25、条件运算符:
                            between...and
                            in
                            like
                            null
                            and、or、not
    26、集合函数:
                            count()
                            sun()
                            avg()
                            max()
                            min()
                            注意:括号里写distinct去掉重复后计算
    
    27、交叉连接:select 字段列表
                            from 表一 [inner] join 表二
                            on 表一.字段名 比较运算符 表二.字段名;

                            select 字段列表
                            from 表一,表二
                            where 表一.字段名 = 表二.字段名 and条件 ;
    
    28、外连接:    select 字段列表
                            from 表一 left/right jion 表二
                            on 表一.字段名 比较运算符 表二.字段名;
    
    29、自连接:    select 字段列表
                            from 表一 as 别名一 [inner] join 表一 as 别名二
                            on 别名一.字段名 比较运算符 别名二.字段名;

    30、比较子查询:select 字段名
                            from 表名
                            where 字段=(select 字段名
                                                from 表名
                                                where 条件    );

    31、储存过程:create procedure 过程名(in/out/inout 参数名 数据类型) begin end

    32、调用存储过程:call 存储过程名();
    
    35、储存函数:create function 函数名(参数名 数据类型)returns 返回值类型 begin return变量名;end

    34、查看过程:select routine_name from information_schema.ROUTINES where routine_schema='表名';

    35、启动事务:start transaction/begin

    36、提交事务:commit

    37、回滚事务:rollback
    
    38、查看隔离级别:select @@global.transaction_isolation,@@transaction_isolation;
        
    39、修改隔离级别:
                读未提交:
                      set session transaction isolation level read uncommitted;
                读提交:(linux默认)
                      set session transaction isolation level read committed;
                可重复读:()
                      set session transaction isolation level repeatable read;
                可串行化:
                      set session transaction isolation level serializable;

    40、建表时创建索引:[索引类型] 索引关键字(字段名)
                   [unique/fulltext/spatial] index/key [别名] (字段[(长度)]) [asc/desc]
    
    41、已存在的表创建索引:create  [索引类型] index 索引名 on 拍(字段名[(长度)] [asc/desc]);

    42、添加索引:alter table 表名 add [索引类型] index 索引名 on 表名(字段名[(长度)] [asc/desc]);

    43、删除索引:drop index 索引名 on 表名;
                                      alter table 表名 drop index 索引名;

    44、定义视图:create view 视图名[(字段列表别名)]as select 语句;

    45、查看视图:describe/desc 视图名
                show table status like'视图名';
                show create view 视图名;

    46、修改视图:create/replace view 视图名 as select语句;

    47、删除视图:drop view 视图名;

    48、视图可以像表一样做更新和删除操作,被操作的视图数据,会直接作用到原表

    49、创建触发器:create trigger 触发器名 after/before insert/update/delete on 表名 for each row begin ~end;

    50、删除触发器:drop trigger 触发器名;

    51、备份数据库:mysqldump -uroot -hhost -p123456 数据库名>路径文件名.sql

    52、数据库恢复:mysql -uroot -p123456 [数据库名]<路径文件名.sql

    53、创建用户:create user '用户名'@'localhost/%/本地主机名' identified by '密码';
                            insert into user(host,user,ssl_cipher,x509_issuer,x509_subject) values('%','用户名','','','');
                            
    54、删除用户:drop user '用户名'@'本地主机名/localhost';
                              delete from mysql.user where Host='localhost' and User='用户名'
    
    55、查看用户:select host,user,password from user;

    56、刷新权限:flush privileges;

    57、修改用户名: rename user '原用户名'@'原本地主机名/localhost' to '新用户名'@'新本地主机名';

    58、修改用户密码: mysqladmin -u用户名 -p原密码 newpwd;
                                alter user '用户名'@'本地主机名'identified by '新密码';

    59、导入数据:source 路径        (注意不加“;”)

    60、更改结束符:delimiter // 

    61、支持储存函数:set global log_bin_trust_function_creators=1;

    62、查看执行顺序:explain ---type:key:rows:

    63、启动MySQL服务:net start mysql

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值