顯示具有 SQLite Studio 標籤的文章。 顯示所有文章
顯示具有 SQLite Studio 標籤的文章。 顯示所有文章

2021-01-24

SQL : DROP TABLE

SQL : DROP TABLE
DROP TABLE 除了會清除資料表內的資料,也會將這個資料表在資料庫中的結構定義資料一併清除,整個資料表都清光光。

DROP TABLE的語法(Syntax)格式:
DROP TABLE [IF EXISTS] 資料表名稱;

  1. 可以增加 [IF EXISTS] 檢查,確認資料表存在,再予清除。
  2. DELETE 跟 DROP 不一樣,DELETE只清除資料,不清除資料表的結構定義。

以下在測試資料庫下執行,預計刪除TestWind資料庫下的Emp_Id_Name資料表:
  1. 測試環境的資料庫,可以參閱以下網址連結來建立:
  2. 選取要作業的資料庫對象(TestWind),開啟(SQL Editor):Tools → Open SQL Editor
  3. Emp_Id_Name資料表,可以透過以下指令製造:
    CREATE TABLE Emp_Id_Name AS 
      SELECT EmployeeId, LastName, FirstName FROM Employee;
    

  4. 在Query分頁中輸入所要執行的指令
    DROP TABLE Emp_Id_Name;

  5. 執行SQL指令:(F9) Execute SQL
  6. Status : 確認SQL指令執行無誤

參考資料:
SQL As Understood By SQLite : DROP TABLE  https://sqlite.org/lang_droptable.html

SQL : CREATE TABLE ... AS SELECT ...

SQL : CREATE TABLE ... AS SELECT ... 使用SELECT的結果建立資料表,並將條件過濾後的查詢結果,匯入新建立的資料表中。
  1. 測試環境的資料庫,可以參閱以下網址連結來建立:
  2. 選取要作業的資料庫對象(TestWind),開啟(SQL Editor):Tools → Open SQL Editor
  3. 在Query分頁中輸入所要執行的指令
    CREATE TABLE Album_20190920 as 
      SELECT AlbumId, Title FROM Album WHERE Title like 'A%';
    

  4. 執行SQL指令:(F9) Execute SQL
  5. Status : 確認SQL指令執行無誤


  6. 確認資料表Album_20190920已建立,
    包含兩個資料欄位:AlbumId, Title,
    只匯入A開頭的資料


  7. 注意:查看DLL分頁
    PRIMARY KEY 等限制條件(Constraints)、結構定義的內容,不會被匯入。
    資料型態:INTEGER → 被轉換成 INT,NVARCHAR(160) →被轉換成 TEXT

用SQLiteStudio跨資料庫複製或搬移資料表內容

SQLite Studio 資料表的複製、移動功能,可以讓SQLite 的資料庫間,快速地達到資料複製(copy)或搬移(move)的需求,尤其是在測試資料的過程,更能感受到這個功能的妙用。

  1. 這個學習範例所需的資料庫環境,可以參閱:
  2. 來源資料庫:Chinook,有11個資料表,每個資料表都有資料
    目的資料庫:TestWind ,沒有資料表、沒有資料
  3. 選取Chinook 的 Artist 資料表,按住滑鼠左鍵,拖曳到 TestWind 的 Tables 位置,放開左鍵。
    勾選:include data, include indexes, include triggers
    點選:Copy選項


  4. Referenced tables的提醒:
    SQLite Studio 提醒 Artist 這個資料表被 Albumn, PlaylistTrack, Track, InvoiceLine 等資料 Reference了,提示:要不要一併匯入這些資料表?
    這裡選擇:No,只Copy Artist資料表
  5. 確認複製成功。

2021-01-23

用SQLite Studio來CREATE TABLE

自行輸入CREATE TABLE的指令,在對CREATE TABLE指令,還不是很清楚孰悉的情況下,是有一些難度的,SQLite Studio這時候,可以發揮極佳的輔助功能,輕鬆地幫忙使用者CREATE TABLE。
完成資料表建立後,還可以檢視取得CREATE這個TABLE的SQL指令內容。

這裡CREATE TABLE2的目標,是要完成一個像下圖內容的資料表:
資料表名稱:Album,包含三個資料欄位:AlbumId, Title, ArtistId,... 詳細資料如下表 ...
>
  1. 選取要執行這段指令的資料庫,可以參閱:
  2. Structure → Create a table
    或 點選 工具列上的『Create a table』


  3. 輸入表格名稱(Table name):Album
    按下『Add Column(Ins)』鈕


  4. 新增資料欄位:
    以AlbumId為例,輸入 Column name,選取Data type ,
    限制(Constraints)定義的選項,有:Primary Key, Foreign Key, Unique, Check conditions, Not NULL, Collate, Default等,每一個選項都可以按『Configure』鈕,進行進一步的設定。

  5. 新增資料欄位 :
    Column name : ArtistId 的 Foreign Key 設定
  6. Commit structure change,儲存表格新增。


  7. 切換到DDL分頁,查看剛剛透過經由程式頁面操作所得到的DDL SQL指令碼

    CREATE TABLE Album (
        AlbumId  INTEGER        CONSTRAINT PK_Album PRIMARY KEY
                                NOT NULL
                                DEFAULT NULL,
        Title    NVARCHAR (160) NOT NULL
                                DEFAULT NULL,
        ArtistId INTEGER        REFERENCES Artist (ArtistId) ON DELETE NO ACTION
                                                             ON UPDATE NO ACTION
                                NOT NULL
                                DEFAULT NULL
    );
    


用SQLiteStudio建立SQL學習環境

2021-01-21

SQLite管理工具SQLite Studio


資料更新:
2019-12-30起,SQLiteStudio的原始碼及程式下載,已經移至GitHub
https://github.com/pawelsalawa/sqlitestudio/releases
如需要舊版的資料:
(3.x.x) : https://www.dropbox.com/sh/ao4nz2qjfsz2yuy/AABwiiss3do7n0wNecuk-uyna?dl=0
(2.x.x) : https://www.dropbox.com/sh/iyilxtepgswpdlm/AADmYlJ4QRYWn_eo9u4fPn0Aa?dl=0


原內容:
之前介紹的SQLite-tools是文字介面的管理程式,還有圖形介面的DB Browser for SQLite,但我最常用的是SQLite Studio,現在就來介紹一下SQLite Studio:

SQLite Studio官方網站:https://sqlitestudio.pl/index.rvt
SQLite Studio下載網誌:https://sqlitestudio.pl/index.rvt?act=download

提供Windows / Linux / MacOSX 安裝版即可攜版
有提供SHA-256,下載後先比對一下再使用,用起來會更放心。


以Windows portable SQLiteStudio-3_2_1.zip為例:(解壓縮後,可以直接使用)


SQLite Studio 主要的功能簡介:
  • 主畫面:
  • Database→Connect to database
  • Database→Disconnect from database
  • Database→Add a database
  • Database→Edit the database
  • Database→Remove the database
  • Database→Export the database
    可以匯出的格式:HTML / JSON / PDF / SQL / XML
    可以指定文字編碼(text encoding)
  • Database→Convert database type
    以開啟SQLite3為例,可以指定轉換為:SQLite2 / SQLCipher / System.Data.SQLite / WxSQLite3
  • Database→Vacuum
  • Database→Integrity check
  • Database→Refresh selected database schema
  • Database→Refresh all database schema
  • Structure→Create a table
  • Structure→Edit the table
  • Structure→Delete the table
  • Structure→Create an index
  • Structure→Edit the index
  • Structure→Create a trigger
  • Structure→Edit the trigger
  • Structure→Delete the trigger
  • Structure→Create a view
  • Structure→Edit the view
  • Structure→Delete the view
  • View→Databases
  • View→Status
  • View→Database toolbar
  • View→Structure toolbar
  • View→Tools
  • View→Window list
  • View→View toolbar
  • View→Tile windows
  • View→Tile windows horizontally
  • View→Tile windows vertically
  • View→Cascade window
  • View→Close selected windows
  • View→Close all windows but selected
  • View→Close all windows
  • View→Restore recently closed window
  • View→Rename selected windows
  • View→Window list
  • Tools→Open SQL editor
  • Tools→Open DDL history
  • Tools→Open SQL functions editor
  • Tools→Open collations editor
  • Tools→Import 
  • Tools→Export
  • Tools→Open configuration dialog

  • Help→User Manual
  • Help→SQLite documentation
  • Help→Open home page
  • Help→Open forum page
  • Help→Check for updates
  • Help→Report a bug
  • Help→Propose a new feature
  • Help→Bugs and feature requests
  • Help→Licence
  • Help→About