Loading

增删改查

DDL

数据定义语言

数据库

  1. 创建

    mysql> create database db1 charset utf8mb4;	//创建数据库并设置字符集为utf8mb4
    
  2. 删除

    mysql> create database db1 charset utf8mb4;
    
  3. 查询

    mysql> show databases;				//查询所有的数据库
    mysql> show create database db1;	//查询数据库db1的建表语句
    
  4. 修改

    mysql> alter database db1 charset utf8;	//修改数据库db1字符集为utf8
    
  5. use

    mysql> use db1;	//切换数据库,类似Linux中的cd命令
    

  1. 创建

    mysql> create table tb1(
        -> id 	int not null primary key comment '工号',
        -> name varchar(10) not null comment '姓名'
        -> )engine innodb charset=utf8 comment '个人信息表';
    
  2. 删除

    mysql> drop table tb1;
    
  3. 查询

    mysql> desc tb1;
    mysql> show tables;
    mysql> show create table tb1;
    
  4. 修改

    mysql> alter table tb1 add gender enum('boy','girl') not null comment '性别';
    //在表的最后添加一列
    mysql> alter table tb1 add age tinyint not null comment '年龄' after id;
    //在id的后边添加一列
    mysql> alter table tb1 add time timestamp not null default now() comment '时间' first;	//在表的头部添加一列
    mysql> alter table tb1 drop time;	//删除time列
    mysql> alter table tb1 modify name varchar(10) not null;	//modify只可以修改数据类型
    mysql> alter table tb1 change name myname char(10) not null;	//change可以修改名称和数据类型
    

DML

对表中的数据进行增、删、改

  1. insert

    mysql> insert into tb1 (time,id,age,name,gender)	//()中的属性如果是默认的顺序可以不写
    	-> values 
    	-> (now(),1,17,'Tom','boy'), 
    	-> (now(),2,23,'Jerry','girl');
    
  2. update

    mysql> update tb1 
        -> set age = 8
        -> where id = 1;
    
  3. delete

    mysql> delete from tb1 		//delete: DML操作, 是逻辑性质删除,逐行进行删除,速度慢.
        -> where id = 1;
    

    全表删除

    mysql> truncate table tb1;	//truncate: DDL操作,对与表段中的数据页进行清空,速度快.
    

select

聚合函数

max()  		    :最大值
min() 	 	    :最小值
avg() 	 	    :平均值
sum() 		    :总和
count()		    :个数
group_concat()  :列转行
  1. from

    mysql> select * from tb1;
    mysql> select id,name from tb1;
    mysql> select max(id) from tb1;
    
  2. where(and/or)

    mysql> select * from tb1
        -> where id = 1;
        
    mysql> select * from tb1  
    	-> where id = 1 and age > 18;
    	
    mysql> select * from tb1 where name like 'J%'; //模糊查询,%不能放在前面,因为不走索引
    
    mysql> select * from tb1 where age in (17,23);
    
    mysql> select * from tb1 where age > 10 and age < 30;
    mysql> select * from tb1 where age between 10 and 30;
    
  3. group by

    根据 by后面的条件进行分组,方便统计,by后面跟一个列或多个列

    一般配合聚合函数使用

    mysql> select gender,sum(age) from tb1 group by gender;
    
  4. having

    功能和where一样,跟在group by之后

    mysql> select * from tb1 
    	-> where age > 10 
    	-> group by gender
    	-> having id < 10;
    
  5. order by + limit

    实现先排序,by后添加条件列

    mysql> select * from tb1 
    	-> order by age desc;
    
  6. distinct

    去重复

    mysql> select distinct(name) from tb1;
    
  7. 联合查询

    mysql> select * from tb1 where name in ('Tom','Jerry');
    
    mysql> select * from tb1 where name = 'Tom'
        -> union all
        -> select * from tb1 where name = 'Jerry';
        
    说明:一般情况下,我们会将 IN 或者 OR 语句 改写成 UNION ALL,来提高性能
    UNION     去重复
    UNION ALL 不去重复
    
  8. join

    多表连接

    mysql> select tb1.name,tb2.address  
        -> from tb1 join tb2 on tb1.id = tb2.id;
    
  9. information_schema.tables

    DESC information_schema.TABLES
    TABLE_SCHEMA    ---->库名
    TABLE_NAME      ---->表名
    ENGINE          ---->引擎
    TABLE_ROWS      ---->表的行数
    AVG_ROW_LENGTH  ---->表中行的平均行(字节)
    INDEX_LENGTH    ---->索引的占用空间大小(字节)
    
  10. show

    show databases;                     #查看所有数据库
    show tables;                        #查看当前库的所有表
    SHOW TABLES FROM                    #查看某个指定库下的表
    show create database world          #查看建库语句
    show create table world.city        #查看建表语句
    show grants for  root@'localhost'   #查看用户的权限信息
    show charset;                      #查看字符集
    show collation                      #查看校对规则
    show processlist;                   #查看数据库连接情况
    show index from                     #表的索引情况
    show status                         #数据库状态查看
    SHOW STATUS LIKE '%lock%';          #模糊查询数据库某些状态
    SHOW VARIABLES                      #查看所有配置信息
    SHOW variables LIKE '%lock%';       #查看部分配置信息
    show engines                        #查看支持的所有的存储引擎
    show engine innodb status\G         #查看InnoDB引擎相关的状态信息
    show binary logs                    #列举所有的二进制日志
    show master status                  #查看数据库的日志位置信息
    show binlog evnets in               #查看二进制日志事件
    show slave status \G                #查看从库状态
    SHOW RELAYLOG EVENTS                #查看从库relaylog事件信息
    desc (show colums from city)        #查看表的列定义信息
    
posted @ 2020-07-11 23:14  北兢王  阅读(78)  评论(0)    收藏  举报