查询基本使用
1.查询所有列
select * from 表名
例:
select * from classes;
2.查询指定列
select 列1,列2,...from 表名;
例:
select id,name from classes;
增加
说明:主键列是自动增长,但是在全列插入时需要占位,通常使用空值(0或者null) ; 字段默认值 default 来占位,插入成功后以实际数据为准
1.全列插入:值的顺序与表结构字段的顺序完全一一对应
此时 字段名列表不用填写
insert into 表名 values (...)
例:
insert into students values(0,’郭靖‘,1,'蒙古','2016-1-2');
2.部分列插入:值的顺序与给出的列顺序对应
此时需要根据实际的数据的特点 填写对应字段列表
insert into 表名 (列1,...) values(值1,...)
例:
insert into students(name,hometown,birthday) values('黄蓉','桃花岛','2016-3-2');
上面的语句一次可以向表中插入一行数据,还可以一次性插入多行数据,这样可以减少与数据库的通信
3.全列多行插入
insert into 表名 values(...),(...)...;
例:
insert into classes values(0,'python1'),(0,'python2');
4.部分列多行插入
insert into 表名(列1,...) values(值1,...),(值1,...)...;
例:
insert into students(name) values('杨康'),('杨过'),('小龙女');
修改
update 表名 set 列1=值1,列2=值2... where 条件
例:
update students set gender=0,hometown='北京' where id=5;
删除
delete from 表名 where 条件
例:
delete from students where id=5;
逻辑删除,本质就是修改操作
update students set isdelete=1 where id=1;
as关键字
1.使用 as 给字段起别名
select id as 序号, name as 名字, gender as 性别 from students;
2.可以通过 as 给表起别名
select s.id,s.name,s.gender from students as s;