#######################################
索引类似于“书的目录” 索引的优点 加快查询的速度 索引的缺点 减慢写的速度(占用物理存储空间) (1)普通索引 a.创建表时直接设置普通索引 create table stu( >age int, >name char(8), >index(age)); 给age字段创建普通索引,索引名为age show index from stu 查看索引 desc stu /age 字段的key 为MUL b.创建表之后给字段设置普通索引 create index name(索引名) on stu(age(字段名)); c.删除普通索引 drop index name on stu; (2)主键 主键特点:字段内不能有重复值,而且不能赋空值 如果表里已经有主键,不能在创建主键。 a.创建表时直接设置主键 create table stu( >age int, >name char(8), >primary key(name)); 给name字段创建主键 desc stu; /name字段的key为PRI b.创建表之后给字段创建主键 alter table stu add primary key(name); c.删除主键 alter table stu drop primaru key; /删除主键后,值可以重复,但不能赋空值 alter table stu modify name char(8); /如果想赋控制,继续修改字段 d.复合主键:多个字段一起做主键,字段的值不允许同时重复 create table stu( >port int, >cip char(15), >status enum("up","down") default "up" >primary key(port,cip)); 同时设置两个主键,不能一个一个设置 desc stu; insert into stu(port,cip) values(23,"1.1.1.1"); ok insert into stu(port,cip) values(24,"1.1.1.1"); ok insert into stu(port,cip) values(24,"1.1.1.2"); ok insert into stu(port,cip) values(24,"1.1.1.1"); 复合主键时,一个字段内的值可以重复,但不能多字段内的值同时重复 error e.主键与 auto_increment(自加1)连用 一般用于id序号 使用auto_increment 前提是必须要是主键且是数值类型 create table stu( >id int(2) zerofill primary key auto_increment, /int()里面表示显示宽度,不能够限制给字段赋值的大小,大小由类型决定。 zerofill 用0 补位 >port int); insert into stu(port) values(24); desc stu; select * from stu; +++++++++++ id port 01 24 +++++++++++ (3)唯一索引 unique 特点:字段可以赋空值,但不能出现重复 a.创建表时直接设置普通索引 create table stu( >age int, >name char(8), >unique index(name)); 给name字段创建唯一索引 desc stu; /name 字段的key 为 UNI b.创建表之后给字段设置唯一索引 create unique index name(索引名) on stu(name(字段名)); c.删除唯一索引 drop index name on stu; (4)外键 作用:限制给字段赋值。值必须在指定字段值的范围里选择 存储引擎必须为 innodb a.创建表时直接设置主键 指定参考表 create table t1( >age int, >primary key(age) 参考表的参考字段要是索引的一种 >)engine=innodb; /设置表t1存储引擎为engine=innodb 创建表t2 create table t1( >age int, >name char(8), >foerign key(age) references t1(age) 创建外键 >on update cascade 同步更新 >on delete cascade 同步删除 >)engine=innodb; /设置表t1存储引擎为engine=innodb 创建成功后 t2表的age字段的值必须在t1age字段的范围内,t1 age字段的值更新或者删除 t2表相应发生变化(更新或删除) b.创建表之后给字段创建主键 alter table stu add foreign key(age) references t1(age) on update cascade on delete cascade; c.删除主键 show create table t2; /查看建表过程 CONSTRAINT `t5_ibfk_1` FOREIGN KEY 找出t5_ibfk_1 alter table stu drop froeign key t5_ibfk_1; /删除外键。删除后t2表不再受约束。 #############################################
show engines; /查看存储引擎 默认为innodb mysql服务体系结构:(8个功能模块) 连接池 sql接口 分析器 优化器 查询缓存 存储引擎 文件系统 管理工具 存储引擎介绍:mysql数据库服务软件自带的程序。 不同的存储引擎有不同的功能和数据存储方式 是表的处理器 设置表的存储引擎: create table 表名(。。。。)engine=innodb; 修改表的存储引擎 alter table 表名 engine=存储引擎名; 修改表的默认存储引擎 /etc/my.cnf default-storage-engine=myisam myisam特点: 表.MYI 索引信息 表.MYD 数据 表.frm 表结构 支持表级锁 (锁一张表) 不支持事务 事务回滚 innodb特点: 表.frm 表结构 表.ibd 索引信息+数据 支持行级锁 (只给当前被访问的行加锁) 支持事务 事务回滚 锁的作用:解决并发访问冲突问题。 锁类型 :读锁 和 写锁 select update delete insert 事务: 一次数据访问从开始到结束的过程 称为事务 事务回滚: 一次数据访问 任意一步执行失败,恢复所有操作。 事务的特性: 一致性 原子性 隔离性 事务日志文件:记录对innodb存储引擎的表执行过的操作。 工作如何如何决定表使用的存储引擎: 接收写操作多的表适合使用innodb存储引擎。 接收读操作多的表适合使用myisam存储引擎
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。