<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # SQLite學習手冊(數據表和視圖) 一、創建數據表: 該命令的語法規則和使用方式與大多數關系型數據庫基本相同,因此我們還是以示例的方式來演示SQLite中創建表的各種規則。但是對于一些SQLite特有的規則,我們會給予額外的說明。注:以下所有示例均是在sqlite自帶命令行工具中完成的。 1). 最簡單的數據表: ``` sqlite> CREATE TABLE testtable (first_col integer); ``` 這里需要說明的是,對于自定義數據表表名,如testtable,不能以sqlite_開頭,因為以該前綴定義的表名都用于sqlite內部。 2). 創建帶有缺省值的數據表: ``` sqlite> CREATE TABLE testtable (first_col integer DEFAULT 0, second_col varchar DEFAULT 'hello'); ``` 3). 在指定數據庫創建表: ``` sqlite> ATTACH DATABASE 'd:/mydb.db' AS mydb; sqlite> CREATE TABLE mydb.testtable (first_col integer); ``` 這里先通過ATTACH DATABASE命令將一個已經存在的數據庫文件attach到當前的連接中,之后再通過指定數據庫名的方式在目標數據庫中創建數據表,如mydb.testtable。關于該規則還需要給出一些額外的說明,如果我們在創建數據表時沒有指定數據庫名,那么將會在當前連接的main數據庫中創建該表,在一個連接中只能有一個main數據庫。如果需要創建臨時表,就無需指定數據庫名,見如下示例: --創建兩個表,一個臨時表和普通表。 ``` sqlite> CREATE TEMP TABLE temptable(first_col integer); sqlite> CREATE TABLE testtable (first_col integer);?? ? ``` --將當前連接中的緩存數據導出到本地文件,同時退出當前連接。 ``` sqlite> backup d:/mydb.db sqlite> exit ``` --重新建立sqlite的連接,并將剛剛導出的數據庫作為主庫重新導入。 ``` sqlite> restore d:/mydb.db ``` --查看該數據庫中的表信息,通過結果可以看出臨時表并沒有被持久化到數據庫文件中。 ``` sqlite> .tables testtable??? ``` 4). "IF NOT EXISTS"從句: 如果當前創建的數據表名已經存在,即與已經存在的表名、視圖名和索引名沖突,那么本次創建操作將失敗并報錯。然而如果在創建表時加上"IF NOT EXISTS"從句,那么本次創建操作將不會有任何影響,即不會有錯誤拋出,除非當前的表名和某一索引名沖突。 ``` sqlite> CREATE TABLE testtable (first_col integer); Error: table testtable already exists sqlite> CREATE TABLE IF NOT EXISTS testtable (first_col integer); ``` 5). CREATE TABLE ... AS SELECT: 通過該方式創建的數據表將與SELECT查詢返回的結果集具有相同的Schema信息,但是不包含缺省值和主鍵等約束信息。然而新創建的表將會包含結果集返回的所有數據。 ``` sqlite> CREATE TABLE testtable2 AS SELECT * FROM testtable;?? ? sqlite> .schema testtable2 CREATE TABLE testtable2(first_col INT); ``` .schema命令是sqlite3命令行工具的內置命令,用于顯示當前數據表的CREATE TABLE語句。??? 6). 主鍵約束: --直接在字段的定義上指定主鍵。 ``` sqlite> CREATE TABLE testtable (first_col integer PRIMARY KEY ASC ``` --在所有字段已經定義完畢后,再定義表的數約束,這里定義的是基于first_col和second_col的聯合主鍵。 ``` sqlite> CREATE TABLE testtable2 ( ...> ?? ?first_col integer, ...> ?? ?second_col integer, ...> ?? ?PRIMARY KEY (first_col,second_col) ...> ); ``` 和其他關系型數據庫一樣,主鍵必須是唯一的。 7). 唯一性約束: --直接在字段的定義上指定唯一性約束。 ``` sqlite> CREATE TABLE testtable (first_col integer UNIQUE); ``` --在所有字段已經定義完畢后,在定義表的唯一性約束,這里定義的是基于兩個列的唯一性約束。 ``` sqlite> CREATE TABLE testtable2 ( ...>???? first_col integer, ...>???? second_col integer, ...>???? UNIQUE (first_col,second_col) ...> );?? ? ``` 在SQLite中,NULL值被視為和其他任何值都是不同的,這樣包括和其他的NULL值,如下例: ``` sqlite> DELETE FROM testtable; sqlite> SELECT count(*) FROM testtable; count(*) ---------- 0 sqlite> INSERT INTO testtable VALUES(NULL); sqlite> INSERT INTO testtable VALUES(NULL); sqlite> SELECT count(*) FROM testtable; count(*) ---------- 2?? ? ``` 由此可見,兩次插入的NULL值均插入成功。 8). 為空(NOT NULL)約束: ``` sqlite> CREATE TABLE testtable(first_col integer NOT NULL); sqlite> INSERT INTO testtable VALUES(NULL); Error: testtable.first_col may not be NULL ``` 從輸出結果可以看出,first_col已經被定義了非空約束,因此不能在插入NULL值了。??? 9). 檢查性約束: ``` sqlite> CREATE TABLE testtable (first_col integer CHECK (first_col < 5)); sqlite> INSERT INTO testtable VALUES(4); sqlite> INSERT INTO testtable VALUES(20); _-- 20違反了字段first_col的檢查性約束(first_col < 5)_ Error: constraint failed ``` --和之前的其它約束一樣,檢查性約束也是可以基于表中的多個列來定義的。?? ? ``` sqlite> CREATE TABLE testtable2 ( ...>???? first_col integer, ...>???? second_col integer, ...>???? CHECK (first_col > 0 AND second_col < 0) ...> ); ``` 二、表的修改: SQLite對ALTER TABLE命令支持的非常有限,僅僅是修改表名和添加新字段。其它的功能,如重命名字段、刪除字段和添加刪除約束等均為提供支持。 1). 修改表名: 需要先說明的是,SQLite中表名的修改只能在同一個數據庫中,不能將其移動到Attached數據庫中。再有就是一旦表名被修改后,該表已存在的索引將不會受到影響,然而依賴該表的視圖和觸發器將不得不重新修改其定義。 ``` sqlite> CREATE TABLE testtable (first_col integer); sqlite> ALTER TABLE testtable RENAME TO testtable2; sqlite> .tables testtable2?? ? ``` 通過.tables命令的輸出可以看出,表testtable已經被修改為testtable2。??? 2). 新增字段: ``` sqlite> CREATE TABLE testtable (first_col integer); sqlite> ALTER TABLE testtable ADD COLUMN second_col integer; sqlite> .schema testtable CREATE TABLE "testtable" (first_col integer, second_col integer);?? ? ``` 通過.schema命令的輸出可以看出,表testtable的定義中已經包含了新增字段。??? 關于ALTER TABLE最后需要說明的是,在SQLite中該命令的執行時間是不會受到當前表行數的影響,也就是說,修改有一千萬行數據的表和修改只有一條數據的表所需的時間幾乎是相等的。 三、表的刪除: 在SQLite中如果某個表被刪除了,那么與之相關的索引和觸發器也會被隨之刪除。在很多其他的關系型數據庫中是不可以這樣的,如果必須要刪除相關對象,只能在刪除表語句中加入WITH CASCADE從句。見如下示例: ``` sqlite> CREATE TABLE testtable (first_col integer); sqlite> DROP TABLE testtable; sqlite> DROP TABLE testtable; Error: no such table: testtable sqlite> DROP TABLE IF EXISTS testtable;?? ? ``` 從上面的示例中可以看出,如果刪除的表不存在,SQLite將會報錯并輸出錯誤信息。如果希望在執行時不拋出異常,我們可以添加IF EXISTS從句,該從句的語義和CREATE TABLE中的完全相同。 四、創建視圖: 我們這里只是給出簡單的SQL命令示例,具體的含義和技術細節可以參照上面的創建數據表部分,如臨時視圖、"IF NOT EXISTS"從句等。 1). 最簡單的視圖: ``` sqlite> CREATE VIEW testview AS SELECT * FROM testtable WHERE first_col > 100;?? ? ``` 2). 創建臨時視圖: ``` sqlite> CREATE TEMP VIEW tempview AS SELECT * FROM testtable WHERE first_col > 100; ``` 3). "IF NOT EXISTS"從句: ``` sqlite> CREATE VIEW testview AS SELECT * FROM testtable WHERE first_col > 100; Error: table testview already exists sqlite> CREATE VIEW IF NOT EXISTS testview AS SELECT * FROM testtable WHERE first_col > 100; ``` 五、刪除視圖: 該操作的語法和刪除表基本相同,因此這里只是給出示例: ``` sqlite> DROP VIEW testview; sqlite> DROP VIEW testview; Error: no such view: testview sqlite> DROP VIEW IF EXISTS testview;??? ??? ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看