常用MSSQL語句

八戒他哥發表於2018-05-31

現在很少用SQL 寫東西,但有時真用起來半天想不起來,看來還是有必要記錄一下。。。

 

新建表:
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,

)

修改欄位型別

Alter table [表名] Alter column [列名] [列型別] 

 

建立表時同時建立主鍵、外來鍵:

create table grade
(id int ,
grade int
constraint id_fk foreign key (id) references student (id)
)

create Table Examination (e_id smallint primary key not null,l_language float, english float,computer float)  –可以建立表時同時建立主鍵、外來鍵

alter table examination add constraint FK_ID foreign key(student_id)  references student(student_id)  –當然也可以建立表後再建外來鍵

alter table examination add student_id int not null   –修改student_id 為not null
alter table examination alter column e_id nvarchar not null  –也要修改為not null 
alter table examination add constraint PK_Id primary key(e_id) –新增主鍵
alter table examination drop constraint PK_id   –刪除主鍵
alter table examination alter column student_id nvarchar(50)  –修改欄位資料型別
alter table examination add constraint FK_ID foreign key(student_id)  references student(student_id) –新增外來鍵約束

 

 

 

刪除表:
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
END
CLOSE  curItems
DEALLOCATE  curItems

–刪除主鍵

alter table 表名 drop constraint 主鍵名
–新增主鍵
alter table 表名 add constraint 主鍵名 primary key(欄位名1,欄位名2……)
–新增非聚集索引的主鍵
alter table 表名 add constraint 主鍵名 primary key NONCLUSTERED(欄位名1,欄位名2……)

插入資料:
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   …