一、基礎(chǔ)
1、說明:創(chuàng)建數(shù)據(jù)庫
CREATE DATABASE database-name
2、說明:刪除數(shù)據(jù)庫
drop database dbname
3、說明:備份sql server
--- 創(chuàng)建 備份數(shù)據(jù)的 device
USE master
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'
--- 開始 備份
BACKUP DATABASE pubs TO testBack
4、說明:創(chuàng)建新表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
根據(jù)已有的表創(chuàng)建新表:
A:create table tab_new like tab_old (使用舊表創(chuàng)建新表)
B:create table tab_new as select col1,col2… from tab_old definition only
5、說明:刪除新表
drop table tabname
...
二、提升
1、說明:復(fù)制表(只復(fù)制結(jié)構(gòu),源表名:a 新表名:b) (Access可用)
法一:select * into b from a where 1<>1(僅用于SQlServer)
法二:select top 0 * into b from a
2、說明:拷貝表(拷貝數(shù)據(jù),源表名:a 目標(biāo)表名:b) (Access可用)
insert into b(a, b, c) select d,e,f from b;
3、說明:跨數(shù)據(jù)庫之間表的拷貝(具體數(shù)據(jù)使用絕對(duì)路徑) (Access可用)
insert into b(a, b, c) select d,e,f from b in ‘具體數(shù)據(jù)庫’ where 條件
例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..
4、說明:子查詢(表名1:a 表名2:b)
select a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3)
5、說明:顯示文章、提交人和最后回復(fù)時(shí)間
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
6、說明:外連接查詢(表名1:a 表名2:b)
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
...
三、技巧
1、1=1,1=2的使用,在SQL語句組合時(shí)用的較多
“where 1=1” 是表示選擇全部 “where 1=2”全部不選,
如:
if @strWhere !=''
begin
set @strSQL = 'select count(*) as Total from [' + @tblName + '] where ' + @strWhere
end
else
begin
set @strSQL = 'select count(*) as Total
...
數(shù)據(jù)開發(fā)-經(jīng)典
1.按姓氏筆畫排序:
Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as //從少到多
2.數(shù)據(jù)庫加密:
select encrypt('原始密碼')
select pwdencrypt('原始密碼')
select pwdcompare('原始密碼','加密后密碼') = 1--相同;否則不相同 encrypt('原始密碼')
select pwdencrypt('原始密碼')
select pwdcompare('原始密碼','加密后密碼') = 1--相同;否則不相同
3.取回表中字段:
declare @list varchar(1000),
@sql nvarchar(1000)
select @list=@list+','+b.name from sysobjects a,syscolumns b where a.id=b.id and a.name='表A'
set @sql='select '+right(@list,len(@list)-1)+' from 表A'
exec (@sql)