開發總結 資料庫

2021-08-30 07:57:49 字數 1467 閱讀 9894

1、n個以上條件的查詢

在專案中經常會遇到對乙個表或檢視進行多個條件查詢,如下:

select * from tab1 where

col1>v1 and col2>v2 and col3>v3 and col4>v4 ....

記得剛開始參加工作時是直接在**裡封裝這多達好幾十個條件的sql語句,後來改為在儲存過程中用 if 語句判斷,最近又遇到此類問題,參考網上幾位牛人的方法,改用or直接查詢,**減少不說,也方便維護

create table history

(code varchar(6),

[name] nvarchar(20),

price money,

datadt datetime

)create proc proc_getcodes(

@dt datetime,

@price money)as

select

*from

history

where

(@datadt is null or datadt>@dt)

and(@price is null or price>@price)

)

/*查詢表的總行數,

select dbo.row_count('tradedetail')

*/

alter function row_count(@tablename sysname)

returns bigint

asbegin

declare @nn bigint

if @tablename is not null

begin

select @nn=sum(rows) from sys.partitions where index_id in(0,1) and [object_id]=object_id(@tablename)

endreturn @nn

endgo

查詢tradestat表的所有列名

select name from syscolumns where id in (select id from sysobjects where type = 'u' and name = 'tradestat')

--或者

select name from syscolumns where id=object_id('tradestat')

更改表的字段型別

alter table tradedetail alter column price smallmoney

待續。。。

資料庫開發

前面四種 除了基本的sql方面的知識,還要對開發流程,軟體工程,各種框架和開發工具等等 資料庫應用開發這個方向上的機會最多,職位最多,薪水一般 資料建模專家 data modeler 除了基本的sql方面的知識,非常熟悉資料庫原理,資料建模 負責將使用者對資料的需求轉化為資料庫物理設計和物理設計 這...

使用oracle資料庫開發,異常總結

最近兩天使用的oracle資料庫開發專案時遇到了2個異常,第乙個是執行sql語句時報異常 ora 00911 無效字元 如下圖 sql語句如下 斷點除錯,把sql語句拷貝到pl sql裡執行,sql語句執行正常。反覆測試幾次後,去掉了sql語句結尾的分號,程式執行正常。總結到 pl sql裡執行的s...

資料庫總結

從這張表可以看到存在冗餘,西遊記,孫悟空 和 葫蘆娃,火娃 出現兩次,在資料量更大的情況下可能出現更多次。假設用這張表,然後候選鍵為什麼?你分析一下,小朋友的id肯定是候選鍵 候選鍵的概念,請翻閱資料庫教材 之一,就可以了麼。貌似不行,因為動漫電影不由這個決定,那麼動漫人物加上作為候選鍵之一,根據這...