国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

Linux數據庫大比拚(二)

2024-07-21 02:12:51
字體:
來源:轉載
供稿:網友
菜鳥學堂:
為了測試并比較3個數據庫管理系統,我當然需要一個數據庫來管理他們。在讀完了《sql傻瓜書》后,我有了一些如何設計現實的數據庫的基本知識,因此我揀選了一特別無聊的真實世界情形(一個因特網書店,其他?)并且寫下了一個小型數據庫設計以便探討一點sql的能力。
  
  在這個數據庫里有5個表,book保存可得到的書的登記信息;customer包含登記的顧客。對每份訂單,bookorder創建一行,參照其顧客。對每個定購的項目,在order_position里引用它的訂單號。作為一種獎勵,針對書的排名我增加了一rating表。
  
  下列的sql代碼可裝入到一個數據庫系統的sql監控程序并且被接受應該沒有任何問題。所有的表在創建前被刪除因此確保他們不會在創建之前已經存在。
  
   
  
  drop table book;
  create table book (
  article_no integer primary key,
  author_first_names character(30),
  author_last_names character(30),
  title character(30),
  isbn character(13) unique,
  wholesale_price numeric(4,2),
  retail_price numeric(4,2),
  copies_available integer
  );
  drop table customer;
  create table customer (
  customer_no integer primary key,
  first_names character(30),
  last_names character(30),
  street character(30),
  house_no smallint,
  postcode character(7),
  town character(30),
  iso_country_code character(2)
  );
  drop table bookorder;
  create table bookorder (
  order_no integer primary key,
  customer_no integer not null,
  ordered date,
delivery date,
  status character(8)
  check (status in (′accepted′,
  ′delayed′,
  ′shipped′,
  ′returned′,
  ′paid′)),
  constraint customer_fk foreign key (customer_no)
  references kunde (kundenname)
  );
  drop table order_position;
  create table order_position (
  position_no integer primary key,
  order_no integer not null,
  article_no integer not null,
  number smallint,
  constraint order_fk foreign key (order_no)
  references bookorder (order_no),
  constraint book_fk foreign key (article_no)
  references book (article_no)
  );
  drop table rating;
  create table rating (
  rating_no integer primary key,
  article_no integer not null,
  score numeric(1,0),
  comment character varying(300),
  constraint book_fk foreign key (article_no)
  references book (article_no)
  );
  當然,這是一個極其簡單的數據庫。它看上去真實,但是它不適用于真實世界的應。它不保存顧客記錄或任何東西,并且它甚至沒有書的出版商的列。它只是一個測試環境。
  
  注意我不想花大氣力強制customer.iso_country_code為今天是實際有效的編碼。我在代碼做了一點限制以測試數據庫系統是否接受他們;我沒嘗試使數據庫無懈可擊。
  
  改變設計適應postgresql
  當我將遵循ansi標準的create table語句裝入postgresql的psql監控視程序是,我遇到的困難是很少的。我得到一些警告:外部關鍵字限制被接受但還沒有實現,而且我不得不裁減rating的comment字段到255個字符,因為這是postgresql的character varying類型的字段的最大字段寬度。系統為存儲大量數據提供blob數據類型,但是它們不在標準版本內,因此我決定了不使用他們。另外的問題是相當愚蠢--因為我不能找到有關postgresql如何強制numeric到c數據類型,也因為我不想使用float以避免舍入,我決定使得貨幣字段為分值(cent)的整數數字。
  我最后得到了這個略有不同的腳本:
  
  drop table book;
  create table book (
  article_no integer primary key,
  author_first_names character(30),
  author_last_names character(30),
  title character(30),
  isbn character(13) unique,
  wholesale_price integer,
  retail_price integer,
  copies_available integer
  );
  drop table customer;
  create table customer (
  customer_no integer primary key,
  first_names character(30),
  last_names character(30),
  street character(30),
  house_no smallint,
  postcode character(7),
  town character(30),
  iso_country_code character(2)
  );
  drop table bookorder;
  create table bookorder (
  order_no integer primary key,
  customer_no integer not null,
  ordered date,
  delivery date,
  status character(8)
  check (status in (′accepted′,
  ′delayed′,
  ′shipped′,
  ′returned′,
  ′paid′)),
  constraint customer_fk foreign key (customer_no)
  references kunde (kundenname)
  );
  drop table order_position; 
  create table order_position (
  position_no integer primary key,
  order_no integer not null,
  article_no integer not null,
  number smallint,
  constraint order_fk foreign key (order_no)
  references bookorder (order_no),
  constraint book_fk foreign key (article_no)
  references book (article_no)
  );
  drop table rating;
  create table rating (
  rating_no integer primary key,
  article_no integer not null,
  score smallint,
  comment character varying(255),
  constraint book_fk foreign key (article_no)
  references book (article_no)
  );
   
  
  使設計適應mysql
  mysql象postgresql一樣忽略外部關鍵字的限制,但是它搞了個unique限制。最后的腳本與postgresql腳本差不多:
  
  drop table book;
  create table book (
  article_no integer primary key,
  author_first_names character(30),
  author_last_names character(30),
  title character(30),
  isbn character(13),
  wholesale_price integer,
  retail_price integer,
  copies_available integer
  );
  drop table customer;
  create table customer (
  customer_no integer primary key, 
  first_names character(30),
  last_names character(30),
  street character(30),
  house_no smallint,
  postcode character(7),
  town character(30),
  iso_country_code character(2)
  );
  drop table bookorder;
  create table bookorder (
  order_no integer primary key,
  customer_no integer not null,
  ordered date,
  delivery date,
  status character(8),
  constraint customer_fk foreign key (customer_no)
  references kunde (kundenname)
  );
  drop table order_position;
  create table order_position (
  position_no integer primary key,
  order_no integer not null,
  article_no integer not null,
  number smallint,
  constraint order_fk foreign key (order_no)
  references bookorder (order_no),
  constraint book_fk foreign key (article_no)
  references book (article_no)
  );
  drop table rating;
  create table rating (
  rating_no integer primary key,
  article_no integer not null,
  score numeric(1,0),
  comment character varying(255),
  constraint book_fk foreign key (article_no)
  references book (article_no) 
  );
  使設計適應 msql
  因為msql是一個精簡的數據庫管理器(的確,有些人可能懷疑mysql和msql是否是數據庫管理系統),它放棄了大多數sql的功能而僅僅接受sql的一個嚴格限制的子集。這樣,msql的腳本看上有很大不同:
  
  drop table book
  create table book (
  article_no integer not null,
  author_first_names character(30),
  author_last_names character(30),
  title character(30),
  isbn character(13),
  wholesale_price money,
  retail_price money,
  copies_available integer
  )
  drop table customer
  create table customer (
  customer_no integer not null,
  first_names character(30),
  last_names character(30),
  street character(30),
  house_no smallint,
  postcode character(7),
  town character(30),
  iso_country_code character(2)
  )
  drop table bookorder
  create table bookorder (
  order_no integer not null,
  customer_no integer not null,
  ordered date,
  delivery date,
  status character(1)
  )
  drop table order_position
  create table order_position (
  position_no integer not null,
  order_no integer not null,
  article_no integer not null, 
 number smallint
  )
  drop table rating
  create table rating (
  rating_no integer not null,
  article_no integer not null,
  score smallint,
  comment text(255)
  )
  幾乎所有的約束都不見了,并且numeric和character varying分別由money和text代替。
  
  在msql的監視程序中有令人沮喪的不足:它似乎不能接受從標準輸入輸入sql腳本,這樣, 需要剪切/粘貼代碼。msql也討厭分號;最終我只能一個一個地輸入命令并用/g(“go”斜杠命令)終止每條命令 。
  
  實現測試客戶
  為了比較3個數據庫管理器,我決定為執行在bookstore數據庫上的交易的目的用c寫了一個測試客戶。結果,我實現了一些操作,它們能比較api。為了性能比較,我隨后充分實現了它們,并且把一個非交互式模式加入客戶程序,因此它能自己運行,產生隨意的數據且隨機執行交易。
  
  我決定了在樣品數據庫上實現下列行動:
  
  增加一本新書: insert into book (...) values (...);
  刪除一本存在的書: delete from book where article_no=...;
  增加一個顧客: insert into customer (...) values (...);
  刪除一個顧客: delete from customer where customer_no=...;
  訂書的一個顧客: insert into bookorder (...) values (...); insert into order_position (...) values (...);;
  評估一本書的一個顧客: insert into rating (...) values (...);
  改變一份訂單的狀態: update bookorder set status=... where order_no=...;
  然后,能生成下列報表:
  
  書籍列表: select * from book;
  顧客列表: select * from customer;
  正在投遞的交貨表,按狀態排序: select * from bookorder order by status;
  書籍的利潤額,最后有平均值: select retail_price-wholesale_price from book; select avg(retail_price-wholesale_price) from book; 
  書評、評級和為一本書的平均評級: select * from rating where article_no=...; select avg(score) from rating where article_no=...;
  將客戶帶入postgresql的生活
  關于用c編程postgresql的好處是你能使用嵌入式sql。(而且,至少我喜歡它)它不是很好地文檔化,但是esql預處理器ecpg運行并能產生postgresql接口代碼就好。sql 的定式思維方法有時妨礙了我;但是,開發客戶程序并不是很難的。
  
  我說過“不是很好地文檔化”嗎?那是一個保守說法。否則postgresql完整的html 文檔在這方面非常缺乏。我從書本得到的esql知識是初級的,而且聯機文檔沒幫助太多,因此我不得不自己了解如何由ecpg將c的變量強制轉換為numeric值--還有其他東西,而且,esql預處理器不是很詳細,且無論何時它碰到任何小錯誤,總是似乎完全釋放出來,這對任何從事又長期準備的項目的人來說將是一個持久戰。
  
  在編程postgresql的客戶程序時,我碰到了一些小錯誤。例如,如果文檔記錄是可能的話,在聲明一個光標(cursor)時,ecpg將不接受一個 for read only子句 。order by子句甚至沒被實現。我遇見的問題大都ecpg預處理器有關。postgres有一個 c api(不管怎么說,esql需要被編譯進一些東西),它可能是優秀的,但是我沒使用它(這就是生活)。當有esql時,我準備使用esql。
  
  這是摘自postgres-client.pgc的list_books()函數:
  
  void list_books(void)
  {
  exec sql begin declare section;
  int article_no;
  char author_first_names[30];
  char author_last_names[30];
  char title[30];
  char isbn[14];
  int wholesale_price;
  int retail_price;
  int copies_available;
  exec sql end declare section;
  exec sql declare book_cursor cursor for
  select article_no, author_first_names, author_last_names,
  title, isbn, wholesale_price, retail_price,
  copies_available from book; 
 exec sql open book_cursor;
  while (1)
  {
  exec sql fetch next from book_cursor
  into :article_no, :author_first_names, :author_last_names,
  :title, :isbn, :wholesale_price, :retail_price,
  :copies_available;
  if (sqlca.sqlcode == 100) /* 100 == not found */
  break; /* bail out */
  printf("/narticle no. %d/n", article_no);
  printf("%s, %s:/n", author_last_names, author_first_names);
  printf(" %s (%s)/n", title, isbn);
  printf("bought at %d; selling at %d; %d copies available/n/n",
  wholesale_price, retail_price, copies_available);
  };
  exec sql close book_cursor;
  }
  代碼是相當直觀。它聲明一些宿主變量,在一個begin/end declare section構造中包裝聲明,打開一個select光標查詢,并且然后一行一行地取到宿主變量中,光標然后關閉。
  
  我使用了更舊的, 遭到一致反對的sqlcode變量而不是更現代的sqlstate,因為這種方式更容易檢查一個not found情形。
  
  把客戶帶入mysql的生活
  mysql的c api是相當易用的。核心元素是包含有關數據庫連接的信息和其狀態的結構,它通過連接mysql服務器進行初始化。該結構的一根指針必須被傳遞給所有的 mysql 客戶函數。
  
  查詢以字符串提交;這意味著一個人必須處理 c 字符串變換功能,包含空字節(/0) 的數據應該能使用,情況變得更復雜了,因為隨后傳遞了一個計數字符串而不是一個 c字符串。
  
  為了獲取查詢結果,一個指向mysql_res結構的指針和一個數值變量用適當的 api 函數初始化,然后將一行取進一個mysql_row變量,它是一個字符串數組,直接將結果放進整數變量,就像postgresql的esql的實現能做的那樣,但這是不可能的,結果緩沖區隨后被釋放。只要你能理解,語義幾乎與在esql使用光標相同。 
  list_books(void)
  {
  int count;
  mysql_res *result;
  mysql_query(&bookstore, "select article_no, author_first_names,author_last_names, title, isbn, wholesale_price, retail_price,copies_available from book");
  result = mysql_store_result(&bookstore);
  for(count = mysql_num_rows(result); count > 0; count--)
  {
  mysql_row record;
  record = mysql_fetch_row(result);
  printf("/narticle no. %s/n", record[0]);
  printf("%s, %s:/n", record[2], record[1]);
  printf(" %s (%s)/n", record[3], record[4]);
  printf("bought at %s; selling at %s; %s copies available/n/n",
  record[5], record[6], record[7]);
  };
  mysql_free_result(result);
  }
  mysql_free_result ( 結果 );
  }
  
  api函數簡明扼要,但足夠了, texinfo格式的文檔作為mysql文檔的主要來源。
  
  把客戶帶入msql的
  msql和mysql c api 之間的差別非常非常小。這樣, 甚至可能有一個自動變換器。主要的差別是:
  
  msql 不存儲連接數據塊, 僅存一個數字(int bookstore)
  一些 msql 函數不拿連接作為一個參數
  msql 函數名字是pascal風格(他們使用大寫首字符而不是下劃線)
  方便的money數據類型是一個有2個的十進制位的固定精度小數類型。為了使msql正確地在money列中將分幣(cent)存入整數數字里,我需要轉換他們,強制到float,分離他們并且在add_new_book()函數中的sprintf語句格式化他們。 
 這是list_books(), 移植到 msql :
  
   
  
  void
  list_books(void)
  {
  int count;
  m_result *result;
  msqlquery(bookstore, "select article_no, author_first_names,author_last_names, title, isbn, wholesale_price, retail_price,copies_available from book");
  result = msqlstoreresult();
  for(count = msqlnumrows(result); count > 0; count--)
  {
  m_row record;
  record = msqlfetchrow(result);
  printf("/narticle no. %s/n", record[0]);
  printf("%s, %s:/n", record[2], record[1]);
  printf(" %s (%s)/n", record[3], record[4]);
  printf("bought at %s; selling at %s; %s copies available/n/n",
  record[5], record[6], record[7]);
  };
  msqlfreeresult(result);
  }
   
  
  msql的 c api文檔可以在msql 手冊里找到,它以postscript和一個大的html文件與msql一起發行。
  
  一些早期結論
  所有這3個討論的數據庫系統是相當容易安裝、設置和編程。實現c api的客戶庫是很小的;與現今的比如gui工具箱,他們的大小是可以忽略的,并且在客戶程序的二進制大小或存儲器足跡(footprint)沒有太大的差別。
  
  postgresql的esql api的不斷增加的冗長和更長的準備時間通過少花些精力在轉換字符串到非字符串后反過來進行彌補。
  
  到目前為止, 我沒有說過任何關于性能的事情。我將在這個系列的下一部分做深入研究。 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 肥西县| 井陉县| 灵寿县| 鄂伦春自治旗| 通化县| 开远市| 南皮县| 永靖县| 土默特左旗| 永年县| 吉安市| 商丘市| 仁怀市| 游戏| 太仓市| 土默特左旗| 北票市| 定边县| 象山县| 柯坪县| 图片| 常山县| 宣汉县| 楚雄市| 白银市| 安徽省| 丰都县| 伊吾县| 漠河县| 永修县| 南城县| 池州市| 鹤岗市| 根河市| 永登县| 海安县| 泰顺县| 南宁市| 丹寨县| 镇康县| 礼泉县|