15.3、SQL基础入门

来源:互联网 发布:跟聪明的女朋友知乎 编辑:程序博客网 时间:2024/05/16 08:34
SQL基础入门
一.数据库查询语句:select

1. 查询所有数据:
select * from 表名;
select * from exam_books;

2.按照一定的条件查找:
select * from 表名 where 条件;
select * from exam_books where id<20;

3.范围条件查询:
select * from 表名 where 字段 between  值1  and 值2 ;
select * from exam_books where id<20 and id>10;
select * from exam_books where id between 10 and 20;
select * from exam_books where addtime between '2011-03-17 00:00:00' and '2011-03-18 00:00:00';

4.模糊查询:
select * from 表名 where 字段 like '%条件%';
select * from exam_books where bookname like '%马克思%';
select * from exam_books where bookname like '马__';
(查询书名中第一个字是“马”,后面有两个字)
%和_叫做通配符

5.复合查询:
select * from 表名 where 条件 and 另一个条件;
select * from exam_questions  where coursecode = '03706' and chapterid = 2;

6.查询个数:
select count(*) from 表名 where 条件
select count(*) as 别名 from exam_questions  where coursecode = '03706' and chapterid = 2;

7.查询结果按顺序排列:
正序、倒序(asc/desc
select * from 表名 where 条件 order by 字段 desc/asc;
select * from exam_questions where coursecode = '03706' order by chapterid;

8、按照limit查找某个范围内的数据:
SELECT  * FROM exam_questions  order by id asc  limit  0, 15;(代表查找出的第一页的信息)
SELECT  * FROM exam_questions  order by id asc  limit 15, 15;(代表查找出的第二页的信息)
【备注:】limit后面的两个数字中:第一个代表偏移量,第二个代表每页展示的数量
                偏移量 = (当前页码数-1) * 每页显示条数


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


9、sql常用函数
count()
length()
min()
max()
datetime('now')
date('now')
time('now')

10、group by分组查询
select name ,sum(score) from students group by name having name='xxx'


二. 删除数据:delete
delete from 表名 where 条件;
delete from exam_questions where id<2000;
delete from exam_questions where coursecode='00041' and chapterid=1;

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
2、要删除表中的列,请使用下列语法:
ALTER TABLE table_name 
DROP COLUMN column_name

3、要改变表中列的数据类型,请使用下列语法:
ALTER TABLE table_name
ALTER COLUMN column_name datatype


【备注:】

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)几个单词的首字母简写。主要被用在描述数据库的基本操作。



【补充】sqlite3.exe

1、.open 数据库名

        如果open一个不存在的数据库,则会新建该表,如果该表没有看到,则需要创建表 CREATE TABLE 表名先创建表,才能看到数据库文件出现。

2、.help

3、.show

4、.tables 展示所有数据库的表

5、.schema 表名


【备注:】

SQLite支持哪些数据类型?

1)、NULL 值为NULL

2)、INTEGER 值为带符号的整型,根据类别用1,2,3,4,6,8字节存储

3)、REAL 值为浮点型,8字节存储

4)、TEXT 值为text字符串,使用数据库编码(UTF-8, UTF-16BE or UTF-16-LE)存储

5)、BLOB 值为二进制数据,具体看实际输入

实际上:SQLite是无类型的. 这意味着你可以保存任何类型的数据到你所想要保存的任何表的任何列中, 无论这列声明的数据类型是什么,对于SQLite来说字段不指定类型是完全有效的.

虽然理论上SQLite只支持以上五种数据类型,但是如果写成以下数据类型,sqlite3也能够接受:

  •   smallint  16 位元的整数 
  •   interger  32 位元的整数 
  •   decimal(p,s)  p 精确值和 s 大小的十进位整数,精确值p是指全部有几个数(digits)大小值 ,s是指小数点後有几位数。如果没有特别指定,则系统会设为 p=5; s=0 。 
  •   float   32位元的实数。 
  •   double   64位元的实数。 
  •   char(n)   n 长度的字串,n不能超过 254。 
  •   varchar(n)  长度不固定且其最大长度为 n 的字串,n不能超过 4000。 
  •   graphic(n)  和 char(n) 一样,不过其单位是两个字元 double-bytes, n不能超过127。这个形态是为了支援两个字元长度的字体,例如中文字。 
  •   vargraphic(n)  可变长度且其最大长度为 n 的双字元字串,n不能超过 2000。 
  •   date   包含了 年份、月份、日期。 
  •   time   包含了 小时、分钟、秒。 
  •   timestamp  包含了 年、月、日、时、分、秒、千分之一秒。











0 0
原创粉丝点击