1.MySQL学习笔记
两种数据库
关系型数据库:sqllite,db2,oracle,access,sql,server,mysql
非关系型数据库:MongoDB,redis
在安装、配置完环境变量和服务之后,
#这两句都需要在管理员权限下的cmd中运行
net start mysql #打开服务器端
#打开服务器端之后需要登录
mysql -u root -p #之后再输入密码
#打开服务器端之后需要登录
net stop mysql #关闭服务器端
学习SQL语句规则
操作文件夹
文件夹级别的不能修改,想要修改只能删掉重来
create database db1; #创建一个数据库db1
create database db1 default charset utf8;
show databases; #展示都有什么数据库
drop database db1; #删除数据库db1
操作文件
use db1;
show tables;
create table t1(id int,name char(10));
create table t1(id int,name char(10)) default charset=utf8;
create table t1(id int,name char(10)) engine=innodb default charset=utf8;
#innodb 支持事务,原子性操作
#myisam 不支持事务,但这个速度快
create table t1(
列名 类型 not null default 1 auto_increment primary key,
id int,
name char(10)
) engine=innodb default charset=utf8;
#一个表里面只能有一个自增列,一个主键
#设置自增列的话,还需要和主键绑定,要不会出错
auto_increment:表示自增
primary key:(主键)
1.表约束(不能重复并且不能为空)
2.加速查找
null:可为空
not null:不可为空
default 1:默认是多少
基本数据类型:(常用的)
1.数字类型:
1.1.1 int
1.1.2 tinyint
1.1.3 bigint
1.2.1 float(表小数不精准)
1.2.2 double(表小数不精准,比float精准点)
1.2.3 decimal(精准,它内部以字符串存储)
1.2.3.1 decimal(10,5)第一个数表小数点前和小数点后一共多少位,第二个数表小数点后最多多少位,
2.字符类型:(里面数字表示长度,可以改变)
#在创建数据表的时候,把定长的列放前面,变长的放后面
char(10) 不管数据长度登不等于10,都占10个位置(定长)
好处:速度快
缺点:浪费空间
varchar(10) 最多给10个位置,数据少的话就会灵活给空间(变长)
好处:节省空间
缺点:速度慢
#这两个类型的最大字符数都是255个字符
需要大数量数据类型的时候:
text:
上传文件:
文件存硬盘
路径存DB
3.时间类型:
datatime
4.枚举(enum)
#只能在枚举规定的那些里面插入值等
create table shrets(
name varchar(40),
size enum('L','XLL','XL')
)
insert into shirts(name,size) values('shd','XL')
5.集合(set)(用的很少)
#只能插入set规定的值的组合
create table shrets(
name varchar(40),
size set('L','XLL','XL')
)
insert into shirts(name,size) values('shd','XL,L')
清空表:
delete from t1; #如果此表中有自增列,清空表之后,再插入数据自增列会接着之前的数据显示
truncate table t1;#这个速度快,并且清空之后,再插入数据自增列会从1开始
删除表:
drop table t1;
操作文件中的内容
#插入数据
insert into t1(id,name) values(1,'wang');
#删除:
delete from t1;#清空全部行
delete from t1 where id<6;#删除指定的行
#修改:
update t1 set age=18;#这个会将age全部改为18
update t1 set age=18 where age=17;#将age=17全部改为18
#查看数据
select * from t1;#这个效率低,因为数据量大,工作之后最好不要这样查找
select id,name from tb;
外键
外键:
- 创建的时候有约束
- 删除的时候也有
constraint 外键名字(随便写一个) foreign key (本表的属性) references 联系表名 (联系表属性)
create table userinfo(
uid bigint auto_increment primary key,
name varchar(32),
department_id int,
constraint fk_user_depar foreign key(department_id)references department(id)
)engine=innodb default charset=utf8;
create table department(
id bigint auto_increment primary key,
title char(15)
)engine=innodb default charset=utf8;
主键
- 一个表只能有一个主键
- 主键可以由多列组成
CREATE TABLE t5 (
nid int(11) NOT NULL AUTO_INCREMENT,
pid int(11) not NULL,
num int(11),
primary key(nid,pid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
create table t6(
id int auto_increment primary key,
name char(10),
id1 int,
id2 int,
CONSTRAINT fk_t5_t6 foreign key (id1,id2) REFERENCES t1(nid,pid)
)engine=innodb default charset=utf8;
数据行
######插入
insert into tb1(name,age) values('alex',18);
####删除####
#如果此表中有自增列,清空表之后,再插入数据自增列会接着之前的数据显示
delete from t1;
#这个速度快,并且清空之后,再插入数据自增列会从1开始
truncate table t1;
#有条件的删除
delete from tb1 where id > 10
#####更新数据
update tb1 set name='root' where id > 10
######查找
select * from tb;#这个效率低,因为数据量大,工作之后最好不要这样查找
select id,name from tb;
自增补充
对于自增补充:
desc t10;#查看表的字段,是否自增等等
show create table t10;#查看表是怎么创建的
show create table t10 \G;#竖着看
alter table t10 AUTO_INCREMENT=20;#指定这个AUTO_INCREMENT=20
#AUTO_INCREMENT=20致使下一个插的数据是自增列的第几个
MySQL: 自增步长
基于会话级别:(一次登录就是一次会话)
show session variables like 'auto_inc%'; 查看全局变量
set session auto_increment_increment=2; 设置会话步长
# set session auto_increment_offset=10; #设置起始值
基于全局级别:(全局的最好不要用)
show global variables like 'auto_inc%'; 查看全局变量
set global auto_increment_increment=2; 设置会话步长
# set global auto_increment_offset=10; 设置起始值
SqlServer:自增步长:
基础表级别:
CREATE TABLE `t5` (
`nid` int(11) NOT NULL AUTO_INCREMENT,
`pid` int(11) NOT NULL,
`num` int(11) DEFAULT NULL,
PRIMARY KEY (`nid`,`pid`)
) ENGINE=InnoDB AUTO_INCREMENT=4, 步长=2 DEFAULT CHARSET=utf8
CREATE TABLE `t6` (
`nid` int(11) NOT NULL AUTO_INCREMENT,
`pid` int(11) NOT NULL,
`num` int(11) DEFAULT NULL,
PRIMARY KEY (`nid`,`pid`)
) ENGINE=InnoDB AUTO_INCREMENT=4, 步长=20 DEFAULT CHARSET=utf8
唯一索引
唯一索引
create table t1(
id int ....,
num int,
xx int,
unique 唯一索引名称(可以随便写) (列名,列名),
constraint ....
)
唯一:
约束不能重复(可以为空,但最多能有一个,这样就能保证不重复)
加速查找
PS: 主键不能重复(不能为空)
外键的变种
#一对一
#一对多
#多对多
create table userinfo1(
id int auto_increment primary key,
name char(10),
gender char(10),
email varchar(64)
)engine=innodb default charset=utf8;
create table admin(
id int not null auto_increment primary key,
username varchar(64) not null,
password VARCHAR(64) not null,
user_id int not null,
unique uq_u1 (user_id),
CONSTRAINT fk_admin_u1 FOREIGN key (user_id) REFERENCES userinfo1(id)
)engine=innodb default charset=utf8;
===》多对多
create table userinfo2(
id int auto_increment primary key,
name char(10),
gender char(10),
email varchar(64)
)engine=innodb default charset=utf8;
create table host(
id int auto_increment primary key,
hostname char(64)
)engine=innodb default charset=utf8;
create table user2host(
id int auto_increment primary key,
userid int not null,
hostid int not null,
unique uq_user_host (userid,hostid),
CONSTRAINT fk_u2h_user FOREIGN key (userid) REFERENCES userinfo2(id),
CONSTRAINT fk_u2h_host FOREIGN key (hostid) REFERENCES host(id)
)engine=innodb default charset=utf8;
SQL语句数据行操作补充
SQL语句数据行操作补充
create table tb12(
id int auto_increment primary key,
name varchar(32),
age int
)engine=innodb default charset=utf8;
增
insert into tb11(name,age) values('alex',12);
#可一次插入多条数据
insert into tb11(name,age) values('alex',12),('root',18);
#将tb11中的数据插入到tb12中
insert into tb12(name,age) select name,age from tb11;
删
delete from tb12;
delete from tb12 where id !=2
delete from tb12 where id =2
delete from tb12 where id > 2
delete from tb12 where id >=2
delete from tb12 where id >=2 or name='alex'
改
update tb12 set name='alex' where id>12 and name='xx'
update tb12 set name='alex',age=19 where id>12 and name='xx'
查
select * from tb12;
select id,name from tb12;
select id,name from tb12 where id > 10 or name ='xxx';
#改变打印出来的表头(下面这行改变的是name,改成了cname)
select id,name as cname from tb12 where id > 10 or name ='xxx';
#加常数之后,会增加一列,这一列全部都是这个常量
select name,age,11 from tb12;
其他:
select * from tb12 where id != 1
select * from tb12 where id in (1,5,12);
select * from tb12 where id not in (1,5,12);
select * from tb12 where id in (select id from tb11)
#是闭区间
select * from tb12 where id between 5 and 12;
通配符:
#以a开头的多个字符
select * from tb12 where name like "a%"
#以a开头的一个字符
select * from tb12 where name like "a_"
#只要有a就行
select * from tb12 where name like "a%"
#分页:
#为什么分页? 因为一次请求太多数据或者返回太多数据会让数据库,浏览器等崩溃
#查看前10条
select * from tb12 limit 10;
#起点是第0条,查看第0条之后的10条
select * from tb12 limit 0,10;
#起点是第10条,查看第10条之后的10条
select * from tb12 limit 10,10;
#起点是第20条,查看第20条之后的10条
select * from tb12 limit 20,10;
#起点是第20条,查看第20条之后的10条(和上面那个功能一样,但是写法是反着的)
select * from tb12 limit 10 offset 20;
#####举个应用场景####
# page = input('请输入要查看的页码')
# page = int(page)
# (page-1) * 10
# select * from tb12 limit 0,10;
# select * from tb12 limit (page-1) * 10,10;
排序:
select * from tb12 order by id desc; #大到小
select * from tb12 order by id asc; #小到大
#多列排序,先按照第一个排序,重复的按照后面的排序
select * from tb12 order by age desc,id desc;
#取后10条数据
select * from tb12 order by id desc limit 10;
#分组:
#这个是根据group by 后面的属性分组,下面这个也就是根据part_id分组
#分组之后对各个属性要进行处理,不然程序就不知道要留哪条数据,比如下面这个,id属性会有多个,那就留最大的那个(因为分组之后每一组只有一条数据)
select count(id),max(id),part_id from userinfo5 group by part_id;
#常用的聚合函数
count
max
min
sum
avg
#####如果对于聚合函数结果进行二次筛选时?必须使用having #####
select count(id),part_id from userinfo5 group by part_id having count(id) > 1;
select count(id),part_id from userinfo5 where id > 0 group by part_id having count(id) > 1;
#连表操作:
#会重复很多很多次(笛卡尔积显示)(不推荐)
select * from userinfo5,department5
#可正常显示
select * from userinfo5,department5 where userinfo5.part_id = department5.id
#left join 第一个表在左边,第一张表会全部显示,另一张表可能会出现null
select * from userinfo5
left join department5 on userinfo5.part_id = department5.id
#right 第一个表在左边,但是第二张表会全部显示,另一张表可能会出现null
select * from userinfo5 right
join department5 on userinfo5.part_id = department5.id
#inner join将出现null的一整行隐藏
select * from userinfo5 innder
join department5 on userinfo5.part_id = department5.id
#连接多张表
select * from department5
left join userinfo5 on userinfo5.part_id = department5.id
left join userinfo6 on userinfo6.part_id = department5.id
#连接多张表,连接之后的表都可以在继续连接的时候使用
#最好查询的数据带上表名
#例如:score.sid,student.sid
select score.sid,student.sid 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.teacher_id=teacher.tid
select count(id) from userinfo5;
浙公网安备 33010602011771号