MYSQL学习
数据库操作
创建数据库 create database db_name;
删除数据库 drop database db_name;
选择/使用数据库 use db_name;
使用mysqladmin在终端执行
创建数据库 mysqladmin -u root -p create db_name Enter password:***
删除数据库 mysqladmin -u root -p drop db_name Enter password:***
数据类型
常用数据类型:
- int:整数
- float(M,D):浮点数,4字节,M长度D小数位数
- double(M,D):浮点数,8字节,M长度D小数位数
- varchar:字符串,0-65535字节 可变长度字符串 String
- datetime:日期型,8字节,混合日期和时间
数据表操作
创建数据表 create table tb_name (column_name column_type);
eg:
create table if not exists 'tb_name'(
id int auto_increment,
title varchar(100) not null,
date date
primary key (id)
);
#创建表tb_name,存在id、title、date。id是自增的(auto_increment),且为主键(primary key)是int型,title是字符串型限制100大小,date是日期型
删除数据表 drop table tb_name;
修改数据表(表名、表字段)
#存在int i 和char(1) c
alter table tb_name drop i; #删除表字段
alter table tb_name add i int;#增加表字段
alter table tb_name modify c char(20);#修改表字段c的数据类型
alter table tb_name change i j char(5);#修改表字段i的名称为j以及数据类型
alter table tb_name rename to new_name;#修改表名
插入数据 insert into tb_name(column1,column2)values (v1,v2);
查询数据 select column1,column2 from tb_name where … limit 5
更新数据 update tb_name set column1=v1,column2=v2 where …
删除数据 delete from tb_name where …
其他操作
like模糊匹配
模糊匹配,%多个字符,_一个字符
eg:
name:abcdef、bcdef、aaabcdef
select * from tb_name where name like '%bcdef' abcdef、aaabcdef
select * from tb_name where name like '_bcdef' abcdef
union并集
union:连接两个以上的select语句的结果到一个结果集合中,删除重复数据,即取并集并去重
union all:连接两个以上的select语句的结果到一个结果集合中,即取并集并不去重
eg:
#并集去重
select country from tb1
union
select country from tb2
order by country;
#并集不去重
select country from tb1
union all
select country from tb2
order by country;
order by排序
ASC 升序,DESC降序
eg:
select * from tb_name order by id ASC; #id:1-10
select * from tb_name order by id ASC; #id:10-1
group by分组
根据一个或多个列对结果集进行分组,可使用 COUNT,SUM,AVG等函数
eg:
select * from tb_name where age=10 group by sex;
连接(内连接、外连接)
inner join:内连接,获取两表中字段匹配关系的记录;即两表交集
left join:左连接,获取左表所有记录,即使右表无对应记录;
right join:右连接,获取右表所有记录,即使左表无对应记录
NULL 值
select * from tb_name where id is null;
select * from tb_name where id is not null;
#求相关null的数据的时候,不能使用id=null,id!=null。要是用is 和 is not
事务
场景:处理操作量大,复杂度高的数据,需要确保要么全部执行,要么全部未执行,用于管理insert、update、delete。
特性:原子性、一致性、隔离性、持久性
begin开始事务,操作完成之后,commit提交事务,即所有操作全部执行
begin开始事务,操作完成后,rollback回滚,即所有操作全部取消执行
索引
目的:提高MySQL的检索速度
单列索引:一个索引只包含单个列
组合索引:一个索引包含多个列
创建索引:create index index_name on tb_name(column_name);
添加索引:alter table tb_name add index index_name(column_name);
删除索引:drop index index_name on mytable; alter table tb_name drop index index_name;
显示索引信息:show index from tb_name\G \G:格式化输出信息
临时表
只在当前连接中可见,关闭连接自动删除表并释放空间
#创建临时表
create temporary table tb_name(
a int,
b char(5)
);
#写入数据
insert into tb_name (a,b) values (100,'string');
#查看数据
select * from tb_name;
#使用show tables 显示所有表时,不存在临时表tb_name
#退出当前连接,再使用select查找临时表tb_name时,无法查看
#删除临时表,断开连接自动删除,or:
drop table tb_name;
复制表
由表a复制出表b,保留表结构、索引以及数据
#查看a的建表语句
show creat table a \G;
#修改a的表名为b后创建表b
create table b(
……
);
#拷贝a的数据到b中
insert into b (b1,b2,b3) select a1,a2,a3 from a;
设置不能重复数据
#使用主键确保不重复
create table tb_name(
a char(20),
b char(20),
c int,
primary key(a,b)
);#双主键
#使用unique确保不重复
create table tb_name(
a char(20),
b char(20),
c int,
unique(a,b)
);