1. 程式人生 > >Linux下安裝及操作SQL資料庫

Linux下安裝及操作SQL資料庫

———————————————————————————————————————

主機作業系統:Centos 6.7

安裝配置:SQL資料庫
郵箱:[email protected]

———————————————————————————————————————

一.SQL資料庫簡介

1.定義

SQL是Structured Query Language(結構化查詢語言)的縮寫。SQL是專為資料庫而建立的操作命令集,是一種功能齊全的資料庫語言。在使用它時,只需要發出“做什麼”的命令,“怎麼做”是不用使用者考慮的。SQL功能強大、簡單易學、使用方便,已經成為了資料庫操作的基礎,並且現在幾乎所有的資料庫均支援

SQL

2.體系

SQL資料庫的資料體系結構基本上是三級結構,但使用術語與傳統關係模型術語不同。在SQL中,關係模式(模式)稱為“基本表”(base table);儲存模式(內模式)稱為“儲存檔案”(stored file);子模式(外模式)稱為“檢視”(view);元組稱為“行”(row);屬性稱為“列”(column)。

3.優點

1)非過程化語言 2)統一的語言 3)是所有關係資料庫的公共語言

4.組成

1.一個SQL資料庫是表(Table)的集合,它由一個或多個SQL模式定義。 2.一個SQL表由行集構成,一行是列的序列(集合),每列與行對應一個資料項 3.一個表或者是一個基本表或者是一個檢視。基本表是實際儲存在資料庫的表,而檢視是由若干基本表或其他檢視構成的表的定義。
4.一個基本表可以跨一個或多個儲存檔案,一個儲存檔案也可存放一個或多個基本表。每個儲存檔案與外部儲存上一個物理檔案對應。 5.使用者可以用SQL語句對檢視和基本表進行查詢等操作。在使用者角度來看,檢視和基本表是一樣的,沒有區別,都是關係(表格)。 6.SQL使用者可以是應用程式,也可以是終端使用者。SQL語句可嵌入在宿主語言的程式中使用,宿主語言有FORTRAN,COBOL,PASCAL,PL/I,C和Ada語言等。SQL使用者也能作為獨立的使用者介面,供互動環境下的終端使用者使用。

二.檢查硬體是否滿足要求

1.確保系統有足夠的 RAM 和交換空間大小,執行以下命令:

   #grep MemTotal /proc/meminfo

   #grepSwapTotal /proc/meminfo

[[email protected] ~]$ grep MemTotal /proc/meminfo

MemTotal:        1939728 kB

[[email protected] ~]$ grep SwapTotal /proc/meminfo

SwapTotal:       4194300 kB

注:所需最小 RAM 為 512MB,而所需最小交換空間為 1GB。對於 RAM 小於或等於 2GB 的系統,交換空間應為 RAM 數量的兩倍;對於 RAM 大於 2GB 的系統,交換空間應為 RAM 數量的一到兩倍。

2.確保有足夠的磁碟空間。Oracle 10g軟體大約需要 2.5GB 的可用磁碟空間,資料庫則另需至少1.2G的磁碟空間

3./tmp 目錄至少需要 400MB 的可用空間。

 要檢查系統上的可用磁碟空間,執行以下命令:

[[email protected] ~]$ df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda2        26G   11G   14G  46% /

tmpfs           948M  312K  947M   1% /dev/shm

4.檢查系統是否已安裝所需的開發包,使用rpm -qa命令,確保以下包已成功安裝,若還沒安裝可到以下地址進行先下載:

http://www.sqlite.org/download.html

解壓並編譯sqlite-autoconf-3140100

[[email protected] ~]$ tar -zxvf sqlite-autoconf-3140100.tar.gz

[[email protected] ~]$ cd sqlite-autoconf-3140100

[[email protected] sqlite-autoconf-3140100]$ ./configure

[[email protected] sqlite-autoconf-3140100]$ make

[[email protected] sqlite-autoconf-3140100]$ sudo make install

三.資料庫管理

1.建立資料庫檔案

[[email protected] sqlite-autoconf-3140100]$ sqlite3 mydbtest

SQLite version 3.14.1 2016-08-11 18:53:32

Enter ".help" for usage hints.

sqlite>

出現sqlite>提示符

2.檢視目前的資料庫。注意資料庫操作命令以 ”.”開頭。

sqlite> .database

seq  name             file                                                      

---  ---------------  ----------------------------------------------------------

0    main             /home/leiyuxing/sqlite-autoconf-3140100/mydbtest  

列出當前使用的資料庫mydbtest

資料庫mydbtest檔案建立在執行命令# sqlite3 mydbtest時,命令列所在的目錄。

3.建立表

sqlite> create table mytable(name varchar(80),num smallint);

4.列出表

檢視建立了哪些表

sqlite> .tables

Mytable

5.查詢某個表

sqlite> .tables my

sqlite> .tables mytable

Mytable

6.插入記錄

sqlite> insert into mytable values('su',21);

sqlite> insert into mytable values('tang',20);

sqlite> insert into mytable values('lei',22);

7.查詢

sqlite> select * from mytable;

su|21

tang|20

lei|22

8.模式查看錶結構

sqlite> .schema

CREATE TABLE mytable(name varchar(80),num smallint);

9.從檔案向表中匯入資料

建立檔案data.txt,內容如下

[[email protected] sqlite-autoconf-3140100]$ vim data.txt

LTian Hong|19

Eng Lish|20

Gao Yuan|23

Wei Da|26

其中“|”是分隔符,分隔符左右不要有空格

匯入資料前查詢

sqlite> select * from mytable;

su|21

tang|20

lei|22

匯入資料

sqlite> .import data.txt mytable

data.txt:2: expected 2 columns but found 1 - filling the rest with NULL

data.txt:4: expected 2 columns but found 1 - filling the rest with NULL

data.txt:6: expected 2 columns but found 1 - filling the rest with NULL

匯入資料後查詢

sqlite> select * from mytable;

su|21

tang|20

lei|22

n Hong|19

|

Eng Lish|20

|

Gao Yuan|23

|

Wei Da|26

10.生成形成資料庫表的SQL指令碼

sqlite> .dump mytable

PRAGMA foreign_keys=OFF;

BEGIN TRANSACTION;

CREATE TABLE mytable(name varchar(80),num smallint);

INSERT INTO "mytable" VALUES('su',21);

INSERT INTO "mytable" VALUES('tang',20);

INSERT INTO "mytable" VALUES('lei',22);

INSERT INTO "mytable" VALUES('n Hong',19);

INSERT INTO "mytable" VALUES('',NULL);

INSERT INTO "mytable" VALUES('Eng Lish',20);

INSERT INTO "mytable" VALUES('',NULL);

INSERT INTO "mytable" VALUES('Gao Yuan',23);

INSERT INTO "mytable" VALUES('',NULL);

INSERT INTO "mytable" VALUES('Wei Da',26);

COMMIT;

11.資料匯出

將輸出定向到檔案

sqlite> .output create.sql

sqlite> .dump mytable

將資料庫表生成的SQL指令碼輸出到create.sql檔案

12.將輸出恢復到標準輸出

sqlite> .output stdout

列印SQLite環境變數到設定

sqlite> .show

        echo: off

         eqp: off

     explain: auto

     headers: off

        mode: list

   nullvalue: ""

      output: stdout

colseparator: "|"

rowseparator: "\n"

       stats: off

       width:

退出資料庫,使用.quit或.q

sqlite> .quit

[[email protected] sqlite-autoconf-3140100]$

四.特殊用法

命令列下直接使用

[[email protected] sqlite-autoconf-3140100]$ sqlite3 mydbtest "select * from mytable;"

su|21

tang|20

lei|22

n Hong|19

|

Eng Lish|20

|

Gao Yuan|23

|

Wei Da|26

五.安裝rlwrap完成上下方向鍵尋找歷史命令

1.下載rlwrap工具

 http://download.csdn.net/detail/sharqueen/5433087

2.安裝rlwrap

[[email protected] ~]$ tar -zxf rlwrap-0.30.tar.gz

[[email protected] ~]$ cd rlwrap-0.30

[[email protected] rlwrap-0.30]$ ./configure

[[email protected] rlwrap-0.30]$ make

[[email protected] rlwrap-0.30]$ sudo make install

3.檢視rlwrap命令

[[email protected] rlwrap-0.30]$ rlwrap

Usage: rlwrap [options] command ...

Options:

  -a[password:]              --always-readline[=password:]

  -A                         --ansi-colour-aware

  -b <chars>                 --break-chars=<chars>

  -c                         --complete-filenames

  -C <name|N>                --command-name=<name|N>

  -D <0|1|2>                 --history-no-dupes=<0|1|2>

  -f <completion list>       --file=<completion list>

  -F <format string>         --history-format=<format string>

  -h                         --help

  -H <file>                  --history-filename=<file>

  -i                         --case-insensitive

  -l <file>                  --logfile=<file>

  -n                         --no-warnings

  -p[ANSI colour spec]       --prompt-colour[=ANSI colour spec]

  -P <input>                 --pre-given=<input>

  -q <chars>                 --quote-characters=<chars>

  -m[newline substitute]     --multi-line[=newline substitute]

  -r                         --remember

  -v                         --version

  -s <N>                     --histsize=<N> (negative: readonly)

  -t <name>                  --set-term-name=<name>

bug reports, suggestions, updates:

http://utopia.knoware.nl/~hlub/uck/rlwrap/

4.使用rlwrap

vim ~/.bash_profile

--使之生效,或是重新登入sql使用者

[[email protected] ~]$ source  ~/.bash_profile

[[email protected] ~]$ sqlplus mydbtest

SQLite version 3.14.1 2016-08-11 18:53:32

Enter ".help" for usage hints.

sqlite>

就可以在輸入 sqlplusmydbtest之後的命令列裡面實現上下尋找歷史命令