2023年8月3日发(作者:)
MySQL数据库增删改查的SQL语句添加的语句:L("insert into toutiao (type,title) values (?,?)",new String[]{type,title});删除的语句:L("delete from shoucang where title = ?",new String[]{title});修改的语句:L("update toutiao set type = ? where title = ?",new String[]{type,title});查询的语句:Cursor query = ("toutiao", null, null, null,null,null,null);按条件查询全部:Cursor query = ("toutiao", null, "type=?", new String[]{tiaojian},null,null,null);
模糊查询并且降序:sql = "select * from house where price_address like '%"+pri+"%' order by price asc";Cursor house = ry(sql, null);
模糊查询并且升序:sql = "select * from house where price_address like '%"+pri+"%' order by price desc";Cursor house = ry(sql, null);-- 登录数据库 mysql -uroot -proot mysql -uroot -p (如果遇到 Can't connect to MySQL server on 'localhost' (10061) 那么可以右击计算机(我的电脑)--->管理--->服务和应⽤程序--->服务, 找到MySQL,启动该服务,关闭cmd命令窗⼝后,重新打开进⾏登录) show databases; -- 新建数据库 create database 1503d; -- 删除数据库 drop database 1503d; -- 选择数据库 use 1503d; -- 查看所有表格 show tables; -- 创建表 --
create table t_student2( id int(11) primary key auto_increment, stuno varchar(20) not null unique, name varchar(10) not null, age int(2) default 18, sex char(1), birthday date default '1998-01-01', marriage boolean ); -- 查看表结构 describe t_student; desc t_student; -- 向表中插⼊数据 -- insert into 表名(列名A,列名B...) value(A列的值,B列的值...),(A列的值,B列的值...),(A列的值,B列的值...) insert into t_student(id,stuno,name,age,sex,birthday) value(null,'9','柴俊杰',23,'男','1993-05-31'); insert into t_student values(null,'4','王威',19,'男','1997-06-14',false); insert into t_student values(null,'5','王威2',19,'男','1997-06-14',false),(null,'6','王威3',19,'男','1997-06-14',false),(null,'7','王威4',19,'男','1997-06-14',false); -- 查询表数据 -- select 列名A,列名B... from 表名 select name,sex from t_student; -- select * from 表名
-- 删除表数据
-- delete from 表名 where 条件 -- 清空表数据 truncate table 表名; -- 修改表数据 -- update 表名 set 列名A=A列的新值, 列名B=B列的新值... where 条件 update t_student set age = age+1 where id = 1; -- 设置gbk字符集 set names gbk; -- 修改表名 alter table 表名 rename 新表名; rename table 表名 to 新表名; -- 增加表字段 alter table 表名 add 新列名 数据类型 ; alter table 表名 add 新列名 数据类型 first; alter table 表名 add 新列名 数据类型 after 列名; -- 删除表字段 alter table 表名 drop 列名; -- 修改表字段的名称和数据类型 alter table 表名 change 列名 新列名 数据类型; -- 修改表字段的数据类型 alter table 表名 modify 列名 数据类型; -- 【单表查询】 -- 排序 order by asc升序(降序desc) 默认asc -- 单列排序 select 列名A,列名B... from 表名 order by 列名 asc; -- 多列排序 select 列名A,列名B... from 表名 order by 列名1 asc, 列名2 desc; -- 分页查询 -- limit M,N M表⽰起始索引(索引是从0开始的), N表⽰要查询的数据条数 -- 每页2条,查询第2页的数据 limit 2,2 -- 每页10条,查询第5页的数据 limit 40,10 -- 每页有pageSize条,查询第n页的数据 limit (n-1)*pageSize,pageSize -- where条件 -- 【关系运算符】 -- 查询年龄介于16和18之间(包含16和18)的所有学⽣的信息: select * from t_student where age between 16 and 18; -- 【逻辑运算符 and or not (&& || !)】 -- 查询年龄介于16和18之间(包含16和18) 或者是id为偶数的学⽣: select * from t_student where (age >= 16 and age <= 18) or id%2 = 0; select * from t_student where (age >= 16 && age <= 18) || id%2 = 0; --查询年龄不等于18的学⽣的信息: select * from t_student where age != 18; select * from t_student where age <> 18; -- 查询性别不为空的学⽣的信息: select * from t_student where xingbie is not null; -- 查询年龄不⼤于18的学⽣的信息: select * from t_student where not(age > 18); select * from t_student where !(age > 18); select * from t_student where age <= 18; -- 【⽇期函数】 -- 查询当前⽇期 select curdate() from dual; select current_date from dual; -- 查询当前⽇期和时间 select now() from dual; select sysdate() from dual; -- 查询当前时间(不带有年⽉⽇): select curtime() from dual; -- 【聚合函数 avg平均值 max最⼤值 min最⼩值 sum求和 count计数】 -- 查询学⽣表中的最⼤年龄 -- 起别名时可以使⽤as或省略 select max(age) 最⼤年龄 from t_student; select max(age) as 最⼤年龄 from t_student; select max(age) 最⼤年龄,min(age) 最⼩年龄 , avg(age) 平均年龄 , sum(age) 年龄总和, count(age) ⾏数 from t_student; -- like 模糊查询 -- 通配符:①_:匹配⼀个字符②%:匹配任意0到n个字符 -- 查询姓名的倒数第⼆个字符为"威"的学⽣信息: select * from t_student where name like '%威_'; -- in的使⽤ -- 查询年龄不在16,18,20之中的所有学⽣的信息: select * from t_student where age not in (16,18,20); -- 去除重复⾏ select distinct xingbie,birthday from t_student; -- 分组查询 group by 字段名 -- 查询平均年龄⼤于17的城市: select address,avg(age) from t_student group by address having avg(age)>17;
-- 查询语句中各关键字的使⽤顺序: select [distinct] 列名1 as 别名1,列名2 as 别名2...
from
表名 where 条件
group by
having
order by
limit
create table school( id int primary key auto_increment, name varchar(20) unique, address varchar(20), tel varchar(20) ); create table student( id int primary key auto_increment, name varchar(20) , age int , school_id int ); -- 在表已经存在的情况下添加外键约束: alter table student
add constraint foreign key (school_id) references school(id)
on delete cascade; -- 查询 19岁的张三所在学院的地点【内联接】①: select s from student stu inner join school sch
on _id =
where = 19 and = '张三'; select s from student inner join school
on _id =
where = 19 and = '张三'; -- 查询 19岁的张三所在学院的地点【内联接】②: select s from student,school
where _id =
and = 19 and = '张三'; -- 查询各个学院的学⽣信息【内联接】 select school.*,student.* from school ,student
where = _id; -- 查询各个学院的学⽣信息【左联接】 select school.*,student.* from school left join student
on = _id; -- 查询各个学院的学⽣信息【右联接】 select school.*,student.* from student right join school
on = _id;
insert into school(id,name,address,tel)
value(1,'移动学院','2号楼','111111') ,(2,'游戏学院','6号楼','222222') ,(3,'软⼯学院','8号楼','333333'); insert into student(name,age,school_id)
value('张三',19,1) ,('李四',20,2) ,('王五',18,3) ,('赵六',17,2) ,('孙七',21,1); -- where型⼦查询 -- 将移动学院的所有学⽣的年龄增加1 update student set age = age+1 where school_id =
(select id from school where name = '移动学院'); -- 查询移动学院的学⽣⼈数 select count(*) from student where school_id =
(select id from school where name = '移动学院');
2023年8月3日发(作者:)
MySQL数据库增删改查的SQL语句添加的语句:L("insert into toutiao (type,title) values (?,?)",new String[]{type,title});删除的语句:L("delete from shoucang where title = ?",new String[]{title});修改的语句:L("update toutiao set type = ? where title = ?",new String[]{type,title});查询的语句:Cursor query = ("toutiao", null, null, null,null,null,null);按条件查询全部:Cursor query = ("toutiao", null, "type=?", new String[]{tiaojian},null,null,null);
模糊查询并且降序:sql = "select * from house where price_address like '%"+pri+"%' order by price asc";Cursor house = ry(sql, null);
模糊查询并且升序:sql = "select * from house where price_address like '%"+pri+"%' order by price desc";Cursor house = ry(sql, null);-- 登录数据库 mysql -uroot -proot mysql -uroot -p (如果遇到 Can't connect to MySQL server on 'localhost' (10061) 那么可以右击计算机(我的电脑)--->管理--->服务和应⽤程序--->服务, 找到MySQL,启动该服务,关闭cmd命令窗⼝后,重新打开进⾏登录) show databases; -- 新建数据库 create database 1503d; -- 删除数据库 drop database 1503d; -- 选择数据库 use 1503d; -- 查看所有表格 show tables; -- 创建表 --
create table t_student2( id int(11) primary key auto_increment, stuno varchar(20) not null unique, name varchar(10) not null, age int(2) default 18, sex char(1), birthday date default '1998-01-01', marriage boolean ); -- 查看表结构 describe t_student; desc t_student; -- 向表中插⼊数据 -- insert into 表名(列名A,列名B...) value(A列的值,B列的值...),(A列的值,B列的值...),(A列的值,B列的值...) insert into t_student(id,stuno,name,age,sex,birthday) value(null,'9','柴俊杰',23,'男','1993-05-31'); insert into t_student values(null,'4','王威',19,'男','1997-06-14',false); insert into t_student values(null,'5','王威2',19,'男','1997-06-14',false),(null,'6','王威3',19,'男','1997-06-14',false),(null,'7','王威4',19,'男','1997-06-14',false); -- 查询表数据 -- select 列名A,列名B... from 表名 select name,sex from t_student; -- select * from 表名
-- 删除表数据
-- delete from 表名 where 条件 -- 清空表数据 truncate table 表名; -- 修改表数据 -- update 表名 set 列名A=A列的新值, 列名B=B列的新值... where 条件 update t_student set age = age+1 where id = 1; -- 设置gbk字符集 set names gbk; -- 修改表名 alter table 表名 rename 新表名; rename table 表名 to 新表名; -- 增加表字段 alter table 表名 add 新列名 数据类型 ; alter table 表名 add 新列名 数据类型 first; alter table 表名 add 新列名 数据类型 after 列名; -- 删除表字段 alter table 表名 drop 列名; -- 修改表字段的名称和数据类型 alter table 表名 change 列名 新列名 数据类型; -- 修改表字段的数据类型 alter table 表名 modify 列名 数据类型; -- 【单表查询】 -- 排序 order by asc升序(降序desc) 默认asc -- 单列排序 select 列名A,列名B... from 表名 order by 列名 asc; -- 多列排序 select 列名A,列名B... from 表名 order by 列名1 asc, 列名2 desc; -- 分页查询 -- limit M,N M表⽰起始索引(索引是从0开始的), N表⽰要查询的数据条数 -- 每页2条,查询第2页的数据 limit 2,2 -- 每页10条,查询第5页的数据 limit 40,10 -- 每页有pageSize条,查询第n页的数据 limit (n-1)*pageSize,pageSize -- where条件 -- 【关系运算符】 -- 查询年龄介于16和18之间(包含16和18)的所有学⽣的信息: select * from t_student where age between 16 and 18; -- 【逻辑运算符 and or not (&& || !)】 -- 查询年龄介于16和18之间(包含16和18) 或者是id为偶数的学⽣: select * from t_student where (age >= 16 and age <= 18) or id%2 = 0; select * from t_student where (age >= 16 && age <= 18) || id%2 = 0; --查询年龄不等于18的学⽣的信息: select * from t_student where age != 18; select * from t_student where age <> 18; -- 查询性别不为空的学⽣的信息: select * from t_student where xingbie is not null; -- 查询年龄不⼤于18的学⽣的信息: select * from t_student where not(age > 18); select * from t_student where !(age > 18); select * from t_student where age <= 18; -- 【⽇期函数】 -- 查询当前⽇期 select curdate() from dual; select current_date from dual; -- 查询当前⽇期和时间 select now() from dual; select sysdate() from dual; -- 查询当前时间(不带有年⽉⽇): select curtime() from dual; -- 【聚合函数 avg平均值 max最⼤值 min最⼩值 sum求和 count计数】 -- 查询学⽣表中的最⼤年龄 -- 起别名时可以使⽤as或省略 select max(age) 最⼤年龄 from t_student; select max(age) as 最⼤年龄 from t_student; select max(age) 最⼤年龄,min(age) 最⼩年龄 , avg(age) 平均年龄 , sum(age) 年龄总和, count(age) ⾏数 from t_student; -- like 模糊查询 -- 通配符:①_:匹配⼀个字符②%:匹配任意0到n个字符 -- 查询姓名的倒数第⼆个字符为"威"的学⽣信息: select * from t_student where name like '%威_'; -- in的使⽤ -- 查询年龄不在16,18,20之中的所有学⽣的信息: select * from t_student where age not in (16,18,20); -- 去除重复⾏ select distinct xingbie,birthday from t_student; -- 分组查询 group by 字段名 -- 查询平均年龄⼤于17的城市: select address,avg(age) from t_student group by address having avg(age)>17;
-- 查询语句中各关键字的使⽤顺序: select [distinct] 列名1 as 别名1,列名2 as 别名2...
from
表名 where 条件
group by
having
order by
limit
create table school( id int primary key auto_increment, name varchar(20) unique, address varchar(20), tel varchar(20) ); create table student( id int primary key auto_increment, name varchar(20) , age int , school_id int ); -- 在表已经存在的情况下添加外键约束: alter table student
add constraint foreign key (school_id) references school(id)
on delete cascade; -- 查询 19岁的张三所在学院的地点【内联接】①: select s from student stu inner join school sch
on _id =
where = 19 and = '张三'; select s from student inner join school
on _id =
where = 19 and = '张三'; -- 查询 19岁的张三所在学院的地点【内联接】②: select s from student,school
where _id =
and = 19 and = '张三'; -- 查询各个学院的学⽣信息【内联接】 select school.*,student.* from school ,student
where = _id; -- 查询各个学院的学⽣信息【左联接】 select school.*,student.* from school left join student
on = _id; -- 查询各个学院的学⽣信息【右联接】 select school.*,student.* from student right join school
on = _id;
insert into school(id,name,address,tel)
value(1,'移动学院','2号楼','111111') ,(2,'游戏学院','6号楼','222222') ,(3,'软⼯学院','8号楼','333333'); insert into student(name,age,school_id)
value('张三',19,1) ,('李四',20,2) ,('王五',18,3) ,('赵六',17,2) ,('孙七',21,1); -- where型⼦查询 -- 将移动学院的所有学⽣的年龄增加1 update student set age = age+1 where school_id =
(select id from school where name = '移动学院'); -- 查询移动学院的学⽣⼈数 select count(*) from student where school_id =
(select id from school where name = '移动学院');
发布评论