MYSQL数据库-2

一、Null 的主要事项
1. null 意味着没有值 或者 未知值
2. 可以测试某个值是否为null is null
3. 不能对null进行算数运算
4. 所有和努力了进行

二、mysql的索引

  1. 主键索引 primary key
  2. 唯一索引 unique
  3. 常规索引 index
  4. 全文索引 fulltext

(1) 主键索引

主键索引是数据库中最常见的索引类型 主要确定数据表里数据记录的位置 给字段添加primary key 来确定索引值

注意:

  1. 每个表中 只能有一个主键索引

  2. 每个表中最号有一个主键索引 并不是必须的

  3. 主键索引可以有很多的候选项 (auto_increment,not null)

  4. 当表中的数据 被删除以后 auto_increment 依然记录着下一个数据插入的行号值

    • truncate 表名 清空表 并将自增归位
    • alter 表名 auto_increment=1

实例

mysql> create table myindex(
    -> id int unsigned primary key auto_increment not null
    -> );

(2) 唯一索引

唯一索引和主键索引 都一样 不能插入重复的值 不同的是 主键一个表只能存在一个 唯一索引可以存在多个

实例

mysql> create table myunique(
    -> username varchar(20) unique not null
    -> );

(3) 常规索引

常规索引 只作为提高数据的查询效率来使用的

缺点:

  1. 提高了查询效率 但是降低了增删改的效率
  2. 索引文件 占用磁盘空间

数据库文件存放的位置

windwos: C:\ProgramData\MySQL\MySQL Server 5.7\Data\hz03

ubuntu: /var/lib/mysql

(4) 全文索引

fulltext

alter table 表名 add fulltext 索引名称(索引字段)

创建表并添加索引的完整写法

mysql> create table user(
-> id int unsigned primary key auto_increment not null,
-> username varchar(20) unique,
-> age tinyint,
-> index(age)
-> #unique(username)
-> );
#给索引添加索引名称
mysql> create table user2(
    -> username varchar(20),
    -> age tinyint,
    -> unique myname(username),
    -> index aindex(age)
    -> );

例子的创建表

mysql> create table test(
    -> id int unsigned primary key auto_increment not null,
    -> username varchar(50) not null,
    -> userpass varchar(50) not null,
    -> telno varchar(20) not null,
    -> sex enum('w','m') not null default 'm',
    -> birthday date not null default '0000-00-00',
    -> index(username),
    -> index(userpass),
    -> unique(telno)
    -> );

三、表的存储类型

MyISAM和InnoDB

修改类型

alter table 表名 engine=表存储类型;

MyISAM和InnoDB的区别

  1. MyISAM表的存储文件为3个 InnoDB为2个
  2. MyISAM不支持事物 innodb支持
  3. MyISAM不支持外键 innodb支持
  4. MyISAM表的查询效率高于innodb 但是innodb的安全性高于MyISAM

(1) MyISAM的文件说明

  1. .frm文件 存储当前表结构的文件

    在innodb和MyISAM中都存在

  2. .MYD: 即MY DATA 存储表数据的文件

  3. .MYI:即 MY INDEX 存储表索引的文件

(2) InnoDB的文件说明

  1. .frm文件 存储当前表结构的文件

    在innodb和MyISAM中都存在

  2. .ibd 存储表数据和索引

(3) innodb事物处理操作

  1. 将当前表文件存储类型改为 innodb

    alter table 表名 engine=innodb;

  2. 查看当前表的提交类型

    select @@autocommit

    如果值为1 则为自动提交

  3. 改为手动提交(开启事物)

    set autocommit=0

  4. 事物开始
    begin;
  5. 执行各种SQL语句
  6. 提交或者回滚
    1. commit work;
    2. rollback work;

注意:

事物是针对库中表的数据来操作的 并不是针对你当前的库 如果库被干掉了,那就一切都不存在

四、更改表结构

  1. 给当前的表添加一个新的字段 add

    alter table user add sex enum('w','m') default 'w';

  2. 添加新字段在某个字段的后面 after

    alter table user add info varchar(100) default '个人说明' after id;

  3. 添加新字段在第一位 first

    alter table user add infoop varchar(100) default '个人说明' first;

  4. 更改字段名称并更改字段顺序 change

    alter table user change infoop money decimal(6,2);

    alter table user change money money decimal(6,2) after username;

  5. 更改字段类型 modify

    alter table user modify username char(11);

  6. 添加字段索引

    alter table user add index(username); #添加常规索引

    alter table user add unique(username);

    alter table user add index infoindex(info); 添加索引并添加索引名称

  7. 删除索引

    alter table user drop key username;

  8. 创建一个表b和表a一样

    create table b like a;

  9. 查看所有的索引

    show index from 表名;

  10. 更改字段的字符集

alter table user modify username varchar(20) character set utf8;

五、INSERT 数据的添加

  1. 指定字段名称添加数据

    insert into user(username,age,info) values('张三',18,'个人说明');

    需要注意:如果有字段不为空且没有默认值 那么必须插入值

  2. 不指定字段添加值(需要将所有字段都插入值)

    insert into user values(null,'李四',20,'个人说明李四')

  3. 指定字段添加多个值

    insert into user(username,age,info) values('王五',30,'王五的说明'),('赵六',33,'赵六的个人说明')...;

  4. 不指定字段添加多个值

    insert into user values('王五',30,'王五的说明'),('赵六',33,'赵六的个人说明')...;

六、SELECT 数据的查询

  1. 不指定字段查询(查询所有字段)

    select * from 表名;

  2. 指定字段查询

    select 字段名1,字段名2... from 表名;

  3. 查询字段并运算

    select id+age from user;

  4. 起别名

    select username as name from 表名;

    select username name from 表名;

七、where 条件

(1) 比较运算符

  1. >
  2. <
  3. >=
  4. <=
  5. !=/<>
  6. =

(2) 逻辑运算符

  1. 逻辑与 and

    select * from user where username='苍老师' and age<=30;

    俩侧为真才为真

  2. 逻辑或 or

    select * from user where username='苍老师' or age>=30;

    只要满足一个就为真

  3. and 和 or 一起使用

    select * from user where id=1 or (age=18 and sex='w');

  4. 在...内 in

    select * from user where id in(1,3,5,6,7); #和下面的写法一个意思

    select * from user where id=1 or id=2 or id=6;

  5. 不在...内 not in

    select * from user where id not in(1,3,5,6,7); #和下面的写法一个意思

    select * from user where id!=1 and id!=2 and id!=6;

  6. 在...范围内 between ... and ...

    select * from user where age between 20 and 33;和下面的写法一个意思

    select * from user where age>=20 and age<=33;

  7. 不在...范围内 not between ... and ...

    select * from user where age not between 20 and 33;和下面的写法一个意思

    select * from user where age not between 20 and 33;

(3) order by 排序

  1. 默认升序 asc

    select * from user order by age;

    select * from user order by age asc;

  2. 降序 desc

    select * from user order by age desc;

(4) is is not

  1. is not

    select * from user where username is not null;

  2. is

    select * from user where username is null;

(5) limit

limit num 直接取出num条数据

select * from user order by age desc limit 1; #取出年龄最大的一条数据

limit x,num 从x的位置取出 num条数据

你可能感兴趣的:(MYSQL数据库-2)