1. INSERT用于向数据库的表中插入1条记录
insert into 表名 (字段1,字段2,...) values (数据1,数据2,数据3...)
示例
-- 如果表存在,就删除 drop table if exists classes; show tables; create table classes (id BIGINT not null auto_increment, name varchar(10) not null,PRIMARY KEY(id)) default charset=utf8; select * from classes; -- 插入全部记录时,省略字段 insert into classes VALUES(1,"一班"); -- 掺入指定的字段 insert into classes (name) VALUES ("二班"); -- 插入多条数据 insert into classes (name,id) values ("三班",3),("四班",4); select * from classes;
Insert语句总结:
- 可以向指定表插入一条记录
- 语法:insert into 表名 (字段1,字段2,...) values (数据1,数据2,数据3...)
select用于查询表的记录
语法:select 列1,列2,列3,... from 表名 where ... ;
show tables; create table students ( id BIGINT not null auto_increment, class_id INT , name VARCHAR(10) not null, gender char(1), PRIMARY KEY(id)) default charset=utf8; INSERT into students (class_id,name,gender)VALUES (1,"小明","M"), (1,"小红","F"), (1,"小军","M"), (2,"小白","F"), (2,"小兵","M"), (3,"小王","M"), (3,"小丽","F"); select * from students; -- where条件 select * from student where id = 2; select * from student where id > 3; select * from student where class_id > 3; select * from student where gender = 'M'; -- and select * from student where id > 3; -- 筛选特定的列 select id,name from students; -- 聚合查询 -- 获取记录数量 select count(*) from students; -- select count(*) from students group by class_id; select class_id,count(*) number from students group by class_id; -- 多表查询 -- 查询的结果是一个4*7=28条的记录,即2个表的乘积,没什么用 select * from classes,students; -- 通常使用inner join来进行联合查询 select * from classes inner join students on classes.id = students.class_id; -- 指定别名来更好的返回结果 select c.id,c.name class_name,s.name student_name,s.gender from classes c inner join students s on c.id = s.class_id;
select查询总结:
- 可以指定查询的列
- 可以通过where条件筛选符合条件的记录
- 可以使用聚合查询
- 可以多表联合查询
- 查询结果仍然是一个关系表
```