1.创建数据库
net start mysql57 启用数据库
mysql -u root -p 连接数据库
creat database 数据库名; 创建数据库
show databases; 查看所有数据库
drop database 数据库名; 删除数据库
2.创建表
use 数据库; 选中数据库
show tables; 选中库查看库中的表
create table 表名(字段名1 类型(长度),字段名2 类型(长度),); 创建表
desc 表名; 查看表
drop table 表名; 删除表
alter table 表名 rename 新表名; 重命名表
show cteate table user; 查看表创造的过程
show cteate table user\G 查看表创造的过程,以更佳的阅读体验
3.表—字段
desc 表名; 查看表的详情
alter table 表名 add column 字段名 类型(长度); 增加字段
alter table 表名 drop column 字段名; 删除字段
alter table 表名 cahnge 字段名 新字段名 类型(长度); 修改字段名
alter table 表名 modify 字段名 类型(长度); 修改字段类型
alter table 表名 add 字段名 类型(长度) after 指定字段;
增加字段的时候控制字段顺序:
alter table 表名 modify 字段名 类型(长度) first;
将指定字段排到第一位
4.数据字段的类型
数值类型:(整型浮点型)
整型:
tinyint 1
smallint 2
int 4
bigint 8
浮点型:
float(m,d) 4
double(m,d) 8
注意:unsigned(防止负值)
字符串类型:
varchar 0-255 变长
char 0-255 定长
text 0-65535
tinytext 0-255
blob 0-65535
tinyblob 0-255
longtext 极长
日期:
date 3
time 3
datetime 8
timestamp 4
year 1
复合类型:
空间类型:
5.引擎
myisam:读写入较多
innodb:删改多,安全性要求高
建表的时候后面可以直接加引擎和编码方式
create table biao1(ziudan1 int(11),zidaun2 char(12))engine=innodb default charset=utf8;
6.索引
普通索引 index
唯一索引 unique
主键索引 primary key
全文索引 fulltext
alter table
添加索引:alter table 表名 add 索引类型(字段名);
删除索引:alter table 表名 drop 索引类型;
create(不能创建主键索引)
创建索引:create [索引类型] index 索引名 on 表名(字段名);
删除索引(需要有索引名):drop index 索引名 on 表名;
查看索引:
show index from 表名; 查看索引
7.增删改查(数据)
增——插入数据
insert into 表名 values (所有字段的值);
insert into 表名(指定字段) values (字段的值);
查——查询数据
select * from 表名; 查询表中所有数据
select 字段 from 表名; 查询指定字段的数据
select distinct 字段名 from 表名; 指定字段的不重复数据
select * from 表名 where 条件; 满足条件的数据
[where条件:> 、<、 =、 or、 and、 >=、 <=、 !=]
改——更新数据
update 表名 set 字段=值 where 条件;
update 表名,表名 set 字段=值,字段=值 where 条件;
删——删除数据
delete from 表名 where 条件;
清空整个表:
delete from 表名;
truncate table 表名;
排序——order by
select * from 表名 order by 字段名 desc/asc; 该表数据按指定字段名升降序
[也可order by 多个字段,哪个字段在前面,先按该字段,依次向后]
限制数量limit
select * from 表名 order by 字段名 desc/asc limit 2; 排序之后取满足条件的前2条
分页原理:
select * from 表名 limit 偏移开始位置,偏移量;
分组——group by
select * from 表名 group by 字段名; 该字段相同的值分为一组
select count(该字段),该字段 from 表名 group by 该字段名; 该字段相同的值和数量
select count(该字段),该字段 from 表名 group by 该字段名 with rollup;
在上面的基础上汇总
结果再过滤——having
select count(该字段)as result,该字段 from 表名 group by 该字段名 having result>1;
对结果再进行过滤,要结果大于1的值;
8.mysql常用函数
mysql输出用select
sum()
count()
max()
min()
avg()
用法:select count(*) as 别名 from 表名;
select max(字段名) from 表名;
字符串函数:
concat();
lcase();
ucase();
length();
ltrim();
rtrim();
repeat();
replace();
substr();
space();
数值函数:
ceiling();
floor();
rand();
bin();
时间函数:
curtime();
curdate();
now();
week(now());
year(now());
unix_timestamp(now()); 数据库时间一般都是以时间戳存储的
from_unixtime();
9.多表联合查询
内联法:
select 查询字段 from 表1,表2 where 条件;
select 查询字段 from 表1 inner join 表n on 条件;
外联法:
select 查询字段 from 表1 left join 表n on 条件;
左连接,以左边表1为主;
select 查询字段 from 表1 right join 表n on 条件;
右连接,以右边表n为主;
子查询:
select * from 表1 where 表1查询字段名 in(select 需查询字段 from 表n);
记录联合查询
union
select 共有字段 from 表1 union select 共有字段 from 表n;
重复的自动忽略
union all
select 共有字段 from 表1 union select 共有字段 from 表n;
重复的自动累加
10.数据库权限管理
grant 权限 on 库名.表名 to '用户名'@'localhost' identified by '密码';
权限有:insert select update delete all
revoke 权限 on 库名 from 账户@ip地址
11.mysql高级应用
复制表like,先复制表结构,再复制表数据
like
复制表结构:create 新表名 like 原表名;
复制表数据:insert into 新表 select * from 原表;
视图view,相当于临时表
创建视图:create view 视图名 as 内容;
删除视图:drop view 视图名;
查看过程:show create view 视图名;
预处理prepare
创造语句:prepaer 预处理名 from "条件";
设置参数:set @i= ;
执行语句:execute 预处理名 using 参数;(指定参数的值)
存储procedure
\d // #需写函数,先更改结束符号
create procedure 存储名()
begin
set @i=1;
while @i<=10000 do
insert into t1(id,name) values(@i,concat("user",@i));
set @i=@i+1;
end while;
end //
\d ; #恢复;
call 存储名; #需要调用之后才生效
show procedure status; 了解
show create procedure 名\G 查看创建 过程
触发器trigger
t1 t2两张表 一模一样 如果t1中数据发生变动 t2表也跟着变动
\d //
create trigger d_1 before delete on t1 for each row 在插入到t1之前 循环讲数据插入到t2中
begin
delete from t2 where name=old.name;
end //
\d ;