自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

SQLite數(shù)據(jù)庫簡介之對表的操作

數(shù)據(jù)庫 其他數(shù)據(jù)庫
本文主要介紹SQLite的基礎(chǔ)知識以及創(chuàng)建、修改和刪除表的操作,希望會對讀者有所幫助。

一、數(shù)據(jù)庫定義語言 DDL

在關(guān)系型數(shù)據(jù)庫中,數(shù)據(jù)庫中的表 Table、視圖 View、索引 Index、關(guān)系 Relationship 和觸發(fā)器 Trigger 等等,構(gòu)成了數(shù)據(jù)庫的架構(gòu)Schema。 在SQL語句中,專門有一些語句用來定義數(shù)據(jù)庫架構(gòu),這些語句被稱為“數(shù)據(jù)庫定義語言”,即 DDL。

SQLite 數(shù)據(jù)庫引擎支持下列三種DDL語句:

  • CREATE
  • ALTER TABLE
  • DROP

其中,CREATE 語句用來創(chuàng)建表Table、視圖View、索引Index、關(guān)系Relationship 和觸發(fā)器Trigger, DROP語句用來刪除表Table、視圖View、索引Index、關(guān)系Relationship 和觸發(fā)器Trigger, ALTER TABLE 語句用來改變表的結(jié)構(gòu)。

二、SQLite 中的數(shù)據(jù)類型

SQLite 數(shù)據(jù)庫中的數(shù)據(jù)一般由以下幾種常用的數(shù)據(jù)類型組成:

  • NULL - 空值
  • INTEGER - 有符號整數(shù)
  • REAL - 浮點(diǎn)數(shù)
  • TEXT - 文本字符串
  • BLOB - 二進(jìn)制數(shù)據(jù),如圖片、聲音等等

SQLite 也可以接受其他數(shù)據(jù)類型。

三、創(chuàng)建表 CREATE TABLE

首先,創(chuàng)建一個 test.db 數(shù)據(jù)庫并進(jìn)入 SQLite 命令行環(huán)境,還記得怎么做嗎?

  1. myqiao@ubuntu:~$ sqlite3 test.db  
  2.  
  3. -- Loading resources from /home/myqiao/.sqliterc  
  4.  
  5. SQLite version 3.7.4  
  6.  
  7. Enter ".help" for instructions  
  8.  
  9. Enter SQL statements terminated with a ";"  
  10.  
  11. sqlite> .tables  
  12.  
  13. sqlite>  

向上面這樣,我們就在終端中創(chuàng)建了一個 test.db 數(shù)據(jù)庫,并且通過 .tables 命令查詢數(shù)據(jù)庫中的表,結(jié)果沒有任何返回,因?yàn)閿?shù)據(jù)庫本來就是空的嘛。

下面我們創(chuàng)建一個 Student 表,其中包含 Id、Name、Age 等字段。

  1. sqlite>   
  2.  
  3. sqlite> CREATE TABLE Students(Id integer,Name text,age integer);  
  4.  
  5. sqlite> .tables  
  6.  
  7. Students  
  8.  
  9. sqlite> .schema Students  
  10.  
  11. CREATE TABLE Students(Id integer,Name text,age integer);  
  12.  
  13. sqlite>  

向上面這樣,一個 Students 表就被建立了,這回再運(yùn)行 .tables 命令就有響應(yīng)了,系統(tǒng)告訴我們數(shù)據(jù)庫中現(xiàn)在有一個 Students 表, 運(yùn)行 .schema 命令,返回了我們創(chuàng)建這個表的 SQL 命令。

四、修改表 ALTER TABLE

SQLite 僅僅支持 ALTER TABLE 語句的一部分功能,我們可以用 ALTER TABLE 語句來更改一個表的名字,也可向表中增加一個字段(列),但是我們不能刪除一個已經(jīng)存在的字段,或者更改一個已經(jīng)存在的字段的名稱、數(shù)據(jù)類型、限定符等等。

  • 改變表名 - ALTER TABLE 舊表名 RENAME TO 新表名
  • 增加一列 - ALTER TABLE 表名 ADD COLUMN 列名 數(shù)據(jù)類型 限定符

下面我們來演示一下,將前面的 Students 表的名字改為 Teachers

  1. sqlite> 
  2.  
  3. sqlite> .tables  
  4.  
  5. Students  
  6.  
  7. sqlite> ALTER TABLE Students RENAME TO Teachers;  
  8.  
  9. sqlite> .tables  
  10.  
  11. Teachers  
  12.  
  13. sqlite> 

原來數(shù)據(jù)庫中只有一個Students表,改名以后再運(yùn)行 .tables命令,發(fā)現(xiàn)Students表已經(jīng)沒了,現(xiàn)在變成了Teachers表。

下面改變 Teachers 表的結(jié)構(gòu),增加一個Sex列

  1. sqlite> 
  2.  
  3. sqlite> .schema Teachers  
  4.  
  5. CREATE TABLE "Teachers"(Id integer,Name text,age integer);  
  6.  
  7. sqlite> ALTER TABLE Teachers ADD COLUMN Sex text;  
  8.  
  9. sqlite> .schema Teachers  
  10.  
  11. CREATE TABLE "Teachers"(Id integer,Name text,age integer, Sex text);  
  12.  
  13. sqlite> 

五、刪除表 DROP TABLE

刪除一個表很簡單,只要給出表名即可

  • 刪除表 - DROP TABLE 表名

下面,我們將 test.db 中的 Teachers 表刪除

  1. sqlite>   
  2.  
  3. sqlite> .tables  
  4.  
  5. Teachers  
  6.  
  7. sqlite> DROP TABLE Teachers;  
  8.  
  9. sqlite> .tables  
  10.  
  11. sqlite>  

刪除 Teachers 表后再運(yùn)行 .tables 命令,發(fā)現(xiàn)數(shù)據(jù)庫已經(jīng)空了。

到此本文已經(jīng)介紹完畢了,若文中存在不妥之處,歡迎各位批評指正,謝謝大家的支持!

【編輯推薦】

  1. 開發(fā)BI系統(tǒng)時的需求分析研究
  2. 嵌入式數(shù)據(jù)庫Sqlce讀取數(shù)據(jù)過程簡介
  3. 微軟WP7本地數(shù)據(jù)庫之SQLite編程技巧
  4. 微軟WP7本地數(shù)據(jù)庫之Sterling編程技巧
  5. 如何不使用數(shù)據(jù)庫緩存,還達(dá)到實(shí)時更新
責(zé)任編輯:趙鵬 來源: 博客園
相關(guān)推薦

2011-04-18 13:40:15

SQLite

2010-03-04 15:31:44

Python SQLI

2011-07-05 14:46:34

2018-07-13 09:20:30

SQLite數(shù)據(jù)庫存儲

2011-07-05 10:16:16

Qt 數(shù)據(jù)庫 SQLite

2024-03-07 13:02:57

PythonSQLite數(shù)據(jù)庫

2011-07-26 18:11:56

iPhone Sqlite 數(shù)據(jù)庫

2011-07-20 12:34:49

SQLite數(shù)據(jù)庫約束

2019-08-15 07:00:54

SQLite數(shù)據(jù)庫內(nèi)存數(shù)據(jù)庫

2017-07-12 09:20:42

SQLite數(shù)據(jù)庫移植

2011-07-07 16:42:38

iPhone Sqlite3 數(shù)據(jù)庫

2011-08-24 13:49:45

Access數(shù)據(jù)庫轉(zhuǎn)化

2015-08-21 12:59:38

Oracle數(shù)據(jù)庫

2013-04-01 10:49:51

iOS開發(fā)sqlite數(shù)據(jù)庫

2011-08-04 18:00:47

SQLite數(shù)據(jù)庫批量數(shù)據(jù)

2011-08-02 16:16:08

iPhone開發(fā) SQLite 數(shù)據(jù)庫

2024-10-28 16:31:03

2010-06-02 18:07:44

MySQL數(shù)據(jù)庫

2013-03-27 09:47:01

Android開發(fā)SQAndroid SDK

2011-03-25 13:34:20

SQLite數(shù)據(jù)庫
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號