SQL Server 命令例項

2021-06-18 04:47:20 字數 3202 閱讀 5232

--刪除主鍵

alter table 表名 drop constraint 主鍵名

--新增主鍵

alter table 表名 add constraint 主鍵名 primary key(欄位名1,欄位名2……)

--新增非聚集索引的主鍵

alter table 表名 add constraint 主鍵名 primary key nonclustered(欄位名1,欄位名2……)

新建表: 

create table [表名]  ( 

[自動編號字段] int identity (1,1) primary key , 

[欄位1] nvarchar(50) default \'預設值\' null , 

[欄位2] ntext null , 

[欄位3] datetime, 

[欄位4] money null , 

[欄位5] int default 0, 

[欄位6] decimal (12,4) default 0, 

[欄位7] image null ,  )

刪除表:  drop table [表名]

刪除所有表:

declare curitems cursor

for select [name] from sysobjects where xtype='u'

for read only

open curitems

declare @n  nvarchar(100),@m nvarchar(100)

fetch from curitems into @n

while @@fetch_status=0

begin

set @m=@n

exec('drop table ' + @m)

fetch next from curitems into  @n

endclose  curitems

deallocate  curitems

插入資料: insert into [表名] (欄位1,欄位2) values (100,\'51windows.net\')

刪除資料: delete from [表名] where [欄位名]>100

更新資料: 

update [表名] set [欄位1] = 200,[欄位2] = \'51windows.net\' where [欄位三] = \'haiwa\'

新增字段: 

alter table [表名] add [欄位名] nvarchar (50) null

刪除字段: 

alter table [表名] drop column [欄位名]

修改字段: 

alter table [表名] alter column [欄位名] nvarchar (50) null

重新命名表:(access 重新命名表,請參考文章:在access資料庫中重新命名表) 

sp_rename \'表名\', \'新錶名\', \'object\'

新建約束: 

alter table [表名] add constraint 約束名 check ([約束字段] <= \'2000-1-1\')

刪除約束: 

alter table [表名] drop constraint 約束名

新建預設值 

alter table [表名] add constraint 預設值名 default \'51windows.net\' for [欄位名]

刪除預設值 

alter table [表名] drop constraint 預設值名

刪除sql server 中的日誌,減小資料庫檔案大小 

dump transaction 資料庫名 with no_log 

backup log 資料庫名 with no_log 

dbcc shrinkdatabase(資料庫名) 

exec sp_dboption \'資料庫名\', \'autoshrink\', \'true\'

\\\'新增字段通用函式 

sub addcolumn(tablename,columnname,columntype) 

conn.execute(\"alter table \"&tablename&\" add \"&columnname&\" \"&columntype&\"\") 

end sub

\\\'更改字段通用函式 

sub modcolumn(tablename,columnname,columntype) 

conn.execute(\"alter table \"&tablename&\" alter column \"&columnname&\" \"&columntype&\"\") 

end sub

\\\'檢查表是否存在

sql=\"select count(*) as dida from sysobjects where id = object_id(n\'[所有者].[表名]\') and objectproperty(id, n\'isusertable\') = 1\"

set rs=conn.execute(sql)

response.write rs(\"dida\")\'返回乙個數值,0代表沒有,1代表存在

判斷表的存在: 

select * from sysobjects where id = object_id(n\'[dbo].[tablename]\') and objectproperty(id, n\'isusertable\') = 1

某個表的結構 

select * from syscolumns where id = object_id(n\'[dbo].[你的表名]\') and objectproperty(id, n\'isusertable\') = 1

修改表的字首:

alter schema dbo transfer prename.tablename;

如果表2已經存在,把錶1中的記錄加到表2中的語句: 

insert   into   表2   (欄位1,欄位2,...)   select   欄位1,欄位2,..   from   表2   where   ... 

如果表2不存在,則用下面的語句會自動生成表2,欄位的型別和表1一樣: 

select   欄位1,欄位2,..   into   表2   from   表1   where   ... 

sqlserver例項遷移

sqlserver例項遷移.docx 資料庫例項中有大量資料庫,使用者名稱,及密碼或使用者名稱密碼丟失,不能強制修改 如公司.4伺服器資料庫遷移 檢視資料庫版本號 此方案暫只適用於同版本的sqlserver select version 備份系統庫master model msdb到新伺服器 net...

SQL Server 連線例項

sql server的連線可以大致分為三種 內連線 外連線和交叉連線。一 內連線 inner join 使用比較運算子進行表間某 些 列資料的比較操作,並列出這些表中與連線條件相匹配的資料行。根據所使用的比較方式不同。內連線又分為三種 等值連線 自然連線 不等連線三種。二 外連線分 external...

SQL Server儲存過程例項

儲存過程是由過程化sql語句書寫的過程,這個過程經編譯和優化後儲存在資料庫伺服器中。類似於函式,使用時只需呼叫即可。使用儲存過程有以下優點 1 實現了sql語句的可復用性 2 儲存過程降低了客戶機和伺服器之間的通訊量 3 方便實施企業規劃 儲存過程的建立一般格式 gocreate proc proc...