基礎數據類型
類型 |
說明 |
int(integer) |
-2^31(-2,147,483,648)到2^31(2,147,483,647)的整型數字 |
float |
-1.79E+308到1.79E+308可變精度的數字 |
real |
-3.04E+38到3.04E+38可變精度的數字 |
char |
定長非Unicode的字符型數據,最大長度8000 |
varchar |
變長非Unicode的字符型數據,最大長度8000 |
text |
變長非Unicode的字符型數據,最大長度爲2^31-1(2G) |
例:
date(出生日期) |
name(姓名) |
gender(性別) |
hobby(愛好) |
char(16) |
varchar(256) |
char(16) |
text |
19940801 |
小白 |
男 |
C++ |
19940802 |
小黑 |
女 |
PHP |
sql 通用語句
注:命令行語句結束要加
; (分號)
建立數據庫
注: sqlite3 提供的命令行功能語句,非 sql 通用語句
.open person.db
建立數據表格
- create table 表名(字段名 數據類型, 字段名 數據類型, 字段名 數據類型);
create table child(date char(16), name varchar(256), gender char(16), hobby text);
插入數據
- insert into 表名 values('字段數據', '字段數據', ' 字段數據', '字段數據');
注:若是數據類型是 char,varchar,text 數據必須使用 '' 或 "" 引用
insert into child values('19940801', '小白', '男', 'C++');
insert into child values('19940802', '小黑', '女', 'PHP');
查詢數據
- select 字段名, ..., 字段名 from 表名;
注:字段名若是是多個能夠用 " , " 逗號隔開,若是是全部能夠用 " * " 星號
查詢所有:sql
sqlite> select * from child;
19940801|小白|男|C++
19940802|小黑|女|PHP
查詢多個:數據庫
sqlite> select name, date from child;
小白|19940801
小黑|19940802
- select 字段名, ..., 字段名 from 表名 where 條件;
sqlite> select * from child where gender='女';
19940802|小黑|女|PHP
sqlite> select * from child where date like '%1';
19940801|小白|男|C++
sqlite> select * from child where date like '%1' and hobby like 'C%';
19940801|小白|男|C++
sqlite> select * from child where date like '%1' or hobby like 'P%';
19940801|小白|男|C++
19940802|小黑|女|PHP
更新數據
- update 表名 set 字段1=字段1值, 字段2=字段2值... where 條件表達式
sqlite> update child set hobby='JAVA' where name = '小黑';
sqlite> select * from child;
19940801|小白|男|C++
19940802|小黑|女|JAVA
刪除數據
delete form 表名; 刪除整個表數據,不會刪除表格,表格任在數據庫中
drop table 表名; 整個表格從數據庫中刪除
delete from 表名 where 條件;命令行
.table
child
sqlite> select * from child;
19940801|小白|男|C++
19940802|小黑|女|JAVA
sqlite> delect from child where hobby='JAVA';
sqlite> select * from child;
19940801|小白|男|C++
.table
child
delete from child;
sqlite> select * from child;
sqlite>
sqlite> .table
child
.table
child
sqlite> drop table child;
sqlite> .table
sqlite>
查詢建立表命令
注: sqlite3 提供的命令行功能語句,非 sql 通用語句
sqlite> .schema child
CREATE TABLE child(date char(16), name varchar(256), gender char(16), hobby text);
添加字段
- alter table 表名 add column 字段名 數據類型 [default 對應值];
sqlite> select * from child;
19940801|小白|男|C++
19940802|小黑|女|PHP
sqlite> alter table child add column height int;
sqlite> select * from child;
19940801|小白|男|C++|
19940802|小黑|女|PHP|
sqlite> update child set height=171;
sqlite> select * from child;
19940801|小白|男|C++|171
19940802|小黑|女|PHP|171
sqlite> alter table child add column addr varchar(256) default '南京';
sqlite> select * from child;
19940801|小白|男|C++|171|南京
19940802|小黑|女|PHP|171|南京
查詢表結構信息
sqlite> pragma table_info(child);
0|date|char(16)|0||0
1|name|varchar(256)|0||0
2|gender|char(16)|0||0
3|hobby|text|0||0
4|height|int|0||0
5|addr|varchar(256)|0|'南京'|0
建立表格時設置字段約束
屬性 |
說明 |
integer promary key autoincrement |
做爲主鍵,自動遞增 |
not NULL |
不能爲 NULL |
unique |
惟一,不能重複 |
default |
默認值 |
例:
id |
name |
status |
online |
1 |
led1 |
0 |
0 |
2 |
led2 |
0 |
0 |
3 |
led3 |
0 |
0 |
4 |
led4 |
0 |
1 |
create table device(id integer primary key autoincrement, // 設置 id 爲主鍵,並自增加
name varchar(256) unique, // 設置 name 惟一
status int not NULL default 0, // 設置 status 不能爲空,默認值爲 0
online int not NULL); // 設置 online 不能爲空
例:
sqlite> crate table device(id integer primary key autoincrement, name varchar(256) unique, status int not NULL default 0, online int not NULL);
sqlite> .table
device
sqlite> create table device(id integer primary key autoincrement, name varchar(256) unique, status int not NULL default 0, online int not NULL);
Error: table device already exists
- if not exists 判斷表格是否存在,若是不存在則建立
sqlite> .table
device
sqlite> create table if not exists device(id integer primary key autoincrement, name varchar(256) unique, status int not NULL default 0, online int not NULL); // 注意這裏 !!
sqlite> create table if not exists device(id integer primary key autoincrement, name varchar(256) unique, status int not NULL default 0, online int not NULL); // 注意這裏 !!
sqlite> .table
device
主鍵的惟一性 ; unique 屬性的惟一性
sqlite> select * from device;
sqlite> insert into device values(1, 'led1', 0, 0);
sqlite> select * from device;
1|led1|0|0
sqlite> insert into device values(1, 'led1', 0, 0); // 注意這裏!!
Error: UNIQUE constraint failed: device.id
sqlite> insert into device values(2, 'led1', 0, 0); // 注意這裏!!
Error: UNIQUE constraint failed: device.name
sqlite> insert into device values(2, 'led2', 0, 0);
sqlite> select * from device;
1|led1|0|0
2|led2|0|0
sqlite>
默認值的使用 (指定字段(列)插入,沒有指定的將使用默認值)
sqlite> select * from device;
1|led1|0|0
2|led2|0|0
sqlite> insert into device (name, online) values('led3', 0); // 注意這裏 !!
sqlite> insert into device (name, online) values('led4', 1); // 注意這裏 !!
sqlite> select * from device;
1|led1|0|0
2|led2|0|0
3|led3|0|0
4|led4|0|1