--------------------我的一點學習筆記(基於2005)----------

---用戶定義數據類型
create type postcode
from varchar(6) null  --創建用戶定義數據類型
go
select * from information_schema.domains ----獲取用戶定義數據類型信息
select * from information_schema.column_domain_usage ---查看用戶定義數據類型的使用情況
drop type postcode ---刪除用戶定義數據類型


---同義詞
create synonym tb11 for tb ---創建

select * from sys.synonyms   ----查詢同義詞信息

drop synonym tb11  ---刪除


if....else用法

if month(getdate())<7
begin
print('上半年')
select(getdate())
end
else
begin
print('下半年')
select(getdate())
end

--while,continue,break用法

declare @i int
set @i=1
while @i<20
begin
 set @i=@i+1
 if @i<=19
 continue
 print @i
end

declare @i int
set @i=1
while @i<20
begin
 if @i=19
 break
 print @i
 set @i=@i+1
end

----try...catch用法
begin try
   select 1/0
end try
begin catch
   print'打印錯誤'
end catch

---raiserror的用法
begin try
  raiserror('生成一個錯誤消息',11,1)
end try
begin catch
  select error_message() as 錯誤消息,
         error_severity() as嚴重級別,
         error_state() as state;
end catch

---使用sp_addmessage存儲過程添加自定義錯誤
exec sp_addmessage 50001,15, N'new user defined error message!',us_english
exec sp_addmessage 50001,15, N'新增用戶自定義錯誤消息!'
exec sp_dropmessage 50001,'all'

---使用sp_altermessage存儲過程修改用戶定義錯誤消息
exec sp_altermessage 50001,'with_log',true'

---2005查看數據庫信息
select * from sys.databases

exec sp_helpdb

--查看數據庫空間使用情況
exec sp_spaceused

--創建數據庫時指定數據文件
create database db
on
primary(name=db,
        filename='C:/program files/mircosoft SQL server/mssql10.mssqlserver/mssql/data/db.mdf',
        size=100MB
        maxsize=200
        filegrowth=20)
go
--解釋下上面的參數:
  
--primary關鍵字,指定關聯<文件定義>列表用於定義主要數據文件,一個數據庫只能有一個主文件,如果沒有指定primary,那麼create database 語句中列出的第一個文件將成爲主文件。

--name關鍵字,用於指定數據文件的邏輯名稱

--filename關鍵字,指定數據文件的操作系統文件名。其後面的參數是創建數據文件時定義的物理文件的路徑名和文件名。

--size關鍵字,指定數據文件的大小。

--maxsize關鍵字,指定數據文件可以增長到的最大大小。

--unlimitted關鍵字,上面的例子中省略掉了。若指定了,則代表指定定義的數據文件將增長到磁盤變滿爲止。

--filegrowth關鍵字,指定數據文件的增長增量,其值不能超過maxsize設置。0表示不增長。,默認值爲MB。如果指定爲%,則增量大小爲發生時文件大小的指定百分比,如果沒有指定,默認值爲10%。

--在創建數據庫時指定文件組
create database db
on
primary(name=db1,
        filename='C:/program files/mircosoft SQL server/mssql10.mssqlserver/mssql/data/db1.mdf',
        size=100MB
        maxsize=200
        filegrowth=20),
filegroup filegroup1
(name=db2,
        filename='C:/program files/mircosoft SQL server/mssql10.mssqlserver/mssql/data/db2.mdf',
        size=100MB
        maxsize=200
        filegrowth=20)
go

--創建數據庫時指定事務日誌文件

create database db
on
primary(name=db1,
        filename='C:/program files/mircosoft SQL server/mssql10.mssqlserver/mssql/data/db1.mdf',
        size=100MB
        maxsize=200
        filegrowth=20),
log on(
  name=dblog,
        filename='C:/program files/mircosoft SQL server/mssql10.mssqlserver/mssql/data/db_log.ldf',
        size=10MB
        maxsize=20
        filegrowth=2)
go

--獲取約束信息
select * from information_schema.constraint_column_usage---可以獲取指定數據庫中的所有約束的信息以及約束與列的對應關係
go

select * from information_schema.constraint_table_usage---查詢結果中只包含表和約束的對應關係,並沒有約束對應的列信息
go

select * from information_schema.table_constraints---查詢結果中只包含表和約束的對應關係,並沒有約束對應的列信息
go

select * from information_schema.key_column_usage---可以獲取指定數據庫中的所有鍵約束的列信息,包括主鍵約束中的主鍵列,唯一約束中的唯一鍵列和外鍵約束中的引用列
go

select * from sys.key_constraints----獲取約束信息

select * from sys.foreign_keys--獲取表中的外鍵約束

select * from sys.foreign_key_columns--獲取外鍵約束的列信息

select * from information_schema.referential_constraints--獲取外鍵約束信息

select * from information_schema.check_constraints--獲取檢查約束信息

---創建主鍵約束,唯一約束,檢查約束
create table test
(
  id int,
  testname varchar(50),
  sex bit,
  class varchar(50),
  score float default(0)
  constraint pk_test primary key/*這裏可以指定是創建聚集或非聚集索引clustered|nonclustered可選項*/(id)
  constraint ix_test unique(testname)
  constraint ck_test check/*這裏可以使用 not for replication可選項,用於指定當從其他表中複製數據時,不檢查約束條件*/(score>=0)
)
  go


alter table tb
add constraint pk_tb primary key nonclustered (id)
go

alter table tb
add constraint ck_sex check(sex='男' or sex='女')
go

---創建和使用默認約束
alter table tb
add constraint de_test default'test' for test  ---在表tb的列test的默認約束爲'test'
go

--創建和使用外鍵約束
alter table tb
add constraint fk_tb_tb1 foreign key(id)
references tb1(id)
go

--使用dbcc checkident檢查和設置表的標識值
create table tb
(
 id int primary key identity,
 name varchar(50)
)
 insert into tb
 select 'a'
 union all
 select 'b'
 union all
 select 'c'
 union all
 select 'd'
go
 dbcc checkident(tb,noreseed)
go


delete from tb where id>2
go
--刪除記錄後,表tb只剩下兩條記錄了,但是此時表tb的標識值仍爲4,可以用下面的語句重置標識值爲2
dbcc checkident(tb,reseed,2)
go

dbcc checkident(tb,noreseed)
go

/*檢查標識信息: 當前標識值 '4',當前列值 '4'。
DBCC 執行完畢。如果 DBCC 輸出了錯誤信息,請與系統管理員聯繫。

(2 行受影響)
檢查標識信息: 當前標識值 '4',當前列值 '2'。
DBCC 執行完畢。如果 DBCC 輸出了錯誤信息,請與系統管理員聯繫。
檢查標識信息: 當前標識值 '2',當前列值 '2'。
DBCC 執行完畢。如果 DBCC 輸出了錯誤信息,請與系統管理員聯繫。*/

drop table tb

---視圖with encryption的用法
create view 員工信息簡歷表
with encryption----這樣就創建了加密視圖
as
  select
     e.emp_name,e.sex,e.title,d.dep_name
  from
     employees e inner join departments d
  on
     e.dep_id=d.dep_id
go

---獲取表和視圖信息
select * from information_shcema.tables---查看當前數據庫中當前用戶有權限查看的所有表和視圖信息

---獲取視圖信息
select * from information_schema.views

---獲取列信息
select * from information_schema.columns
如果需要查看指定表或視圖的列情況,可以使用下面的語句。
select
 *
from
 information_schema.columns
where
 table_catalog='數據庫名'
and
 table_name='表名'

---獲取視圖中列的信息
select * from information_schema.view_column_usage


---獲取列信息
select * from sys.columns

將sys.columns與系統視圖sys.objects和sys.types關聯,獲得列的一些詳細信息,例如
select
  o.name as 表名,
  c.name as 列名,
  t.name as 數據類型,
  c.max_length as 長度,
  c.precision as精度,
  c.scale as 小數位數,
  case c.is_nullable when 1 then '是' else '否' end as 是否允許空,
  case c.is_identity when 1 then '是' else '否' end as 標識列,
from
  sys.columns c inner join sys.objects o
on
  o.object_id=c.object_id
inner join
  sys.types t
on
  c.system_type_id=t.system_type_id
where
  o.name='表名' and t.name<>'sysname'
order by
  c.column_id
go

--獲取視圖中包含表的信息
select * from information_schema.view_table_usage
go
 
--獲取所有數據庫對象的信息
select * from sys.objects

--綁定規則
exec sp_bindrule '規則名','對象名'
例如
exec sp_bindrule 'sexrule','employees.sex'

--解除綁定規則
exec sp_unbindrule '對象名'

--刪除規則
在刪除規則前,需要調用sp_unbindrule存儲過程解除該規則的綁定,例如

exec sp_unbindrule 'employees.sex'
drop rule sexrule

--查看錶的索引信息
exec sp_helpindex tb

--結合sys.indexes和sys.index_columns,sys.objects,sys.columns查詢索引所屬的表或視圖的信息
select
  o.name as 表名,
  i.name as 索引名,
  c.name as 列名,
  i.type_desc as 類型描述,
  is_primary_key as 主鍵約束,
  is_unique_constraint as 唯一約束,
  is_disabled as 禁用
from
  sys.objects o
inner join
  sys.indexes i
on
  i.object_id=o.object_id
inner join
  sys.index_columns ic
on
  ic.index_id=i.index_id and ic.object_id=i.object_id
inner join
  sys.columns c
on
  ic.column_id=c.column_id and ic.object_id=c.object_id
go

--查詢索引的鍵和列信息
select
  o.name as 表名,
  i.name as 索引名,
  c.name as 字段編號,
from
  sysindexes i inner join sysobjects o
on
  i.id=o.id
inner join
  sysindexkeys k
on
  o.id=k.id and i.indid=k.indid
inner join
  syscolumns c
on
  c.id=i.id and k.colid=c.colid
where
  o.name='表名'

 

發佈了111 篇原創文章 · 獲贊 13 · 訪問量 46萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章