山西省建设厅网站,哈尔滨cms建站系统,应用商店和应用市场,深圳网络运营推广公司1. 数据表#xff1a;
本文中所有命令#xff0c;测试的数据表结构如下图#xff1a;
2. 查询语句#xff1a;
2.1 基础查询#xff1a;select
//查询单个字段#xff1a;
select 字段名 from 表名;
//查询多个字段
select 字段名1,字段名2,... from 表名;
//查询所…1. 数据表
本文中所有命令测试的数据表结构如下图
2. 查询语句
2.1 基础查询select
//查询单个字段
select 字段名 from 表名;
//查询多个字段
select 字段名1,字段名2,... from 表名;
//查询所有字段
select * from 表名;
//为查询的字段起别名,使用 As
select 字段名1 as 别名1,字段名2 as 别名2... from 表名;2.2 去重查询distinct
//去除重复项查询
select distinct 字段名1,字段名2... from 表名;
eg: select distinct friendsAge from friends;2.3 条件查询where
//以某种条件进行筛选
select 字段名1,字段名2,... from 表名 where 条件;
//多种条件查询
select * from 表名 where 条件1 and 条件2;
select * from 表名 where 条件1 or 条件2;
//范围内条件查询
select * from 表名 where 字段名 between 条件1 and 条件2
eg: select * from 表名 where id between 20 and 40;2.4 排序查询order by [ asc | desc]
// asc 升序, desc 降序
select 字段名 from 表名 [where 筛选条件] order by 排序条件 [ asc / desc ]
eg: select * from friends order by friendsAge;2.5 限制查询limit
//使用 limit 子句
select 字段名 from 表名 [where 筛选条件] [order by 排序条件] limit 数字
eg: select * from friends limit 2; //仅查询前两条数据//查询首条数据
select * from friends limit 1;//查询最后一条数据(先倒序排序然后只查询第一条即为最后一条数据)
select * from friends order by friendsID desc limit 1;//查询某一范围的数据比如第3条--第7条
select * from friends limit 2,5;//(如果是第n条--第m条则为 limit n-1,m-n1)2.6 子查询嵌套查询
//即 select 中 嵌套使用 select
eg: select * from friends where friendsID (select friendsID from friends order by friendsID desc limit 1);3. 插入语句insert into
//方式一
insert into 表名 values(值1,值2,值3,...)//值的顺序和字段值最好保持一致
//方式二
insert into 表名(字段值1,字段值2,字段值3,...) values(值1,值2,值3,...) //字段值顺序可打乱,但要和value值相对应,如果有字段值被省略,可以自动推断的会自动推断,否则设置为null
eg: insert into friends values(12,xiaolan,45)
eg: insert into friends(friendsID,friendsName) values(10,xiaolan)
eg: insert into friends(friendsAge,friendsName) values(18,xiaosheng)4. 修改语句update
update 表名 set 字段名1 值1,字段名2 值2,... where [筛选条件]eg: update friends set friendsAge 80 where friendsID 12;
eg: update friends set friendsAge 80,friendsName xiaoming where friendsID 12;5. 删除语句delete
delete from 表名 where[筛选条件]eg: delete from friends where friendsID 12;6. 表table
6.1 创建新表create table
create table 新表名(字段名1 类型,字段名2 类型,...)eg: create table animal(id INTEGER,name STRING,price DOUBLE);6.2 sqlite修改表修改字段名,表名,字段属性…
6.2.1 修改字段名
// sqlite 无法直接对表的内部属性进行修改但是可以通过新建表--复制表--删除旧表的方式间接更改
//第一步:
alter table 原表名 rename to 临时表名
//第二步
create table 原表名(新字段名 新属性,新字段名 新属性,...)
//第三步
insert into 原表名 select * from 临时表名
//第四步
drop table 临时表名
//上述的整个操作类似于交换两个变量,只是先将要更改属性的原表重命名为一个临时表,然后
//创建一个和原表名一样的表将临时表中的数据全部导入新表中,再删除临时表,这样新建的表
//就相当于代替了原来的表,达到改变属性的目的6.2.1 删除字段
将表中 字段3 删除
//第一步:
alter table 原表名 rename to 临时表名
//第二步
create table 原表名(字段名1 属性,字段名2 属性,字段名3 属性,...)
//第三步
insert into 原表名(字段名1,字段名2) select (字段名1,字段名2) from 临时表名
//第四步
drop table 临时表名6.2.2 sqlite增加字段
ALTER TABLE 表名 ADD COLUMN 新列名 类型;6.3 mysql修改表
6.3.1 修改列名(字段名)
ALTER TABLE 表名 CHANGE COLUMN 旧字段名,新字段名 类型;6.3.2 修改列(字段)类型或约束
ALTER TABLE 表名 MODIFY COLUMN 列名 新类型;6.3.3 添加新列
ALTER TABLE 表名 ADD COLUMN 新列名 类型;6.3.4 删除列
ALTER TABLE 表名 DROP COLUMN 列名;6.3.5 修改表名
ALTER TABLE 表名 RENAME TO 新表名;6.3.6 删除表
DROP TABLE 表名;持续更新中请大家多多关注…