目录
一、navicat的安装和使用
navicat即数据库的可视化呈现,封装好sql,利用图形界面直接操作,进行数据库的连接、数据库的创建、数据表的创建、增删改等操作,均能可视化完成。
可以在【查询】中写code文件,并保存;
把一个数据库导出,再新建一个数据库导入,能把内容全部导入到新数据库中
二、事务
有点像在正式提交前,我们进行测试,如果结果正确,则手动提交,如果结果错误则回滚到标记事务的起点。
事务是由一组SQL语句组成的逻辑处理单元,具有ACID属性;Atomic(原子性)、Consistency(一致性)、Isolation(隔离性)、Durability(持久性)。
步骤:关闭MySQL自动提交→标记事务的起点→事务内容→提交事务or回滚→还原MySQL自动提交。
回滚示例:
set autocommit=0;
start transaction;
select * from commodity limit 10;
update commodity set c_num=10;
select * from commodity limit 10;
rollback;
select * from commodity limit 10;
set autocommit=1;
执行示例:
set autocommit=0;
start transaction;
select c_name,c_num from commodity where c_id=15;
update commodity set c_num=100 where c_id=15;
select c_name,c_num from commodity where c_id=15;
commit;
select c_name,c_num from commodity where c_id=15;
set autocommit=1;
三、索引
索引也是一张表,该表保存了主键与索引字段,并指向实体表的记录。
1、普通索引
创建索引
即最基本的索引,无任何限制:
create index indexname on tablename fieldtype;
create index id_index on test1 (id) ;
修改表结构(添加索引)
alter table tablename add index indexname (columnname);
alter table test1 add index id_index (id);
创建表的时候添加索引
create table tablename (
id int(11) not null,
username varchar(50) not null,
index [indexname] (username(length))
);
create table test1(
id int(11) primary key,
username varchar(50) not null,
index id_index (id)
);
示例:
alter table customer add index cu_name_index (cu_name);
show index from customer; #可以看到该表保存了主键和索引字段
删除索引
drop index [indexname] on tablename;
drop index id_index on test1;
查看索引信息
show index from tablename;
show index from test1;
整理
create index indexname on tablename (usecolumn)
create index id_index on test1 (id);
alter table tablename add index indexname (usecolumn);
alter table test1 add index id_index (id);
create table tablename(
id int(11) primary key,
username varchar(50) not null,
index indexname (usecolumn(length))
);
create table test1(
id int(11) primary key,
username varchart(50) not null,
index id_index (id)
);
drop index index_name on tablename;
drop index id_index on test1;
show index from tablename;
show index from test1;
2、唯一索引
索引值必须唯一,但允许有空,即null,null可以有多个;如果是组合索引,则列值的组合必须唯一。
创建索引
create unique index indexname on tablename (username(length));
create index indexname on tablename fieldtype;
对比普通索引,多了一个unique
alter table tablename add unique indexname (username(length));
alter table tablename add index indexname (columnname);
从add index变成add unique
创建表的时候添加索引
create table tablename (
id int(11) not null,
username varchar(50) not null,
unique [indexname] (username(length))
);
create table tablename (
id int(11) not null,
username varchar(50) not null,
index [indexname] (username(length))
);
从index变成unique
create index indexname on tablename (usecolumn);
create unique index indexname on tablename (usecolumn);
alter table tablename add index indexname (usecolumn);
alter table tablename add unique indexname (usecolumn);
create table tablename(
id int(11) primary key,
username varchar(50) not null,
index indexname (usecolumn(length))
);
create table tablename(
id int(11) primary key,
username varchart(50) not null,
unique indexname (usecolumn(length))
);
3、常使用的alter命令来添加和删除索引
alter table tablename add primary key (column_list);
#添加一个主键索引,意味着索引的值唯一且非空
alter table tablename add unique indexname (column_list);
#创建唯一索引,索引的值必须是唯一的(除了null,null可以出现多次)
alter table tablename add index indexname (column_list);
#创建普通索引,索引的值可以出现多次
alter table tablename add fulltext indexname (column_list);
#定义全文索引,用于全文搜索
alter table tablename drop primary key;
#用于删除指定的主键索引
drop index index_name on tablename;
show index from tablename;
索引的增删查看:增有三种方式,一是创建表时即可增加,而是修改表格的时候可以增加,还有就是直接单独新建;删有两种方式,一是alter,而是直接drop;查看就一种方式show。
索引有三种,一是普通索引,没有任何限制,二是唯一索引,除了null外的值都要唯一,null可以有多个,三是主键索引,那值需要唯一且非空,即不能有null。
四、视图
视图把一个复杂的表连接映射成一个虚拟的表,这个虚拟的表可以在show tables中查到;视图相当于把一个查询语句封装起来;
1、利用视图简化复杂的连接查询
create view viewname as
...表连接查询语句;
create view view_userorder as
select c.c_name,cu.cu_name
from commodity as c, `order` as o, customer as cu
where c.c_id=o.o_cid and o.o_cuid=cu.cu_id;
show tables;
select * from view_userorder;
select c_nama from view_userorder where cu_name='小A';
2、利用视图重新格式化查询出的数据(新增字段)
create view view_comm_madein as
select c_name , concat(c_name,'产地是:',c_madein) as madein
from commodity;
select * from view_comm_madein limit 5;
3、利用视图过滤掉不想要的数据
比如过滤空数据
create view view_outpriceisnotnull as
select c_name,c_inprice,c_outprice,c_num
from commodity
where c_outprice is not null;
select count(*) from view_outpirceisnotnull;
select count(*) from commodity;
4、利用视图创建新字段
利润:c_outprice-c_inprice
create view view_sell as
select c_name , c_outprice-c_inprice
from commodity
where c_outprice is not null;