![](https://img.51dongshi.com/20250105/wz/18528826952.jpg)
Postgresql不允許將所有權限賦給匹配到表一級的所有數據庫對象,需要為每一個新建的表授予權限。?使用數據表創建數據表在創建新表前確保用管理員用戶賬戶(MysqL中的root用戶,Postgresql中的postgres用戶)登錄來創建表MysqL和Postgresql的數據類型數據類型描述char定長字符串值varchar變長字符串值int整數值float浮點值Boolean布爾類型true/false值DateYYYY-MM-DD格式的日期值TimeHH:mm:ss格式的時間值Timestamp日期和時間值的組合Text長字符串值BLOB大的二進制值使用CREATETABLE建立表[plain]?test=#CREATETABLEemployees(?test(#empidintnotnull,?test(#lastnamevarchar(30),?test(#firstnamevarchar(30),?test(#salaryfloat,?test(#primarykey(empid));?NOTICE:?CREATETABLE/PRIMARYKEYwillcreateimplicitindex"employees_pkey"fortable"employees"?CREATETABLE???test=#dt???????Listofrelations??Schema|?Name??|Type?|?Owner???--------+-----------+-------+----------??public|employees|table|postgres?(1row)?在psql中還需要在表一級分配權限。[plain]?test=#GRANTSELECT,INSERT,DELETE,UPDATEONpublic.employeesTOsu;?GRANT?以postgres登錄角色來執行,并連接到test數據庫,且必須指定模式名。?插入和刪除數據關于sql部分,這里不做詳細筆記。[plain]?MysqL>CREATETABLEemployees(???->empidintnotnull,???->lastnamevarchar(30),???->firstnamevarchar(30),???->salaryfloat,???->primarykey(empid));?QueryOK,0rowsaffected(0.08sec)???MysqL>INSERTINTOemployeesVALUES(1,'Blum','Rich',1234.5);?QueryOK,1rowaffected(0.03sec)?查詢數據[plain]?MysqL>SELECT*FROMemployees;?+-------+----------+-----------+--------+?|empid|lastname|firstname|salary|?+-------+----------+-----------+--------+?|??1|Blum??|Rich???|1234.5|?+-------+----------+-----------+--------+?1rowinset(0.00sec)??在腳本中使用數據庫連接到數據庫對于psql:[plain]?$catpsql_connection?#!/bin/bash?psql=`whichpsql`?sudo-upostgres$psql??$psql_connection?Couldnotchangedirectoryto"/home/su1216/android/source/linux_learned"?psql(8.4.17)?Type"help"forhelp.???postgres=#??對于MysqL:[plain]?$catMysqL_connection?#!/bin/bash?MysqL=`whichMysqL`?$MysqL"test"-u"test"-p?$MysqL_connection?Enterpassword:??Readingtableinformationforcompletionoftableandcolumnnames?Youcanturnoffthisfeaturetogetaquickerstartupwith-A???WelcometotheMysqLmonitor.?Commandsendwith;org.?YourMysqLconnectionidis38?Serverversion:5.1.72-0ubuntu0.10.04.1(Ubuntu)???copyright(c)2000,2013,Oracleand/oritsaffiliates.Allrightsreserved.???OracleisaregisteredTrademarkofOracleCorporationand/orits?affiliates.OthernamesmaybeTrademarksoftheirrespective?owners.???Type'help;'or'h'forhelp.Type'c'toclearthecurrentinputstatement.???MysqL>??執行腳本時,MysqL會停下來要求用戶輸入密碼,這個問題可以避免。下面是一種糟糕的方式,直接將密碼放在腳本中明文顯示:[plain]?$catMysqL_connection?#!/bin/bash?MysqL=`whichMysqL`?$MysqL"test"-u"test"-ptest?-p與密碼緊密相連。另一種解決方案:MysqL使用$HOME/.my.cnf文件來讀取特殊的啟動命令和設置。如果沒有這個文件,我們自己建立一個即可[plain]?$touch/home/su1216/.my.cnf?$gedit/home/su1216/.my.cnf?$chmod400/home/su1216/.my.cnf?.my.cnf內容如下[plain]?$cat/home/su1216/.my.cnf?[client]?password=test?現在再執行MysqL_connection就不會要求輸入密碼了向服務器發送命令1.發送一個命令并退出2.發送多個命令對于MysqL,可以使用-e選項:[plain]?$catMysqL_test?#!/bin/bash?MysqL=`whichMysqL`?$MysqL"test"-u"test"-ptest-e"select*fromemployees"?輸出結果為:[plain]?$MysqL_test?+-------+----------+-----------+--------+?|empid|lastname|firstname|salary|?+-------+----------+-----------+--------+?|??1|Blum??|Rich???|1234.5|?+-------+----------+-----------+--------+?對于psql,可以使用-c選項發送多條命令可以使用重定向,注意:最后的EOF所在行不能有其他字符串。[plain]?$catMysqL_test?#!/bin/bash?MysqL=`whichMysqL`?$MysqL"test"-u"test"-ptest<
empid | lastname | firstname | salary |
---|
1 | Blum | Rich | 1234.5 |
2 | Blum | Poor | 321.099 |
?MysqL還可以以XML格式顯示結果[plain]?$MysqL"test"-u"test"-Xe'select*fromemployees'?????????1???Blum???Rich???1234.5??
???????2???Blum???Poor???321.099??
??本文來自系統大全為您提供如需轉載請注明!推薦win10下載