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;
语法: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;
update返回的结果是一个整数,代表更改了几条记录
-- 全部记录的class_id都会改为5
update students set class_id = 5 ;
-- 修改指定记录
update students set class_id = 5 where id = 3;
-- 修改指定记录的多个字段
update students set class_id = 5, gender = 'F' where id = 3;
delete返回一个整数,代表删除记录的数目
-- 删除表的全部记录
delete from students;
-- 删除某条记录
delete from students where id=3
-- 删除多条记录
delete from students where id > 3 and gender = 'M';
廖雪峰Java15JDBC编程-2SQL入门-2insert/select/update/delete
原文:https://www.cnblogs.com/csj2018/p/11436654.html