MySQL——2

复习

1.Mysql介绍,安装,语法规则
2.Mysql
-服务端
-客户端
3.通信交流(连接)
-授权
-Mysql语句
1.数据库
-create database db1 default charset=utf8;
-drop database db1;
2.表
-create table name(
id int not null auto_increment primary key,
username char(32), # 32代表字符
constraint fk_name_other foreign key (`name`) references other (`cid`)
)engine=innodb default charset=utf8
# 数据框中下拉框,考虑用外键做
# 建立了外键,增加和删除操作均会被约束
3.行
-insert into table(column) values(content)
-select * from table
-delete from table where condition
-truncate table tb1;
-updata table set id='' where id=''

学习内容

1.主键作用
1.一张表只能有一个主键
2.一个主键不一定是一列;可以多列合并作为主键
create table t1(
tid int not null auto_increment,
sid int not null,
primary key (tid,sid)
)engine=innodb default charset=utf8;
2.关于自增
-desc tablename;
-show create table tablename \G; #\G为反向查看,方便审阅
-alter table tablename auto_increment=10;
1.自增步长
-Mysql 基于会话级别
1.show session variables like 'auto_inc%'; #类似于内存中的变量
2.set session auto_increment_increment=2;
-Mysql 基于全局级别
1.show global variables like 'auto_inc%'
2.set global auto_increment_increment=2;
-SqlServer 可通过设置每个表的参数,设置步长
3.外键的变种
1.唯一索引
create table ti(
id int ,...
num int,
unique uq1 (num)
)...
-唯一索引不能重复,但可以有一个为空,加速查找
-主键,不能重复且不能为空
create table ti(
id int ,...
num int,
xx int,
unique uq1 (num,xx)
)...
-联合唯一索引,num 和 xx 不完全相同
2.变种1
-外键+唯一索引,可以构建一对一的对应表关系
例:每个博客园用户只能有一个博客地址
表1:用户表
表2:博客地址对应表 --采用外键+唯一索引
-单独的外键叫一对多关系
3.变种2
-外键+联合唯一索引,构建多对多的对应表关系
例:用户表和主机表,每个用户可使用多个主机,每个主机可以服务多个用户
4.Mysql 语句数据行操作
1.增
-insert into tablename(column) values(content);
-insert into tablename(column) values(content1),(content2),...;
-
2.删
-delete from tb1;
-delete from tb1 where id=1 and name ='alex'
3.改
-update 表 set name='alex' where id >1
4.查
-select nem,id ,gender as gg from 表 where id >1;

a.条件
-select nem,id ,gender as gg from 表 where id >1 and name='alex';
-select nem,id ,gender as gg from 表 where id between 5 and 16;
-select nem,id ,gender as gg from 表 where id in (1,3,8);
-select nem,id ,gender as gg from 表 where id in (select nid from 表);
b.通配符
-select * from 表 where name like 'a%'
-select * from 表 where name like 'a_'
c.分页
-select * from 表 limit 5;
-select * from 表 limit 4,5;
-select * from 表 limit 5 offset 4;
d.排序
-select * from tablename order by 列 desc;
-select * from tablename order by 列 asc;
e.分组*****
-select num from 表 group by num;
-select num, id, count(*) from 表 where id >10 group by num order id by desc;
聚合函数:
max;min;avg;count;sum
# 对于聚合函数结果进行二次筛选,必须使用having
-select num, id, count(*) from 表 where id >10 group by num having count(id)<10;
f.连接*****
select * from score
left join student on score.student_id=student.sid
left join course on score.course_id=course.cid
left join class on student.class_id=class.cid
left join teacher on course.teach_id=teacher.tid;
posted @ 2019-05-28 17:24  王二被占用  阅读(202)  评论(0)    收藏  举报