本文共 1611 字,大约阅读时间需要 5 分钟。
数据库操作指南
一、增
增加记录
#指定字段插值insert into [表名] (字段1,字段2...) values (V1,V2,...)#全表插值
insert into [表名] values (V1,V2,...)#一次性插入多值
insert into [表名] (字段1,字段2...) values (V1,V2,...),(V1,V2,...),(V1,V2,...),...增加表字段
alter table [表名] add (字段) [字段类型] [约束条] [默认值] first|after 已有列名e.g: alter table table1 add certificateid varchar(18) primary key not null default ''增加索引
#添加普通索引、唯一索引和全文索引alter table [表名] add [|unique|fulltext] index (索引名称)(字段列表)#添加主键索引
alter table [表名] add primary key (字段名称)二、删
删除记录
#删除指定记录delete from [表名] where ...#删除表中的所有记录,其工作原理为先删除表-->再建一张与原来结构一致的空表
truncate [表名]删除表字段
alter table [表名] drop (字段)删除表
drop table if exists tablename删索引
alter table drop index [索引名称]三、改
#改库名
rename database 原库名 to 新库名;#改表名
rename table 原表名 to 新表名;#改表字段
alter table [表名] modify (原字段名) [字段类型] [约束条] [默认值] first|after 已有列名#改表字段(变更字段名)
alter table [表名] change (原字段名) (新字段名) [字段类型] [约束条] [默认值] first|after 已有列名注:change命令可以更改原字段名,强烈建议使用change命令#改记录
update [表名] set 字段=新值 where ... #对指定条件的记录进行更新操作四、查
#查询
select * from [表名]select * from [表名] limit n,m #从第(n+1)行开始返回m行
select (字段1,字段2...) from [表名]
#子查询
where子查询select 字段1,sum(字段2) from (select * from [表名]) group by 字段1in子查询
select 字段1,sum(字段2) from [表名] where 字段1 in (select distinct 字段 from [表名])exists子查询
select id,name from [表名1] as a where not|exists(select * from [表名2] as b where a.id=b.id)查询过程中所使用到的关键字必须按照如下顺序进行:
where,group by,having,order by,limit#连接操作
#内联
select * from a inner join b on a.字段=b.字段返回两表中所包含的公共信息#左联
select * from a left join b on a.字段=b.字段返回a表中所有信息,同时可以将关联上的b表信息加入查询结果中#右联
select * from a right join b on a.字段=b.字段效果与左联类似转载地址:http://yxbfk.baihongyu.com/