1.创建一张表
create table 表名(字段名字 字段类型, 字段名字 字段类型);
create table info(id int,name varchar(20));
2.插入语句(增)
<1> 插入全列
insert into 表名 values(要添加的值…);
如果添加的字段的数据类型是int类型,直接写值。
如果添加的字段类型是varchar类型和日期类型,使用单引号把值包起来。
insert into info values(1,'zhangsan','110');
<2> 插入制定列名
insert into test (name) values('lisi');
<3> 删除语句(删)
delete from 表名 where 条件 如果没有where条件 删除表中的所有数据。
delete from info where id > 1;删除表中id大于1的数据。
delete from info; 删除表中的所有数据。
<4> 修改语句(改)
update 表名 set 字段名1 = 值1 , 字段名2 = 值2 where 条件;
update info set phone='12345' where name='zhangsan';
<5> 查询语句(查)
select 字段名 (*) as别名 from 表名 where 条件
select * from info; 查询所有字段的内容
select name,phone from info where id = 1;查询id为1的姓名和手机号
<6> 去除表中重复记录(只会影响查询的结果 不会修改表中的记录)
select distinct * from 表名;
select distinct name from info; 只显示名字 去除重复的记录
<7> 部分查询 : desc 降序 limit 查询的个数, offset 坐标
select number,mode from info order by _id desc limit ? offset?
<8> 读取通讯录联系人,两个表嵌套
select location from Data1 where id=(select outkey from Data2 where num=1)
<9> 插入某列
alter table info add age integer
<10> 删除数据库
drop database 数据库名字;
<11> 删除表
drop table 表名;
drop table info;
<12> 修改表结构 添加一列
alter table 表名 add 字段名字 字段类型;
alter table info add phone varchar(20);
<13> MySQL的约束
a.非空约束 not null
* 表示数据不能为空
b.唯一性约束 unique
* 表中的记录不能重复的
c.主键约束 primary key
* 表示非空,唯一性
d. 自动增长 auto_increment
create table test(id int primary key auto_increment,name varchar(20));
<14> where条件的使用
where子句使用
a. 运算符 <, >, >=, <=
b. in 在范围内
select * from student where Android in (70,90); 注意in不是指定起始和结束的范围 而是在()中的数据中做选择
如果想指定起始和结束的范围要使用and 条件
c. and 条件同时满足
select * from student where Android>=70 and Android<=90;
d. like 模糊查询
% 表示占位符(通配符) %替换若干文字
select * from student where name like '%zha%';
查询表中名字里含有zha的记录
<15> 排序
order by 字段名 asc 升序
order by 字段名 desc 降序
默认升序的 如果想降序 需要加上desc
select * from student order by English desc;
<16> 聚合函数,分组查询&limit关键字
(1) count()函数
select count(*) from ...where....
(2) sum()函数
select sum(要进行求和字段) from ...where....
(3) avg()函数
select avg(要计算平均数的字段名称) from …
(4) max()函数
select max(字段) from...
(5) min()函数
select min(字段) from...
<17> 分组查询 group by
select name,sum(price) from orders group by name;
按照名字进行分组 查询每种商品的总价格
分组查询如果加上条件不能使用where 而要使用having
select name,sum(price) from orders group by name having sum(price)>5000;
<18> limit
select * from orders limit 2; 从第一条开始显示 显示2条
select * from orders limit 2,3; 从第2+1条开始显示 显示3条记录;
通过limit关键字 可以实现数据的分页查询(只查询一部分数据)
<19> 当某一字段的值希望通过其它字值显示出来时(记录转换),可通过下面的语句实现:
case Type when '1' then '普通通道' when '2' then '高端通道' end as Type
其中“Type"是字段名,”1“,”2“是字段值
<20> 返回数据库中用户表的表单名:
select name from table where type = 'u' and status >=2
<21> 按拼音首字母排序:
select * from table order by 列名 Collate Chinese_PRC_CS_AS_KS_WS
首先,在这里的collate是一个子句,主要是定义排序规则,可应用于数据库定义或列定义;或应用于字符串表达式以应用排序规则投影。
语法是collate collation_name。参数collate_name是应用于表达式、列定义或数据库定义的排序规则的名称。
•collation_name 可以只是指定的 Windows_collation_name 或 SQL_collation_name。
1.Windows_collation_name 是 Windows 排序规则的排序规则名称。参见 Windows 排序规则名称。
2.SQL_collation_name 是 SQL 排序规则的排序规则名称。参见 SQL 排序规则名称。
注:SQL SERVER的排序规则平时使用不是很多,也许不少初学者还比较陌生,但有 一个错误大家应是经常碰到: SQL SERVER数据库,在跨库多表连接查询时,若两数据 库默认字符集不同,系统就会返回这样的错误:“无法解决 equal to 操作的排序规则冲突。”
<22> 按姓氏笔画排序:
Select * From table Order By CustomerName Collate Chinese_PRC_Stroke_ci_as
注释如上。
<23> 指定值的范围查询:
1.stockname like ‘%[a-zA-Z]%'
2.stockname like '[^F-M]‘
其中
[ ]指定值的范围
^ 排除指定范围
<24> 对查询结果随机排序:
SELECT * FROM table Orders ORDER BY NEWID()
<25> 返回两个表中共有的所有记录:
select tableA.a tableB.b from tableA inner join tableB as b ontableA.a= b.b
其中sql中as的用法这里就不做熬述。
<26> 等待时间再执行语句:
waitfor delay '00:00:05‘
select * from studentinfowaitfor time '23:08:00
<27> 向一个表A中插入记录,并且插入的记录在A中不存在(通过一个字段来判断):
insert into tableA (tracekey,muteSMS,CreateTime,traceuser,tracetime,traceSlot,traceduration)
Select 'TRACE_TIMER',0,getdate(),mobileid,getdate(),'30','0' from tableB where corpid = 10001
and not exists (select traceuser from tableA ) and mobileid like '13' and len(mobileid) = 11
更多Sqlite语法:http://www.runoob.com/sqlite/sqlite-tutorial.html