# 2016年2月27日

## php

### mysql 聚合函数   mysql 提供的系统实现特定功能的函数

1. 算术函数 sum() 求和 select sum(btypeid) from books; avg() 求平均值: select avg(btypeid) from books; count() 统计记录数 select count(*) from books ; select count(*) from books where btypeid=2 **\***
2. 字符串: concat(str1,str2,str3.....); 拼接 select concat(bName,bTypeid) as p from books;

一般情况: 很少 在mysql语句上使用函数，加中mysql负担.

### mysql 中的索引

* 索引:  类似于查询目录  索引: 是以文件的形式存储的.

特点: 数据的更新和索引的更新 是同步的。 作用： 提高查询效率。

* 索引的类型:

1.主索引： primary key ， 主键 确定唯一记录的。 where id=???? id int auto\_increment primary key,

2.普通索引;

最基本的索引，可以加在多列上 创建表的时候添加

```php
    create table student(
        id  int(6) auto_increment primary key,
        stuNum
        stuName
        stuAge
        stuSex
        stuTel,
        key 索引名称( 字段)
    )
    create table users(
        id  int(6) auto_increment primary key,
        name varchar(20),
        pwd varchar(20),
        key dt (name)   ,
        key dp (pwd)
    )
对已经有的表添加
    alter table 表名称 add index 索引名称(字段1)
    alter table student add index ts (stuNum);
    alter table student add index ts1 (stuName);
```

3.唯一索引;

唯一索引 可以加载多列上，列上加了唯一索引 该列值不能重复

```php
                创建表
                    create table student(
                        id  int(6) auto_increment primary key,
                        stuNum
                        stuName
                        stuAge
                        stuSex
                        stuTel,
                        unique key 索引名称(字段)
                    )
                修改表
                alter table 表名称 add unique 索引名称(字段);
         全文索引:   加载有大段字符串 的文本上。 了解***
            创建  表:
                    create table student(
                        id  int(6) auto_increment primary key,
                        stuNum
                        stuName
                        stuAge
                        stuSex
                        stuTel,
                        fulltext key 索引名称(字段)
                    )
                修改:
                    alter table 表名称 add fulltext 索引名称(字段名称)
                目前不支持中文
```

注意:

1. 主索引
2. 唯一索引
3. 普通索引
4. 全文索引

索引的缺点:

1. 数据索引同步更新，全有索引，降低效率。
2. 索引是以文件的形式存储的，索引过多 ，索引文件会很大。        &#x20;
3. 用户名  手机  性别 商品名称 价格  供应商 都是要加索引的  类型。

### mysql 外键

* 什么是外键

表与 表之间的特定关系，保持了数据的完整性和一致性。 user 用户 order 订单 用户下订单 1》删除用户，没有删除订单，数据就不一致 2》order 表中插入记录\
外键控制：让用户不更新数据，或者是用户删除数据的时候，让 订单同步也删除。

* 外键特点:

1》innodb 类型 查看 show create table 表名 存储引擎engine=值（innodb） 2》外键是两张表的约束关系。 3》外键的 名称在唯一。

* 创建外键:

```php
        create table  表名（
            列定义
            索引定义
            外键定义: [constraint 约束名称] foreign key 外键名称
                            references [外键名称]（外键字段）
                            【on delete {restrict | cascade | set null | no action}】
                            【on update {restrict | cascade | set null | no action}】
        ）
        restrict: 拒绝对父表的删除或更新操作
        cascade: 父表的删除或更新，自动删除或更新 子表中对应的记录。
        set null : 父表删除 更新，设置字表外键字段 null
        no action ：不作为。
```

* demo

```php
        create table user(
            id int(6) auto_increment primary key,
            name varchar(30),
            sex int(1)
        )engine=innodb
        insert into user(name ,sex) values("zhangsan",1);
        insert into user(name ,sex) values("lisi",2);
        insert into user(name ,sex) values("wangwu",1);

        create table orders(
            order_id int(6) auto_increment primary key,
            u_id  int(6) ,
            username varchar(30),
            money int(6),
            datatime date,
            key ud (u_id),
            foreign key order_f_key (u_id) references user(id)
        )engine=innodb
        insert into orders(u_id,username,money,datatime) values(1,"zhangsan",300,"2012-02-02");
        insert into orders(u_id,username,money,datatime) values(1,"zhangsan",500,"2012-02-04");
        insert into orders(u_id,username,money,datatime) values(2,"lisi",700,"2012-02-04");
        insert into orders(u_id,username,money,datatime) values(2,"lisi",900,"2012-02-04");
        insert into orders(u_id,username,money,datatime) values(3,"wangwu",100,"2012-02-04");
        insert into orders(u_id,username,money,datatime) values(3,"wangwu",200,"2012-02-04");



        insert into orders(u_id,username,money,datatime) values(4,"aaaaa",200,"2012-02-04");
```

* 通过修改表 来改变外键的的属性

alter table 表名称 add foreign key 外键名称(外键字段) references 主表（字段） on delete/ on update casecade alter table orders add foreign key (u\_id) references user(id) on delete cascade;

* 删除外键:

alter table 表名称 drop foreign key 外键名称


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://note.niefee.com/summary/2/2016-nian-2-yue-27-ri.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
