use balfish;
create table mytable1(
id int not null,
name varchar(20),
grade int
);
insert into mytable1 values(‘1‘,‘yang‘,97);
insert into mytable1 values(‘2‘,‘li‘,94);
insert into mytable1 values(‘3‘,‘chen‘,88);
insert into mytable1 values(‘4‘,‘zhang‘,57);
insert into mytable1 values(‘5‘,‘dai‘,90); create table mytable2(
id int not null,
name varchar(20),
job varchar(20)
);
insert into mytable2 values(‘1‘,‘yang‘,‘worker‘);
insert into mytable2 values(‘2‘,‘li‘,‘farmer‘);
insert into mytable2 values(‘3‘,‘shi‘,‘doctor‘); select: select * from mytable1 order by grade desc limit 0,2; inner join (1) select * from tb1,tb2 where tb1.name=tb2.name; (2) select * from tb1 inner join tb2 on tb1.name=tb2.name; outer select * from mytable1 tb1 left join mytable2 tb2 on tb1.name=tb2.name; insert 一次插入多个. insert into city(id,city_name) values(1,’beijing’),(2,’shanghai’); 当insert时表中存在唯一性约束时, 方法1:insert into tbl_name(a, b, c) values(?,?,?) on duplicate key update c=values(c); 方法2:Insert ignore into tbl_name (a,b,c) values(1,2,3); update update product set amount=150 where id=1; update product_details set weight=38,exist=1 where name=‘Jim‘; update tbl_name set b=b+1 where name=‘aaa’; delete DELETE FROM orderlog where user = ‘Sean‘ and id between 20000 and 40000; truncate – 特点
属于DDL操作,执行后立即生效无法回滚
等同于delete from tb,删除全表数据,保留空表
需要drop权限
语法:truncate table product; create table
通过表来建表 (1)create table t1 select * from product; 创建一个和原表字段结构一致的新表,去掉所有的约束,同时将原表select的结果数据插入新表 (2)create table t2 like product; 创建一个和原表结构完全一致的新空表,包含全部约束 alter table 字段操作: alter table add/modify/drop column ... alter table t2 add column contact varchar(50);
alter table t2 modify column contact varchar(500);
alter table t2 drop column contact; 索引操作: alter table t2 add index idx_orderno(orderno);
alter table t2 drop index idx_orderno;
alter table t2 add primary key(id);
alter table t2 add unique index uniq_version(version); drop table – 特点
• 删除表操作,清除全部数据,删除表定义文件
• 不可回滚
• 语法:drop table t2; 存储引擎: – MyISAM
不支持事务
表级锁
只能缓存索引
表文件在大批量更新操作后可能损坏
– InnoDB (目前线上使用的引擎)
支持事务
行级锁,提高了并发性
buffer pool中缓存数据和索引
不会损坏 Index 索引是存储引擎用于快速定位数据的一种数据结构 • 索引扫描
主键----直接在Clustered B+Tree上查询
辅助索引----在Secondary B+Tree上查询到主键 ,然后 用主键在Clustered B+Tree
mysql具体语句示例
标签: