資料庫基礎應用知識總結__資料庫

來源:互聯網
上載者:User

資料庫應用基礎

-------------------------------- Start ----------------------- ^_^前言:

如果基本的語句你會的話,前三個基本的資料庫與表及記錄的簡單操作可以跳過。

使用語言:T-SQL語言,資料庫語句

運行環境:Sql Server 2012

-------------------------------- First--------------------------------
一、資料庫、表的基本操作

1.首先資料庫的基本操作:

create database hpu--建立名為 hpu的資料庫use hpu--更改當前資料庫為 hpu,即接下來的操作是基於 hpu資料庫的drop hpu--刪除資料庫 hpu
2.然後對錶操作:

(1)表的建立

車很普及了在今天,很多人都要考駕照,本人還沒拿到手,心疼。。。

以車牌號為例建個表命名為 vehicle

create table vehicle(number_id varchar(11),--車牌brand varchar(11),--品牌color varchar(11),--顏色makedate date--出廠日期)

(2)表的刪除

drop table vehicle--刪除表中資料,同時刪除該表delete vehicle--刪除表中資料
-------------------------------- Second --------------------------------
二、欄位的操作及記錄的增刪改查

(1)欄位的操作

alter table vehicle add carowner varchar(20)--增加欄位(列)alter table vehicle alter column carowner int--修改欄位類型alter table vehicle alter column carowner varchar(15)sp_rename 'vehicle.carowner','carmaster','column'--修改列名alter table vehicle drop column carmaster--刪除表 vehicle的欄位 carmaster

(2)基本的增刪改查是最簡單、最基本也是最常用的 sql語句

insert into vehicle(number_id,brand,makedate) values('豫HPU579','紅旗',getdate())insert into vehicle values('豫HPU33U','福士','銀灰色',getdate())insert into vehicle values('豫HPU123','紅旗','黑色',getdate()-365*2)delete vehicle where number_id = '豫HPU23U'--按條件刪除delete vehicle--刪除表中全部記錄update vehicle set color = '紅色' where brand = '紅旗'--按條件更新車的顏色update vehicle set makedate = getdate()--更新表中全部記錄的出廠日期為目前時間select number_id,makedate from vehicle where brand = '紅旗'--按條件尋找所需欄位的資料select top 2 * from vehicle--查詢表中的前兩條記錄select * from vehicle
-------------------------------- Third --------------------------------
三、約束

約束從字面上理解就是存在一種或多種規則,條件來規範欄位、資料。約束用於維護資料庫完整性,通過表中的列定義約束可以有效防止將錯誤的資料插入表中,也可以保證表之間資料的一致性。

常用的約束有:

1.Primary key主鍵約束

2.Foreign key外鍵約束

3.Unique唯一約束

4.Check檢查約束

5.Default預設約束

6.Not null不為空白

例如對上表 vehicle建立時設定約束

create table vehicle(number_id varchar(11) primary key,--設為主鍵brand varchar(11) not null,--資料不為空白color varchar(11) default '黑色',--預設顏色為黑色makedate date not null--出廠日期)

測試約束:

insert into vehicle(number_id,brand,makedate) values('豫HPU579','紅旗',getdate())--插入成功;顏色預設值為 '黑色'insert into vehicle(number_id,brand,makedate) values('豫HPU579','福士',getdate())--插入失敗:主鍵唯一,已存在主鍵為 '豫HPU579'的記錄不能再插入insert into vehicle values('豫HPU123','福士','蘋果綠',getdate())--插入成功;insert into vehicle values('豫HPU678',null,'蘋果綠',getdate())--插入失敗;不能將值 NULL 插入列 'brand',列不允許有 Null 值

執行查詢語句結果如下:


就像上面如果沒有將約束寫在表的建立語句裡,而表已經建立好的情況下。該怎麼修改呢。

修改語句執行個體如下:

alter table vehicle alter column number_id varchar(11) not null--設定欄位不為空白alter table vehicle alter column brand varchar(11) not nullalter table vehicle alter column makedate date not nullalter table vehicle add constraint PK_number_id primary key(number_id)--修改欄位 number_id為主鍵alter table vehicle add constraint DF_color Default('灰色') for color--設定預設值alter table vehicle add constraint CK_makedate check(makedate < getdate())--核查生產日期應該小於今天
其他修改語句相似。

-------------------------------- Fourth-------------------------------- 四、函數

資料庫語句也有函數,上面的例子中已經用到一個函數 getdate()得到目前時間。

rand()隨機函數;

sin(),cos(),tan()三角函數;

sum(),avg(),max(),min(),len()等求和,求平均數,最大最小值球長度函數;

datediff(),getdate(),dateadd()日期函數;

cast(),str(),char()資料轉換函式;

substring(),left(),right(),ltrim()截取字串函數等。

下面寫個例子簡單說明函數的使用:(利用函數產生一個長度為49的字串,計算程式運行所需時間)

print('目前時間:')print(getdate())--得到目前時間declare @i int , @c varchar(50),@date1 datetime,@date2 datetimeset @i = 0set @c = ''set @date1 = getdate()while @i < 49    begin    set @c = @c + char(round(rand()*26+65,0))--隨機函數,保證產生的數字是隨機的,對應的字元不全相同set @i = @i + 1endset @date2 = getdate()print @cprint '答案的長度為: '+cast(len(@c) as varchar(10))--len()的結果為整型,cast轉換為字串類型print('已耗用時間為:'+cast(datediff(ms,@date1,@date2) as varchar(10))+'ms')--datediff計算兩個給定的時間差,單位為毫秒
運行結果如下:



-------------------------------- Fifth--------------------------------
五、視圖與索引

1.視圖建立與刪除

create view testview as select * from vehicle--建立視圖select * from testviewselect * from vehicledrop view testview--刪除視圖
在sql server2012下執行查詢表與查詢檢視的結果如下:




視圖的查詢結果與普通的查詢表記錄得到的結果相同。兩者的區別是視圖查詢的結果是邏輯結果,直接查詢表擷取的是實際資料。視圖防止了對錶資料的誤刪等操作。不知道我說的對不對,如果有錯誤或不規範請告訴我。謝謝^_^。

視圖的作用:

(1)隱藏了資料的複雜性

(2)有利於控制使用者對錶中某些列的訪問

(3)使使用者查詢變得簡單。

2.索引的建立與使用

create index testindex on vehicle(number_id)--建立索引select * from vehicle where number_id = '豫HPU579'drop index vehicle.testindex--刪除索引
(1)索引的建立,使資料的查詢變得很快,大大減少了查詢時間,在資料總量較少的情況下看不出來,測試插入 200,000條記錄到表 vehicle中,通過 getdate()Function Compute出建立索引前隨機的查詢表中的一條記錄耗時56毫秒,建立索引後用時3毫秒。






(2)索引描述:

使用者對資料庫最頻繁的操作是進行資料查詢。一般情況下,資料庫在進行查詢操作時需要對整個表進行資料搜尋。當表中的資料很多時,搜尋資料就需要很長的時間,這就造成了伺服器的資源浪費。為了提高檢索資料的能力,資料庫引入了索引機制使用者對資料庫最頻繁的操作是進行資料查詢。 -------------------------------- Sixth-------------------------------- 六、觸發器

1.觸發器的建立與刪除

create trigger testtri on vehicle for insert,update,delete asbegin    print('你對錶 vehicle 進行了操作')end--建立觸發器drop trigger testtri--刪除觸發器
此時在執行刪除插入或更新操作,控制台會輸出提示句。觸發器的建立實現了對錶記錄的操作進行監聽。

效果如下:



觸發器就是可以在執行某操作前或某個事件觸發而執行的操作。

2.觸發器的作用

百度給出的作用:

(1)可在寫入資料表前,強制檢驗或轉換資料

(2)觸發器發生錯誤時,異動的結果會被撤銷

(3)部分資料庫管理系統可以針對資料定義語言 (Data Definition Language)(DDL)使用觸發器,稱為 DDL觸發器

(4)可依照特定的情況,替換異動的指令

希望對你有所協助。

-------------------------------- End --------------------------------



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.