mysql基础总结

小结:

1、查找mysql设置参数:

show variables like 'sql_safe_updates';

2、having 是针对结果集,而where是在磁盘里查找,应该是现有where查找 才有having在内存中操作;

3、count(0),count(1),count(*)结果都一样,count(列)排除null,有时候将列数据转化为0或1,通过sum来统计,而不用count,参考求平均成绩

4、查询顺序:where  group by  having  order by  limit

5、exists子查询的句子:  select * from category where exists (select cat_id from goods where goods.cat_id = category.cat_id);

6、列类型:

  A整型:

    tinyint(1字节),smallint(2),mediumint(3),int(4),bigint(8),unsigned无符号,zerofill用0填充

    int(M),M为宽度,没有zerofill就没有任何意义

  B浮点型(有精度损失,精确度):

    float(单精度浮点型),double(双精度浮点型),

    float(M,D),M代表精度,总位数,D代表标度,小数点后的位数,

    比如float(5,2)能存的就是总位数小于等于5位,小数点后位数小于等于2为,比如999.99,1000就不行

  C定点型(无精度损失):

    decimal(定点型,十进制,小数的,精确的)

  D字符串类型:

    定长:char(M),当存储的数据长度不足M个时,char会用空格来占满,所以如果数据后本身有空格,会造成数据的不准确

    变长:varchar(M)

    枚举:enum

    集合:set

    文本:text/blob

  E日期时间类型:

    year年,date日期,time时间,datetime日期时间,timestamp时间戳类型(插进去的数据为datetime)

7、建表:

  auto_increment 只能有一列,并且那一列必须定义索引key(普通索引也可以),一般auto_increment与primary key成对出现
8、修改表:

  增加列:alter table user add column height int not null default 0 after age;

  删除列:alter table user drop column height;

  修改列: 1使用change(改变):alter table user change height shenggao smallint   

      2使用modify(稍做调整): alter table user modify height int

9、视图:

  视图是sql的查询结果,又称为虚拟表,

  视图能不能修改,删除,添加:如果视图的每一行与物理表一一对应,则可以,如果视图是由物理表多行经过计算得到的,则不可以

  create view userview as select name,age from user;

10、查看表信息:

  show table status where name='goods',

  show table status 中有一行Comment可以判断是否为视图

  改表名: rename table goods to newgoods
11、字符集:

  character_set_client(客户端),connection(连接器),results(结果)

12、索引(index/key):

  索引是数据目录,能快速定位数据的位置,索引提高了查询速度,降低了增删改的速度,在查询频繁度高,重复度低的列加索引

  建表时声明索引,

  主键索引:primary key(columnname)

  普通索引:key keyname(columnname)

  唯一索引:unique key keyname(columnname),还可以约束数据不能重复

  全文索引在中文条件下无效,用分词➕索引,解决方案sphinx

  key email(email(10)),针对email前10个字符建立索引,

  多列索引: key keyname(column1,column2)

  冗余索引: key xm(xing,ming),   key ming(ming)

  查看索引:1show index from user;2show create table user;

  删除索引:1alter table user drop index 索引名,  alter table user drop primary key;  2drop index 索引名 on user,

  添加索引:alter table user add index/key xm(xing,ming),  alter table user add unique key/index name(name),  alter table user add primary key (xing)

  思考只有xing,ming两列,key xm(xing,ming),explain select * from x where ming='元璋',也会用到索引,但是多加一列,则不会用到索引,因为需要回行查找多的那一列。

13、事务:

  myisam不支持事务,innodb支持

  事务的三种特性:

    隔离性:看不到中间状态

    1、原子性:rollback回到事务之前,commit来到事务之后

    2、一致性:事务之前和之后业务数据保持一致

    3、持久性:事务commit后不能rollback

    

posted @ 2019-08-24 15:03  仁义礼智信的  阅读(138)  评论(0编辑  收藏  举报