數(shù)據(jù)庫實驗工作報告總結(jié)多篇

更新時間:2022-06-18 05:21:59 工作報告 我要投稿

  • 相關(guān)推薦

數(shù)據(jù)庫實驗工作報告總結(jié)多篇

數(shù)據(jù)庫實驗總結(jié)【一】

數(shù)據(jù)庫實驗工作報告總結(jié)多篇

試驗內(nèi)容

1、 數(shù)據(jù)表的建立

基本表《簡單的》帶有主鍵

帶有外碼約束的(外碼來自其他表或者本表)

2、 數(shù)據(jù)表的修改

添加刪除列

修改列屬性類型

添加刪除約束(約束名)

元組的添加,修改,刪除

刪除數(shù)據(jù)表

試驗過程

1、create table student

(

sno char(9) primary key , /*sno是主碼 列級完整性約束條件*/

sname char(20) unique, /*sname取唯一值*/

ssex char(2),

sage smallint, /*類型為smallint*/

sdept char(20) /*所在系*/

);

create table course

(

cno char(4) primary key, /*列級完整性約束條件,cno是主碼*/

cname char(40),

cpno char(4), /*cpno的含義是先行課*/

ccredit smallint,

foreign key (cpno) references course(cno)

/*表級完整性約束條件,cpno是外碼,被參照表是course,被參照列是cno*/

);

create table sc

(

sno char(9),

cno char(4),

grade smallint,

primary key (sno,cno),

/*主碼有兩個屬性構(gòu)成,必須作為表級完整性進行定義*/

foreign key (sno) references student(sno),

/*表級完整性約束條件,sno是外碼,被參照表是student*/

foreign key (cno) references course(cno),

/*表級完整性約束條件,cno是外碼,被參照表示course*/

);

例1、create table s

(

cno varchar(3), /*變長的字符串,輸入2個字符就是兩個字符不會補空格*/

sname varchar(20),

status int,

city varchar(20),

constraint pk_sno primary key(sno), /*約束條件的名字為pk_sno*/

);

create table p

(

pno varchar(3),

pname varchar(20),

color varchar(3),

weight int,

constraint pk_pno primary key (pno), /*約束條件的名字是pk_pno*/

);

create table j

(

jno varchar(3),

jname varchar(20),

city varchar(20),

constraint pk_jno primary key(jno) /*約束條件的名字為pk_jno*/

);

例2、create table spj

(

sno varchar(3), /*第一個表中的主碼*/

pno varchar(3),

jno varchar(3),

qty int, /*數(shù)量*/

constraint pk_spj primary key(sno,pno,jno), /*主碼由3個屬性組成*/

foreign key(sno) references s(sno),

/*表級完整性約束條件,sno是外碼,被參照表是s*/

foreign key(pno) references p(pno),

/*表級完整性約束條件,pno是外碼,被參照表是p*/

foreign key(jno) references j(jno),

/*表級完整性約束條件,jno是外碼,被參照表是j*/

);

2、數(shù)據(jù)表的更改

在s表中添加一個concat 列

alter table s add concat varchar(20)

在s表中刪除concat 列

alter table s drop column concat

更改s表 concat列的屬性 把長度由20改為30

alter table s alter column concat varchar(30)

聯(lián)系方式 名字為concat 修改屬性為唯一的 屬性名為con_concat

alter table s add constraint con_concat unique(concat)

刪除約束關(guān)系con_concat

alter table s drop constraint con_concat

/*插入一個元組*/

insert into s valus(‘s1’,’精益’,20,’天津’) /*20不能寫成’20’*/

試驗中的問題的排除與總結(jié):

1、在創(chuàng)建spj時

有三個實體所以從3個實體中取主碼,還有一個數(shù)量屬性也要寫上

主碼由那3個主碼確定

2、更改一個數(shù)據(jù)庫中數(shù)據(jù)表時一定要先使該數(shù)據(jù)庫處于正在使用狀態(tài)

3、constraint

是可選關(guān)鍵字,表示 primary key、not null、unique、foreign key 或 check 約束定義的開始。約束是特殊屬性,用于強制數(shù)據(jù)完整性并可以為表及其列創(chuàng)建索引。

4、--go可以不加但是要注意順序 注:go --注釋 提示錯誤

5、注意添加一個空元素用 null

附 sql備份

--創(chuàng)建一個數(shù)據(jù)庫 student

create database student

go

--在數(shù)據(jù)庫student中創(chuàng)建表student course sc 注意順序

use student

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

create table student

(

sno char(9) primary key, /*sno是主碼 列級完整性約束條件*/

sname char(10) unique, /*sname取唯一值*/

ssex char(2),

sage smallint, /*類型為smallint*/

sdept char(20) /*所在系*/

); /*;要加*/

-----------

數(shù)據(jù)庫實驗總結(jié)【二】

我在sql server 索引基礎(chǔ)知識系列中,第一篇就講了記錄數(shù)據(jù)的基本格式。那里主要講解的是,數(shù)據(jù)庫的最小讀存單元:數(shù)據(jù)頁。一個數(shù)據(jù)頁是8k大小。

對于數(shù)據(jù)庫來說,它不會每次有一個數(shù)據(jù)頁變化后,就存到硬盤。而是變化達到一定數(shù)量級后才會作這個操作。 這時候,數(shù)據(jù)庫并不是以數(shù)據(jù)頁來作為操作單元,而是以64k的數(shù)據(jù)(8個數(shù)據(jù)頁,一個區(qū))作為操作單元。

區(qū)是管理空間的基本單位。一個區(qū)是八個物理上連續(xù)的頁(即 64 kb)。這意味著 sql server 數(shù)據(jù)庫中每 mb 有 16 個區(qū)。

為了使空間分配更有效,sql server 不會將所有區(qū)分配給包含少量數(shù)據(jù)的表。sql server 有兩種類型的區(qū):

統(tǒng)一區(qū),由單個對象所有。區(qū)中的所有 8 頁只能由所屬對象使用。

混合區(qū),最多可由八個對象共享。區(qū)中八頁的每頁可由不同的對象所有。

通常從混合區(qū)向新表或索引分配頁。當表或索引增長到 8 頁時,將變成使用統(tǒng)一區(qū)進行后續(xù)分配。如果對現(xiàn)有表創(chuàng)建索引,并且該表包含的行足以在索引中生成 8 頁,則對該索引的所有分配都使用統(tǒng)一區(qū)進行。

為何會這樣呢?

其實很簡單:

讀或?qū)?8kb 的時間與讀或?qū)?64 kb的時間幾乎相同。

在 8 kb 到 64 kb 范圍之內(nèi),單個磁盤 i/o 傳輸操作所花的時間主要是磁盤取數(shù)臂和讀/寫磁頭運動的時間。

因此,從數(shù)學(xué)上來講,當需要傳輸 64 kb 以上的 sql 數(shù)據(jù)時,

盡可能地執(zhí)行 64 kb 磁盤傳輸是有益的,即分成數(shù)個64k的操作。

因為 64 kb 傳輸基本上與 8 kb 傳輸一樣快,而每次傳輸?shù)?sql server 數(shù)據(jù)是 8 kb 傳輸?shù)?8 倍。

我們通過一個實例來看 有and 操作符時候的最常見的一種情況。我們有下面一個表,

create table [dbo].[member]( [member_no] [dbo].[numeric_id] identity(1,1) not null, [lastname] [dbo].[shortstring] not null, [firstname] [dbo].[shortstring] not null, [middleinitial] [dbo].[letter] null, [street] [dbo].[shortstring] not null, [city] [dbo].[shortstring] not null, [state_prov] [dbo].[statecode] not null, [country] [dbo].[countrycode] not null, [mail_code] [dbo].[mailcode] not null, [phone_no] [dbo].[phonenumber] null, [photograph] [image] null, [issue_dt] [datetime] not null default (getdate()), [expr_dt] [datetime] not null default (dateadd(year,1,getdate())), [region_no] [dbo].[numeric_id] not null, [corp_no] [dbo].[numeric_id] null, [Pv_balance] [money] null default (0), [curr_balance] [money] null default (0), [member_code] [dbo].[status_code] not null default (' '))

這個表具備下面的四個索引:

索引名 細節(jié) 索引的列

member_corporation_link nonclustered located on primary corp_no

member_ident clustered, unique, primary key located on primary member_no

member_region_link nonclustered located on primary region_no

memberfirstname nonclustered located on primary firstname

當我們執(zhí)行下面的sql查詢時候,

select m.member_no, m.firstname, m.region_nofrom dbo.member as mwhere m.firstname like 'k%' and m.region_no > 6 and m.member_no < 5000go

sql server 會根據(jù)索引方式,優(yōu)化成下面方式來執(zhí)行。

select a.member_no,a.firstname,b.region_nofrom(select m.member_no, m.firstname from dbo.member as m where m.firstname like 'k%' and m.member_no < 5000) a , -- 這個查詢可以直接使用 memberfirstname 非聚集索引,而且這個非聚集索引覆蓋了所有查詢列-- 實際執(zhí)行時,只需要 邏輯讀取 3 次

(select m.member_no, m.region_no from dbo.member as mwhere m.region_no > 6) b

-- 這個查詢可以直接使用 member_region_link 非聚集索引,而且這個非聚集索引覆蓋了所有查詢列-- 實際執(zhí)行時,只需要 邏輯讀取 10 次

where a.member_no = b.member_no

不信,你可以看這兩個sql 的執(zhí)行計劃,以及邏輯讀信息,都是一樣的。

其實上面的sql,如果優(yōu)化成下面的方式,實際的邏輯讀消耗也是一樣的。為何sql server 不會優(yōu)化成下面的方式。是因為 and 操作符優(yōu)化的另外一個原則。

1/26 的數(shù)據(jù)和 1/6 的數(shù)據(jù)找交集的速度要比 1/52 的數(shù)據(jù)和 1/3 的數(shù)據(jù)找交集速度要慢。

select a.member_no,a.firstname,b.region_nofrom(select m.member_no, m.firstname from dbo.member as mwhere m.firstname like 'k%' -- 1/26 數(shù)據(jù)) a,

(select m.member_no, m.region_no from dbo.member as mwhere m.region_no > 6 and m.member_no < 5000-- 1/3 * 1/ 2 數(shù)據(jù)) bwhere a.member_no = b.member_no

當然,我們要學(xué)習(xí)sql 如何優(yōu)化的話,就會用到查詢語句中的一個功能,指定查詢使用哪個索引來進行。

比如下面的查詢語句

select m.member_no, m.firstname, m.region_nofrom dbo.member as m with (index (0))where m.firstname like 'k%' and m.region_no > 6 and m.member_no < 5000go

select m.member_no, m.firstname, m.region_nofrom dbo.member as m with (index (1))where m.firstname like 'k%' and m.region_no > 6 and m.member_no < 5000goselect m.member_no, m.firstname, m.region_nofrom dbo.member as m with (index (membercovering3))where m.firstname like 'k%' and m.region_no > 6 and m.member_no < 5000goselect m.member_no, m.firstname, m.region_nofrom dbo.member as m with (index (memberfirstname, member_region_link))where m.firstname like 'k%' and m.region_no > 6 and m.member_no < 5000go

這里 index 計算符可以是 0 ,1, 指定的一個或者多個索引名字。對于 0 ,1 的意義如下:

如果存在聚集索引,則 index(0) 強制執(zhí)行聚集索引掃描,index(1) 強制執(zhí)行聚集索引掃描或查找(使用性能最高的一種)。

如果不存在聚集索引,則 index(0) 強制執(zhí)行表掃描,index(1) 被解釋為錯誤。

總結(jié)知識點:

簡單來說,我們可以這么理解:sql server 對于每一條查詢語句。會根據(jù)實際索引情況(sysindexes 系統(tǒng)表中存儲這些信息),分析每種組合可能的成本。然后選擇它認為成本最小的一種。作為它實際執(zhí)行的計劃。

成本代價計算的一個主要組成部分是邏輯i/o的數(shù)量,特別是對于單表的查詢。

and 操作要滿足所有條件,這樣,經(jīng)常會要求對幾個數(shù)據(jù)集作交集。數(shù)據(jù)集越小,數(shù)據(jù)集的交集計算越節(jié)省成本。

的項目中,竟然出現(xiàn)了濫用聚集索引的問題。看來沒有培訓(xùn)最最基礎(chǔ)的索引的意義,代價,使用場景,是一個非常大的失誤。這篇博客就是從這個角度來羅列索引的基礎(chǔ)知識。

使用索引的意義

索引在數(shù)據(jù)庫中的作用類似于目錄在書籍中的作用,用來提高查找信息的速度。

使用索引查找數(shù)據(jù),無需對整表進行掃描,可以快速找到所需數(shù)據(jù)。

使用索引的代價

索引需要占用數(shù)據(jù)表以外的物理存儲空間。

創(chuàng)建索引和維護索引要花費一定的時間。

當對表進行更新操作時,索引需要被重建,這樣降低了數(shù)據(jù)的維護速度。

創(chuàng)建索引的列

主鍵

外鍵或在表聯(lián)接操作中經(jīng)常用到的列

在經(jīng)常查詢的字段上最好建立索引

不創(chuàng)建索引的列

很少在查詢中被引用

包含較少的惟一值

定義為 text、ntext 或者 image 數(shù)據(jù)類型的列

heaps是staging data的很好選擇,當它沒有任何index時

excellent for high performance data loading (parallel bulk load and parallel index creation after load)

excellent as a partition to a partitioned view or a partitioned table

聚集索引提高性能的方法,在前面幾篇博客中分別提到過,下面只是一個簡單的大綱,細節(jié)請參看前面幾篇博客。

何時創(chuàng)建聚集索引?

clustered index會提高大多數(shù)table的性能,尤其是當它滿足以下條件時:

獨特, 狹窄, 靜止: 最重要的條件

持續(xù)增長的,最好是只向上增加。例如:

identity

date, identity

guid (only when using newsequentialid() function)

聚集索引唯一性(獨特型的問題)

由于聚集索引的b+樹結(jié)構(gòu)的葉子節(jié)點必須指向具體數(shù)據(jù)。如果你要建立聚集索引的列不唯一,并且你指定的創(chuàng)建的聚集索引是非唯一的聚集索引,則會有以下情況:

如果未使用 unique 屬性創(chuàng)建聚集索引,數(shù)據(jù)庫引擎 將向表自動添加一個四字節(jié) uniqueifier 列。必要時,數(shù)據(jù)庫引擎 將向行自動添加一個 uniqueifier 值,使每個鍵唯一。此列和列值供內(nèi)部使用,用戶不能查看或訪問。

【數(shù)據(jù)庫實驗工作報告總結(jié)多篇】相關(guān)文章:

關(guān)于書法實驗學(xué)校復(fù)查的工作報告06-20

數(shù)據(jù)庫update語句06-30

數(shù)據(jù)庫英文縮寫03-05

數(shù)據(jù)庫概念設(shè)計03-01

建筑實驗員個人專業(yè)技術(shù)工作報告06-20

實習(xí)工作報告總結(jié)06-20

《初中物理實驗教學(xué)研究》課題研究工作報告06-20

看的多,懂得也多的經(jīng)典短語06-19

陽坡降水多還是陰坡降水多04-08

陽坡降水多還是陰坡降水多?04-22

顺昌县| 乐亭县| 宁化县| 龙南县| 东兰县| 南华县| 乃东县| 囊谦县| 阳东县| 渝北区| 古浪县| 奈曼旗| 浏阳市| 芦山县| 曲水县| 天水市| 凤城市| 泉州市| 内丘县| 浑源县| 陈巴尔虎旗| 肥乡县| 宁河县| 平山县| 广元市| 老河口市| 铁岭市| 鹤岗市| 唐山市| 百色市| 武穴市| 凤阳县| 林口县| 西吉县| 周至县| 唐河县| 荥阳市| 张家口市| 翼城县| 阳原县| 荆门市|