码迷,mamicode.com
首页 > 数据库 > 详细

Mysql基础第二部分,针对以后python使用

时间:2018-04-17 22:26:53      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:mit   AC   排序   record   val   username   sel   add   创建表   

#外键  表与表相连 列名 MUL 外键 参照完整性 不能添加另一个表没有的字段
create table study_record(
id int auto_increment primary key,
day int not null,
status char(32) not null,
stu_id int not null,
#创建外键
key ‘fk_student_key‘ (‘stu_id‘), #起个名字
constraint ‘fk_student_key‘ foreign key ("stu_id") references ‘student‘ (‘id‘)
)
#若表1是参照的 表2是跟随的 如果删除2 是可以的! 但是如果删除1 是不可以的


#NULL 值处理
#IS NULL 如当前值为空 则返回true
#IS NOT NULL 不为空
#不能用 = NULL 或者!= NULL 永远记住 NULL值与任何比较都返回false

#mysql连接多表查询
# left join
#right join
#inner join 内连接 获取两个表匹配记录
#full join

create table A (a int not null )
create table B (b int not null)
insert into A (a) values(1);
insert into A (a) values(2);
insert into A (a) values(3);

insert into B (b) values(4);
insert into B (b) values(5);
insert into B (b) values(6);
求交集 内连接 inner join
select * from A inner join B on A.A = B.b;
select a.*, b.* from A,B where A.a=B.b;

求差集 left join
select * from A left join B on A.a = B.b;

求差集 反向的left join
select * from A right join B on A.a = B.b;

求并集 full join mysql是不支持full join 一般并集无用
select * from a full join B on A.a = B.b;

并集 MySQL
select * from A left join B on A.a = B.b union select * from A right join B on A.a = B.b;


事务必须满足四个条件
原子性(atomicity) 要么成功要么撤回
稳定性(consistency) 有非法数据时 事务撤回
隔离性Isolation 事务独立运行 一个事务处理后的结果影响事务 name其他事务会撤回
可靠性durability 硬件奔溃的时 InnoDB数据会利用日志文件重构修改 可靠和高速度不可兼容
#数据库 成批的 要么都成功 要么都失败
修改 删除 插入 才需要事务
默认为 Innodb 只有innodb才支持事务 mysql sum不支持事务

开启一个事务 begin;
select * from student;
insert into student (name , register_date, gender ) values(‘ljc‘, ‘2014‘, ‘M‘);
insert into student (name , register_date, gender ) values(‘ljc‘, ‘2014‘, ‘M‘);
已经插入表里
如果出现问题 可回滚 输入rollback; 或者会自动回滚 (id会自增 但不影响)

begin;
insert into student (name , register_date, gender ) values(‘ljc‘, ‘2014‘, ‘M‘);
insert into student (name , register_date, gender ) values(‘ljc‘, ‘2014‘, ‘M‘);
commit 确认提交


索引 #使表查询更快 索引可以有很多个!! mysql索引是通过二叉树(B tree)实现 和hash差不多
单列索引 和 组合索引(两个列)就是想两个列加起来唯一!!!
创建索引时 记住查询时用索引查询!!
索引也为表 一个排序好了的数字表
但是 过多索引也会有缺陷 更新表时,也需要更新数字表,需要重新排数据表!! 所以更新时速度会变慢
增加磁盘空间!!
主键就是索引!!!

查看索引 show index from 表名
show index from 表名;
create index index_name on 表名(列名()) #index_name 是key_name 与原表无关系 随意定
create index index_name on student(name(32))

修改表结构添加
alter student add index index_name on (列名())

创建表添加
create table student(id int not null, index index_name (列名(长度)))
create table student (id int not null, index index_name (name(32))); ************

删除索引
drop index index_name on 表名; show index from student
drop index index_name on student;

唯一索引 索引列值必须唯一 可以有空值 主键就是 区别就是添加unique
alter student add unique index_name on (username(32))
create table student(unique index_name (username(32)))
create unique index index_name on student(username(32))

使用alter添加和删除主键
alter table student modify id int not null;
alter table student add primary key(id);

alter删除主键
alter table student drop primary key;不知道索引名时删除
alter table student drop primary key(id); ???????????

show index from student\G 不需要添加; 将数值列表名都列表显示

Mysql基础第二部分,针对以后python使用

标签:mit   AC   排序   record   val   username   sel   add   创建表   

原文地址:https://www.cnblogs.com/Liang-jc/p/8870080.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!