SQL数据库是我们常用的一种数据库。下面是对SQL数据库的一些基本操作进行总结。

一.数据库查询语句:select

1. 查询所有数据:

select * from 表名;

如:

select * from student;

2.按照一定的条件查找:

select * from 表名 where 条件;

如:

select * fromstudent where id<20;

3.范围条件查询:

select * from 表名 where 字段 between  值1  and 值2 ;

如:

select * fromstudent  where id<20 and id>10;
select * fromstudent where id between 10 and 20;

4.模糊查询:

select * from 表名 where 字段 like '%条件%';

如:

select * fromstudent  where name like '%李四%';
select * fromstudent  where name like '李_';

(查询人名中第一个字是“李”,后面有一个字)

%和_叫做通配符

5.复合查询:

select * from 表名 where 条件 and 另一个条件;

如:

select * from  st udent where gender = '男' and id > 15;

6.查询个数:

select count(*) from 表名 where 条件

如:

select count(*) as 别名 from   student   where  gender = '男' and id > 12;;

7.查询结果按顺序排列:

正序、倒序(asc/desc)

select * from 表名 where 条件 order by 字段 desc/asc;

8、按照limit查找某个范围内的数据:

SELECT  * FROM  st udent   order by id asc  limit  0, 15;(代表查找出的第一页的信息)
SELECT  * FROM  st udent  order by id asc  limit 15, 15;(代表查找出的第二页的信息)

【备注:】limit后面的两个数字中:第一个代表偏移量,第二个代表每页展示的数量

               偏移量 = (当前页码数-1) * 每页显示条数

SELECT 字段 AS  别名  FROM 表名 WHERE...   ORDER BY ...  LIMIT....

9、sql常用函数:

count()

length()

min()

max()

二. 删除数据:delete

delete from 表名 where 条件;

如:

delete fromstudent   where id<20;

DELETE    FROM    表名    WHERE子句

三.插入新数据:insert

insert into 表名(字段) values(值);

insert into exam_weburl(webname , weburl , info , bigtypeid) values('人人网', 'renren.com' , '这个网站不错' , 3);

四.更新数据:update

update 表名 set 字段1=值1, 字段2=值2 where 条件;
update exam_weburl set info='这个网站不太好' where id=73;
update exam_weburl set webname='人人2', weburl='www.renren.com' where id=73;

五、创建表结构的语句:

CREATE TABLE  表名 (_id INTEGER PRIMARY KEY AUTOINCREMENT , 字段2, 字段3...)

例如:CREATE TABLE tb_newwords (_id INTEGER PRIMARY KEY AUTOINCREMENT , words  , detail );

六、更新表结构的语句:

1、如需在表中添加列,请使用下列语法:

ALTER TABLE table_name

ADD column_name datatype

(sqlite 里不支持列的删除和修改)

2、要删除表中的列,请使用下列语法:

ALTER TABLE table_name

DROP COLUMN column_name   (sqlite不支持的)

3、要改变表中列的数据类型,请使用下列语法:

ALTER TABLE table_name

ALTER COLUMN column_name datatype (sqlite不支持的)

4、

【备注:名词的解释】

DDL (data define language)  数据定义语句 create,drop,alter

DML (data manipulate language) 数据操纵语句 insert,delete,update,select  (CRUD)

DCL  数据控制语句 grant, revoke.

TCL(transaction controll language)  事务控制语句.

学习SQL的主要重心应该放在DML语句上,而DML语句中的重点应该是在select语句上。

CRUD:是指在做计算处理时的增加(Create)、查询(Retrieve)(重新得到数据)、更新(Update)和删除(Delete)几个单词的首字母简写。主要被用在描述数据库的基本操作。