MySql常用命令集合

1、库的管理
        1、创建库
            create database 库名 character set utf8;
        2、查看创建库语句
            show create database 库名;
        3、查看当前所在库
            select database();
        4、切换库
            use 库名;
        5、删除库
            drop database 库名;
        6、查看库中已有表
            show tables;
2、表的管理
        1、创建表
            create table 表名(....)character set utf8;
        2、查看存储引擎和字符集
            show create table 表名;
        3、查看表结构
            desc 表名;
        4、删除表
            drop table 表名;
        5、表的复制
            create table 表名 select ... from 表名 ... where...;
            只复制表结构:create table 表名 select * from 表名 where false;
        6、表的重命名:
            alter table 表名 rename 新表名;
3、表记录的管理
        1、插入表记录
            1、insert into 表名 values(),(),...;
            2、insert into 表名(..,..,..) values(),(),...;
        2、查询表记录
            1、select * from 表名 where 条件;
            2、select ..,..,.. from 表名 where 条件;
    
        3、删除:
            delete from 表名 where 条件;

        4、更新 :
            update 表名 set 字段名=值,... where 条件;
            
4、表字段的操作
            
            alter table 表名 执行动作;
        1、添加字段(add)
            alter table 表名 add 字段名 数据类型;
            alter table 表名 add 字段名 数据类型 first;
            alter table 表名 add 字段名 数据类型 after 字段名;

        2、删除字段(drop)
            alter table 表名 drop 字段名;

        3、修改字段数据类型(modify)
            alter table 表名 modify 字段名 新数据类型;
            # 修改数据类型会受到表中原有数据的限制

5、SQL查询
    1、总结(执行顺序)
            3、select ...聚合函数 from 表名
            1、where ...
            2、group by ...
            4、having ...
            5、order by ...
            6、limit ...
    2、嵌套查询(子查询)
    
            SQL查询语句 where 条件(SQL查询语句)

6、索引
    1、普通索引(index)/唯一索引(unique)
        1、创建
            1、创建表
             ... index/unique(字段名),index/unique(字段名));
            2、已有表
            create index/unique index 索引名 on 表名(字段名);
        2、查看索引
            1、desc 表名;
            2、show index from 表名;
        3、删除index
            drop index 索引名 on 表名;
 

    2、主键索引(primary key,PRI)&&自增长属性(auto_increment)配合主键一起使用
            alter table 表名 ....
        1、创建
            1、创建表
    
            ... id int primary key auto_increment,
            ... ...)[auto_increment=10000];
            ... id int auto_increment,
            ... primary key(id))[auto_increment=1000];
            2、在已有表中创建
            alter table 表名 add primary key(字段名);
        2、删除主键
            1、先删除auto_increment属性
            alter table 表名 modify id int;
            2、再删除主键
            alter table 表名 drop primary key;
        3、已有表添加自增长属性并指定起始值
            1、添加自增长属性
            alter table 表名 modify id int auto_increment;
            2、指定起始值
            alter table 表名 auto_increment=值;

    3、外键索引(foreign key)
            1、创建表
    
            ... [constraint 外键名]foreign key(参考字段名)
            references 被参考表名(被参考字段名)
            on delete 级联动作
            on update 级联动作

            2、在已有表中创建
            alter table 表名 add [constraint 外键名]
            foreign key(参考字段名)
            references 被参考表名(被参考字段名)
            on delete 级联动作
            on update 级联动作
        级联动作:cascade;restrict(默认);set null;no action 

7、数据导入
        步骤: 创建表 --> 文件拷贝到搜索路径(show variables like "secure_file_priv";)--> 导入
            load data infile "文件名"
            into table 表名
            fields terminated by "分隔符"
            lines terminated by "\n"
            (csv逗号)
        
8、数据导出
            select ... from 表名
            into outfile "文件名"
            fields terminated "分隔符"
            lines terminated by "\n"
   
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值