SQLite 簡單使用(一)

2021-09-28 12:14:12 字數 1622 閱讀 5786

sqlite 使用

//新增列

"alter table user add column sync_state integer not null default 1"

//建立表

"create table if not exists 'localuser' ('uid' integer not null primary key autoincrement,'user_name' text , 'class_name' text, 'face_code' text, 'card_id' text,'reader_id' text not null,'sync_state' integer not null default 0)"

//建立索引

"create index index_user_face_code on user (face_code)"

//建立唯一索引 create unique index index_name on table_name(column_name)

"create unique index index_user_face_code on user (face_code)"

//刪除表 drop table table_name

"drop table user"

關係表

on delete

on update

外來鍵的on delete和 on update 可以用來配置 當從父表中刪除某些行時發生的行為(on delete). 或者修改存在的行的父鍵的值, 發生的行為(on update)

no action: 當父鍵被修改或者刪除時, 沒有特別的行為發生

restrict: 存在乙個或者多個子鍵對應於相應的父鍵時, 禁止刪除(on delete restrict)或者修改(on update restrict) 父鍵

restrict與普通的外來鍵約束的區別是, 當欄位(field)更新時, restrict行為立即發生

set null: 父鍵被刪除(on delete set null) 或者修改 (on update set null)

set default: 類似於set null

cascade: 將實施在父鍵上的刪除或者更新操作,傳播給與之關聯的子鍵.

on delete cascade   級聯刪除

on update cascade 級聯更新

當更新或刪除主鍵表時,那麼外來鍵表也會跟隨一起更新或刪除

約束

使用sqlite建表,通常會使用id作為唯一標示,使用primary keyautocrement進行修飾

而主鍵是不可以重複的。但是在這張表中還有其他的column也不允許重複,則可以使用unique約束

常用的約束有:

在 sqlite 中,alter table 命令允許使用者重新命名表,或向現有表新增乙個新的列。重新命名列,刪除一列,或從乙個表中新增或刪除約束都是不可能的

參考:​ sqlite—使用約束

​ ​

​ ​ sql union 和 union all 操作符

SQLite 簡單使用

在ubuntu 12.04下進行sqlite開發簡單例項如下 1 安裝sqlite3 hadron hadron sudo apt get install sqlite sqlite3 2 檢視版本號 hadron hadron sqlite3 version 3 建立test資料庫 hadron ...

ios簡單sqlite使用

sqlite是嵌入式的和輕量級的sql資料庫。sqlite是由c實現的。廣泛用於包括瀏覽器 支援html5的大部分瀏覽器,ie除外 ios android以及一些便攜需求的小型web應用系統。使用sqlite前的準備 使用sqlite是很多做ios開發中第一次面對c的情況,包括我。因為sqlite是...

SQLite 簡單使用(二)

建立表create table if not exists localuser id integer primary key autoincrement not null,user id text not null,location text 改變表名alter table 舊表名 rename t...