一、单表操作
1.新建表:create table
create table 表名(字段名1 类型 ...,字段名2 类型 ...,字段名3......);
create table testdemo(id int not null,name varchar(200));
testdemo是表名,id和name是字段,int和varchar是类型,no null是不允许为空
2.表中新建数据: insert into ... values(...);
insert into 表名(字段名1,字段名2...)values(字段值1,字段值2.....);
insert into testdemo(name) values('张三');
insert into testclass(name,num)values('一年','01');
3.表中删除数据:delete from .../ delete from ...where
delete from 表名; (删除表中所有数据)
delete from 表名 where 字段名=字段值;
delete from testdemo where name=‘张三’;
4.查询表中数据:select * from ...
select * from testdemo ;(查询表中所有数据)
select * from testdemo where id=10;(查询表中id是10的数据)
select * from testdemo where id=10 or id=12;(查询表中id是10和12的数据)
5.更新表中数据:
update 表名 set 字段名1=字段值1;(修改表里所有数据)
update 表名 set 字段名1=字段值1 where 字段名2=字段值2;
update testdemo set age=25;(修改表中age字段为25)
update testdemo set age=20 where id=10 or id=12;(修改id为10和12的age字段为20)
二、join连接查询
1.左连接查询 select * from... left join....on....;
select * from a_table a left join b_table b ON a.a_id=b.b_id;
left join 是left outer join的简写,他的全称是左外连接,是外连接中的一种,左外连接,左表(a_table)的记录将会全部表示出来,而右表(b_table)只会显示符合搜索条件的记录。右表记录不足的地方均为null。
2.右连接查询 select * from... right join....on....;
select * from a_table a right join b_table b ON a.a_id=b.b_id;
right join 是right outer join的简写,他的全称是右外连接,是外连接中的一种,与左连接相反,右外连接,右表(b_table)的记录将会全部表示出来,而左表(a_table)只会显示符合搜索条件的记录。左表记录不足的地方均为null。
3.交集连接 select * from ... inner join ...on..;
交集连接是指,取两个表中对比值相同存在交集的数据(表中共有的数据)
4.混合连接查询
5.扩展
小表驱动大表即大数据在右小数据在左可以加快运行效率