Mysql 学习记录

( xampp 的mysql 与 直接用 dnf 安装的 mysql 有冲突! )

1. 数据库基本知识:

  一张表的行 又称为 记录

  一张表的列 又称为 字段

  表结构:所有字段,规定了你的每一条记录所应该填的内容,不能随意乱写

 

2.mysql 登录:

  mysql -u root -p password (root密码) 

3. 查看数据库 : show databases;

 

4. 选择数据库: use database-name;

 

5. 数据库文件位置:/var/lib/mysql

 

6.查看表结构:desc table-name; 

 

7.修改表名称:rename table table-name1 to table-name0

 

8.将使用过程中的文件保存至特定文件:(启动时操作)mysql -u root -p password --tee="/filename"

 

9.表字段类型:数值:int, float

         字符串:char(n), varchar(n), text 65535字节, longtext 42亿字节

         日期:date, datetime, timestamp, time, year

 

10.数据字段属性: zerofill,不足指定位数时,自动往前添0

          unsigned,无符号

          auto_increment,自增

          not null,这一列允许为null

                              null, 这一列允许为null

          default,默认值,一般配合 not null 使用

 

11. 帮助: ? word ; 如 ? date

 

12. 查询数据库基本信息:\s

  查看其他可使用的一些帮助命令:\h

 

13.查看数据库或表创建时的信息:show create database database-name;

                show create table table-name;

14. php设置字符集为 utf8, $sql = "set names uft8";

 

15. 在命令(sql语句)前加上 desc: 调试用,显示此条语句的运行相关信息

 

16.命令末尾(分号前)加上 \G : 调整输出信息中行的显示方式为列状

 

17.普通索引: index name(au),如 index in_named(name)

 

18. 查看index:show index from ..  (? show 查看其他show的方法)

 

19. 后期添加普通索引及删除索引:alter table table_name drop index index_name

                 alter table table_name add index index_name(au)

 

20.后期添加/修改/删除字段:alter table table_name add age int ;

             alter table table_name modify age int not null default 20;

              alter table table_name drop age;

 

21.修改列名:alter table table_name change name username varchar(30);

 

22.一次更改多个值:update t1 set id = 77, username = 'h' where id = 7;   // 逗号隔开

 

23.按数值范围操作:如 select id from table_name where id in (1, 4, 5);

          select id from table_name where id >= 1 and id < = 5;

          select id from table_name where id between 1 and 5;

 

24.distinct 取出唯一值:select distinct age from user;

 

25. null : 不能用 = 判断,用 is 方法

 

26.like 关键字:select * from user where name like "%u%"

        % 任意多个字符

        正则:select * from user where name regexp "u"

 

27.使用 limit 限制取出个数:select * from user where name regexp "u" limit 3 (取前3个)

            select * from user where name regexp "u" limit 1, 3(取从第二个开始取2个)

 

28.concat(str1, str2) 连接字符

 

29.rand() 求随机数

 

30.group by 必须放在 order by 前

 

31.group by 必须放在 having 之前,having是对分组的结果进行筛选,此时不能用where

 

32.after :往id字段前插入字段uid  alter table post add uid int unsigned after id;

 

33.left join on : 左连接 ,左边的表显示完全

 

34:php 取数据集:

mysql_fetch_assoc // 关联数组

mysql_fetch_row // 索引数组

mysql_fetch_array // 混合数组

mysql_fetch_object // 对象

 

 

(待继续更新)

posted @ 2015-10-06 22:32  Emerald  阅读(292)  评论(0编辑  收藏  举报