http://www.178linux.com/79462 http://iceyao.blog.51cto.com/9426658/1571865 http://blog.csdn.net/taochangchang/article/details/16919289
數據庫(Database)是按照數據結構來組織、存儲和管理數據的創建在計算機存儲設備上的倉庫。 簡單來講是自己可視爲電子化的文件櫃——存儲電子文件的處所,用戶能夠對文件中的數據進行新增、截取、更新、刪除等操做。 在經濟管理的平常工做中,經常須要把某些相關的數據放進這樣的「倉庫」,並根據管理的須要進行相應的處理。 例如, 企業或事業單位的人事部門經常要把本單位職工的基本狀況(職工號、姓名、年齡、性別、籍貫、工資、簡歷等)存放在表中,這張表就能夠當作是一個數據庫。有了這個「數據倉庫「咱們就能夠根據須要隨時查詢某職工的基本狀況,也能夠查詢工資在某個範圍內的職工人數等等。這些工做若是都能在計算機上自動進行,那咱們的人事管理就能夠達到極高的水平。此外,在財務管理、倉庫管理、生產管理中也須要創建衆多的這種「數據庫「,使其能夠利用計算機實現財務、倉庫、生產的自動化管理。 嚴格來講,數據庫是長期儲存在計算機內、有組織的、可共享的數據集合。數據庫中的數據指的是以必定的數據模型組織、描述和儲存在一塊兒、具備儘量小的冗餘度、較高的數據獨立性和易擴展性的特色並可在必定範圍內爲多個用戶共享。 這種數據集合具備以下特色:儘量不重複,以最優方式爲某個特定組織的多種應用服務,其數據結構獨立於使用它的應用程序,對數據的增、刪、改、查由統一軟件進行管理和控制。從發展的歷史看,數據庫是數據管理的高級階段,它是由文件管理系統發展起來的。
層次模型:用樹狀<層次>結構來表示實體類型和實體間聯繫的數據模型。mysql
網狀模型:用有向圖表示實體和實體之間的聯繫的數據結構模型稱爲網狀數據模型。linux
關係模型:使用表格表示實體和實體之間關係的數據模型稱之爲關係數據模型。git
可以用數據或統一的結構加以表示,咱們稱之爲結構化數據,如數字、符號。傳統的關係數據模型、行數據,存儲於數據庫,可用二維表結構表示。redis
介於徹底結構化數據(如關係型數據庫、面向對象數據庫中的數據)和徹底無結構的數據(如聲音、圖像文件等)之間的數據,XML、HTML文檔就屬於半結構化數據。它通常是自描述的,數據的結構和內容混在一塊兒,沒有明顯的區分。sql
指其字段長度可變,而且每一個字段的記錄又能夠由可重複或不可重複的子字段構成的數據庫,用它不只能夠處理結構化數據(如數字、符號等信息)並且更適合處理非結構化數據(全文文本、圖象、聲音、影視、超媒體等信息)。非結構化數據,包括全部格式的辦公文檔、文本、圖片、XML、HTML、各種報表、圖像和音頻/視頻信息等等數據庫
數據庫:一個方案、一個項目編程
二維關係:表:row, column 索引:index 視圖:view數據結構
結構化查詢語言;簡稱SQL,是一種特殊目的的編程語言,是一種數據庫查詢和程序設計語言,用於存取數據以及查詢、更新和管理關係數據庫系統;同時也是數據庫腳本文件的擴展名。
其語句,也稱爲「數據檢索 語句」,用以從表中得到數據,肯定數據怎樣在應用程序給出。保留字 SELECT是DQL(也是全部SQL)用得最多的動詞,其餘DQL經常使用的保留字有WHERE,ORDER BY,GROUP BY和HAVING。這些DQL保留字常與其餘類型的SQL語句一塊兒使用。
其語句包括動詞 INSERT, UPDATE和 DELETE。它們分別用於添加,修改和刪除表中的行。也稱爲動做查詢語言。
它的語句能確保被DML語句影響的表的全部行及時得以更新。TPL語句包括BEGIN TRANSACTION,COMMIT和ROLLBACK
它的語句經過GRANT或REVOKE得到許可,肯定單個用戶和用戶組對數據庫對象的訪問。某些RDBMS可用GRANT或REVOKE控制對錶單個列的訪問。
其語句包括動詞CREATE和DROP。在數據庫中建立新表或刪除表(CREAT TABLE 或 DROP TABLE);爲表加入索引等。DDL包括許多與人數據庫目錄中得到數據有關的保留字。它也是動做查詢的一部分。
DDL:Data Defined Language(數據定義) CREATE, ALTER, DROP、SHOW (增刪改查) DML: Data Manapulating Language(數據操做) INSERT, DELETE, UPDATE, SELECT (增刪改查)
存儲過程:procedure 存儲函數:function 觸發器:trigger 事件調度器:event scheduler 例程:routine
CREATE建 ALTER改 DROP刪oracle
mysql> SHOW DATABASES; #查看MySQL中的數據庫 +--------------------+ | Database | +--------------------+ | information_schema| | mysql | | test | +--------------------+ 3 rows in set (0.00 sec) mysql> CREATE DATABASE Oracle; #建立數據庫Oracle Query OK, 1 row affected (0.00 sec) mysql> SHOW DATABASES; #查看是否建立成功 +--------------------+ | Database | +--------------------+ | information_schema | | Oracle | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec)
mysql> SELECT DATABASE(); #查看當前所在數據庫位置DATABASE()爲MySQL內置函數 +------------+ | DATABASE() | +------------+ | NULL | +------------+ 1 row in set (0.00 sec) mysql> USE Oracle #切換到咱們以前建立的Oracle數據庫中 Database changed mysql> SELECT DATABASE(); #查看是否切換到Oracle +------------+ | DATABASE() | +------------+ | Oracle | +------------+ 1 row in set (0.00 sec) mysql> CREATE table BranchTab( #建立表 -> Id INT, -> Name CHAR(30) -> ); Query OK, 0 rows affected (0.09 sec) mysql> SHOW TABLES; #查看BranchTab表是否建立成功 +------------------+ | Tables_in_Oracle| +------------------+ | BranchTab | +------------------+ 1 row in set (0.00 sec)
mysql> SELECT DATABASE(); #查看當前所在數據庫爲準 +------------+ | DATABASE() | +------------+ | Oracle | +------------+ 1 row in set (0.00 sec) mysql> SHOW TABLES; #查看當前所在數據庫位置中的表 +------------------+ | Tables_in_Oracle | +------------------+ | BranchTab | +------------------+ 1 row in set (0.00 sec) mysql> ALTER TABLE BranchTab RENAME branchtab; #修改表BranchTab爲branchtab Query OK, 0 rows affected (0.00 sec) mysql> SHOW TABLES; #查看是否修改爲功 +------------------+ | Tables_in_Oracle | +------------------+ | brannhtab | +------------------+ 1 row in set (0.00 sec)
mysql> SELECT DATABASE(); #查看當前所在數據庫位置 +------------+ | DATABASE() | +------------+ | Oracle | +------------+ 1 row in set (0.00 sec) mysql> SHOW TABLES; #查看當前所在數據庫位置中的表 +------------------+ | Tables_in_Oracle | +------------------+ | branchtab | +------------------+ 1 row in set (0.00 sec) mysql> DROP TABLE bracnhtab; #DROP掉branchtab表 Query OK, 0 rows affected (0.00 sec) mysql> SHOW TABLES; #查看branchtabs是否被刪除 Empty set (0.00 sec)
mysql> SHOW DATABASES; #查看MySQL中的全部庫,發現Oracle庫 +--------------------+ | Database | +--------------------+ | information_schema | | Oracle | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) mysql> DROP DATABASE Oracle; #DROP掉Oracle數據庫 Query OK, 0 rows affected (0.00 sec) mysql> SHOW DATABASES; #查看Oracke是否被刪 +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (0.00 sec)
INSERT增 DELETE刪 SELECT查 UPDATE改編程語言
mysql> CREATE DATABASE oracle; #建立oracle數據庫 Query OK, 1 row affected (0.00 sec) mysql> use oracle #切換到oracle數據庫 Database changed mysql> CREATE TABLE branch( -> Id INT, -> Name CHAR(30) -> ); Query OK, 0 rows affected (0.16 sec) mysql> DESC branch; #查看錶結構,簡要增長數據最好看下別弄錯 +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | Id | int(11) | YES | | NULL | | | Name | char(30) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> SELECT * FROM branch; #查看錶結構明細 Empty set (0.00 sec) mysql> SHOW CREATE TABLE branch\G *************************** 1. row *************************** Table: branch Create Table: CREATE TABLE `branch` ( `Id` int(11) DEFAULT NULL, `Name` char(30) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 #能夠看出咱們使用的是MyISMA 1 row in set (0.00 sec) mysql> SELECT @@version; +-----------+ | @@version | +-----------+ | 5.1.73 | +-----------+ 1 row in set (0.00 sec)
mysql> SELECT DATABASE(); #查看本身所在數據庫位置是否正確 +------------+ | DATABASE() | +------------+ | oracle | +------------+ 1 row in set (0.00 sec) mysql> DESC branch; #查看錶結構 +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | Id | int(11) | YES | | NULL | | | Name | char(30) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> INSERT INTO branch VALUES #插入數據到branch表中 -> (1,'Tom'), -> (2,'Sunshine'); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM branch; #查看是否插入成功 +------+----------+ | Id | Name | +------+----------+ | 1 | Tom | | 2 | Sunshine | +------+----------+ 2 rows in set (0.00 sec)
mysql> SELECT DATABASE(); #查看所在數據庫位置 +------------+ | DATABASE() | +------------+ | oracle | +------------+ 1 row in set (0.00 sec) mysql> DESC branch; #查看branch表結構 +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | Id | int(11) | YES | | NULL | | | Name | char(30) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> DELETE FROM branch; #刪除表數據,沒加WHERE條件就是刪除這張表裏面的全部內容 Query OK, 2 rows affected (0.00 sec) mysql> SELECT * FROM branch; #查看是否刪除成功 Empty set (0.00 sec) mysql> INSERT INTO branch VALUES #插入新的數據 -> (1,'Alis'), -> (2,'jeery'); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM branch; #查看是否插入成功 +------+-------+ | Id | Name | +------+-------+ | 1 | Alis | | 2 | jeery | +------+-------+ 2 rows in set (0.00 sec) mysql> DELETE FROM branch WHERE Id=1; #刪除branch表裏面的內容加了條件判斷WHERE Id=1 Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM branch; #查看是否刪除咱們指定的數據 +------+-------+ | Id | Name | +------+-------+ | 2 | jeery | +------+-------+ 1 row in set (0.00 sec) mysql> DELETE FROM branch WHERE Name=jeery; #刪除branch表裏面的內容加了條件判斷 WHERE Name=jeery;可是jeery沒加單引號報錯 ERROR 1054 (42S22): Unknown column 'jeery' in 'where clause' mysql> DELETE FROM branch WHERE Name='jeery'; #刪除branch表裏面的內容加了條件判斷 WHERE Name='jeery';加了單引號成功 Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM branch; #查看是否刪除咱們指定你的數據 Empty set (0.00 sec)
mysql> DESC branch; #查看錶結構 +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | Id | int(11) | YES | | NULL | | | Name | char(30) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> INSERT INTO branch VALUES #插入一些數據 -> (1,'Sunshine'), -> (2,'jeery'), -> (3,'Alis'), -> (4,'Tom'); Query OK, 4 rows affected (0.00 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM branch; #查看branch表中的數據 +------+----------+ | Id | Name | +------+----------+ | 1 | Sunshine | | 2 | jeery | | 3 | Alis | | 4 | Tom | +------+----------+ 4 rows in set (0.00 sec) mysql> SELECT * FROM branch WHERE Id=1; #查看branch表中的數據,以條件 "WHERRE Id=1" +------+----------+ | Id | Name | +------+----------+ | 1 | Sunshine | +------+----------+ 1 row in set (0.00 sec) mysql> SELECT Name FROM branch; #查看branch表中Name字段的數據 +----------+ | Name | +----------+ | Sunshine | | jeery | | Alis | | Tom | +----------+ 4 rows in set (0.00 sec) mysql> SELECT Name FROM branch WHERE Id=1; #查看branch表中Name字段的數據,以條件 "WHERRE Id=1" +----------+ | Name | +----------+ | Sunshine | +----------+ 1 row in set (0.00 sec) mysql> SELECT count(*) FROM branch; #使用count內置函數查看branch表中有多少行 +----------+ | count(*) | +----------+ | 4 | +----------+ 1 row in set (0.00 sec) mysql> SELECT count(*) FROM bransh where Id=1; #使用count內置函數查看branch表中有多少行,以條件 "WHERE Id=1" ERROR 1146 (42S02): Table 'oracle.bransh' doesn't exist mysql> SELECT count(*) FROM bransh; ERROR 1146 (42S02): Table 'oracle.bransh' doesn't exist mysql> SELECT count(*) FROM branch WHERE Id=1; +----------+ | count(*) | +----------+ | 1 | +----------+ 1 row in set (0.00 sec)
mysql> DESC branch; #查看錶結構 +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | Id | int(11) | YES | | NULL | | | Name | char(30) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> UPDATE branch SET Id=5; #更改數據,Id=5,生產環境中最好加條件,否則就呵呵了~ Query OK, 4 rows affected (0.00 sec) Rows matched: 4 Changed: 4 Warnings: 0 mysql> SELECT * FROM branch; #不加條件就變成這樣了,不是咱們想要的 +------+----------+ | Id | Name | +------+----------+ | 5 | Sunshine | | 5 | jeery | | 5 | Alis | | 5 | Tom | +------+----------+ 4 rows in set (0.00 sec) mysql> UPDATE branch SET Id=1 WHERE Name='Sunshine'; #更改數據Id=1,加了條件 "WHERE Name='Sunshine'" Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> SELECT * FROM branch; #查看是不是更改爲咱們所想要的 +------+----------+ | Id | Name | +------+----------+ | 1 | Sunshine | | 5 | jeery | | 5 | Alis | | 5 | Tom | +------+----------+ 4 rows in set (0.00 sec)
GRANT REVOKE
權限級別:管理權限,數據庫,表,字段,存儲例程
mysql> CREATE TABLE branchone( #爲了區別,咱們這裏在建立一個表 -> Id INT, -> Name CHAR(30) -> ); Query OK, 0 rows affected (0.06 sec) mysql> SHOW TABLES; #查看oracle庫有幾張表 +------------------+ | Tables_in_oracle | +------------------+ | branch | | branchone | +------------------+ 2 rows in set (0.00 sec) mysql> GRANT SELECT ON oracle.branch TO 'sunshine'@'192.168.11.28' IDENTIFIED BY 'sunshine'; #受權sunshine用戶只能經過192.168.11.28這個IP訪問數據庫,並且只有oracle數據庫branch的查看權限 Query OK, 0 rows affected (0.00 sec) mysql> SHOW GRANTS FOR 'sunshine'@'192.168.11.28'; #查看是否受權成功,咱們看到GRANT SELECT ON `oracle`.`branch` TO 'sunshine'@'192.168.11.28' +---------------------------------------------------------------------------------------------------------------------+ | Grants for sunshine@192.168.11.28 | +---------------------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'sunshine'@'192.168.11.28' IDENTIFIED BY PASSWORD '*D6B63C1953E7F096DB307F8AC48C4AD703E57001' | | GRANT SELECT ON `oracle`.`branch` TO 'sunshine'@'192.168.11.28' | +---------------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec) [root@redis_master ~]# ifconfig | grep "inet addr:192.168" #使用Linux系統,查看本機IP,爲192.168.11.28 inet addr:192.168.11.28 Bcast:192.168.11.255 Mask:255.255.255.0 [root@redis_master ~]# mysql -h192.168.11.28 -usunshine -psunshine #使用sunshine用戶鏈接數據庫 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> mysql> SHOW DATABASES; #查看數據庫 +--------------------+ | Database | +--------------------+ | information_schema | | oracle | | test | +--------------------+ 3 rows in set (0.00 sec) mysql> USE oracle #進入oracle數據庫 Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> SHOW TABLES; #查看本身是否只能看到咱們設定branch表 +------------------+ | Tables_in_oracle | +------------------+ | branch | +------------------+ 1 row in set (0.00 sec) mysql> DESC branch; #查看錶結構 +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | Id | int(11) | YES | | NULL | | | Name | char(30) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> INSERT INTO branch VALUES #插入數據,提示權限拒絕command denied -> (10,'Test'); ERROR 1142 (42000): INSERT command denied to user 'sunshine'@'gitlab.jinr.com' for table 'branch' mysql> DELETE FROM branch; #刪除數據,提示權限拒絕 command denied ERROR 1142 (42000): DELETE command denied to user 'sunshine'@'gitlab.jinr.com' for table 'branch' mysql> UPDATE branch SET Id=1; #更改數據,提示權限拒絕 command denied ERROR 1142 (42000): UPDATE command denied to user 'sunshine'@'gitlab.jinr.com' for table 'branch' mysql> SELECT * FROM branch; #查看數據,正常 +------+----------+ | Id | Name | +------+----------+ | 1 | Sunshine | | 5 | jeery | | 5 | Alis | | 5 | Tom | +------+----------+ 4 rows in set (0.00 sec) [root@jroa ~]# ifconfig | grep "inet addr:192.168" #使用另一臺Linux系統,查看IP,爲192.168.11.21 inet addr:192.168.11.21 Bcast:192.168.11.255 Mask:255.255.255.0 [root@jroa ~]# mysql -h192.168.11.28 -usunshine -psunshine #嘗試鏈接,提示需'192.168.11.28' (113) 才能登入 ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.11.28' (113)
mysql> SHOW GRANTS FOR 'sunshine'@'192.168.11.28'; #查看權限,發現 GRANT SELECT ON `oracle`.`branch` TO 'sunshine'@'192.168.11.28' +---------------------------------------------------------------------------------------------------------------------+ | Grants for sunshine@192.168.11.28 | +---------------------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'sunshine'@'192.168.11.28' IDENTIFIED BY PASSWORD '*D6B63C1953E7F096DB307F8AC48C4AD703E57001' | | GRANT SELECT ON `oracle`.`branch` TO 'sunshine'@'192.168.11.28' | +---------------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec) mysql> REVOKE SELECT ON oracle.branch FROM 'sunshine'@'192.168.11.28'; #收回受權 Query OK, 0 rows affected (0.00 sec) mysql> SHOW GRANTS FOR 'sunshine'@'192.168.11.28'; #查看權限,沒發現 GRANT SELECT ON `oracle`.`branch` TO 'sunshine'@'192.168.11.28' +---------------------------------------------------------------------------------------------------------------------+ | Grants for sunshine@192.168.11.28 | +---------------------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'sunshine'@'192.168.11.28' IDENTIFIED BY PASSWORD '*D6B63C1953E7F096DB307F8AC48C4AD703E57001' | +---------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) [root@redis_master ~]# !if #查看本機IP,爲192.168.11.28 ifconfig | grep "inet addr:192.168" inet addr:192.168.11.28 Bcast:192.168.11.255 Mask:255.255.255.0 [root@redis_master ~]# !mys #鏈接mysql,由於第一次受權了,就算收回,公共庫的權限仍是有的 mysql -h192.168.11.28 -usunshine -psunshine Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; #查看數據庫,發現oracle數據不見啦 +--------------------+ | Database | +--------------------+ | information_schema | | test | +--------------------+ 2 rows in set (0.00 sec)