創(chuàng)建數(shù)據(jù)庫(kù)

創(chuàng)建之前判斷該數(shù)據(jù)庫(kù)是否存在
if exists (select * from sysdatabases where name='databaseName')
drop database databaseName
go
Create DATABASE databasename
on primary-- 默認(rèn)就屬于primary文件組,可省略
/*--數(shù)據(jù)文件的具體描述--*/
name=‘databasename_data’,-- 主數(shù)據(jù)文件的邏輯名稱
filename=‘'所存位置:\databasename_data.mdf’, -- 主數(shù)據(jù)文件的物理名稱
size=數(shù)值mb, --主數(shù)據(jù)文件的初始大小
maxsize=數(shù)值mb, -- 主數(shù)據(jù)文件增長(zhǎng)的最大值
filegrowth=數(shù)值%--主數(shù)據(jù)文件的增長(zhǎng)率
log on
/*--日志文件的具體描述,各參數(shù)含義同上--*/
name='databasename_log', -- 日志文件的邏輯名稱
filename='所存目錄:\databasename_log.ldf', -- 日志文件的物理名稱
size=數(shù)值mb, --日志文件的初始大小
filegrowth=數(shù)值mb--日志文件的增長(zhǎng)值

刪除數(shù)據(jù)庫(kù)

drop database databasename

備份sql server

--- 創(chuàng)建備份數(shù)據(jù)的 device
USE master
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'
--- 開(kāi)始備份
BACKUP DATABASE pubs TO testBack

創(chuàng)建新表

create table tabname(col1 type1 [not null] [primary key] identity(起始值,遞增量)
,col2 type2 [not null],..)--primary key為主鍵 identity表示遞增數(shù)量
根據(jù)已有的表創(chuàng)建新表:
A:go
use 原數(shù)據(jù)庫(kù)名
go
select * into 目的數(shù)據(jù)庫(kù)名.dbo.目的表名 from 原表名(使用舊表創(chuàng)建新表)
B:create table tab_new as select col1,col2… from tab_old definition only

創(chuàng)建序列

create sequence SIMON_SEQUENCE
minvalue 1 -- 最小值
maxvalue 999999999999999999999999999 -- 最大值
start with 1 -- 開(kāi)始值
increment by 1 -- 每次加幾
cache 20;

刪除表

drop table tabname--這是將表連同表中信息一起刪除但是日志文件中會(huì)有記錄

刪除表中信息

delete from tabname-這是將表中信息刪除但是會(huì)保留這個(gè)表

增加一個(gè)列

Alter table tabname add colname coltype

刪除一個(gè)列

Alter table tabname drop column colname

添加主鍵

Alter table tabname add primary key(col)
說(shuō)明:刪除主鍵:Alter table tabname drop primary key(col)

創(chuàng)建索引

create [unique] index idxname on tabname(col…。)
刪除索引:drop index idxname on tabname
注:索引是不可更改的,想更改必須刪除重新建。

創(chuàng)建視圖

create view viewname as select statement
刪除視圖:drop view viewname

基本的sql語(yǔ)句

(1) 數(shù)據(jù)記錄篩選:
sql="select * from 數(shù)據(jù)表 where字段名=字段值 order by字段名[desc]"
sql="select * from 數(shù)據(jù)表 where字段名like '%字段值%' order by 字段名 [desc]"
sql="select top 10 * from 數(shù)據(jù)表 where字段名=字段值 order by 字段名 [desc]"
sql="select top 10 * from 數(shù)據(jù)表 order by 字段名 [desc]"
sql="select * from 數(shù)據(jù)表 where字段名in ('值1','值2','值3')"
sql="select * from 數(shù)據(jù)表 where字段名between 值1 and 值2"
(2) 更新數(shù)據(jù)記錄:
sql="update 數(shù)據(jù)表 set字段名=字段值 where 條件表達(dá)式"
sql="update 數(shù)據(jù)表 set 字段1=值1,字段2=值2 …… 字段n=值n where 條件表達(dá)式"
(3) 刪除數(shù)據(jù)記錄:
sql="delete from 數(shù)據(jù)表 where 條件表達(dá)式"
sql="delete from 數(shù)據(jù)表" (將數(shù)據(jù)表所有記錄刪除)
(4) 添加數(shù)據(jù)記錄:
sql="insert into 數(shù)據(jù)表 (字段1,字段2,字段3 …) values (值1,值2,值3 …)"
sql="insert into 目標(biāo)數(shù)據(jù)表 select * from 源數(shù)據(jù)表" (把源數(shù)據(jù)表的記錄添加到目標(biāo)數(shù)據(jù)表)
(5) 數(shù)據(jù)記錄統(tǒng)計(jì)函數(shù):
AVG(字段名) 得出一個(gè)表格欄平均值
COUNT(*;字段名) 對(duì)數(shù)據(jù)行數(shù)的統(tǒng)計(jì)或?qū)δ骋粰谟兄档臄?shù)據(jù)行數(shù)統(tǒng)計(jì)
MAX(字段名) 取得一個(gè)表格欄最大的值
MIN(字段名) 取得一個(gè)表格欄最小的值
SUM(字段名) 把數(shù)據(jù)欄的值相加
引用以上函數(shù)的方法:
sql="select sum(字段名) as 別名 from 數(shù)據(jù)表 where 條件表達(dá)式"
set rs=conn.excute(sql)
用 rs("別名") 獲取統(tǒng)計(jì)的值,其它函數(shù)運(yùn)用同上。
查詢?nèi)コ貜?fù)值:select distinct * from table1
(5) 數(shù)據(jù)表的建立和刪除:
CREATE TABLE 數(shù)據(jù)表名稱(字段1 類型1(長(zhǎng)度),字段2 類型2(長(zhǎng)度) …… )
(6) 單列求和:
SELECT SUM(字段名) FROM 數(shù)據(jù)表

編輯本段最新語(yǔ)句

查詢數(shù)據(jù)庫(kù)中含有同一這字段的表:
select name from sysobjects where xtype = 'u' and id in(select id from syscolumns where name = 's3')
根據(jù)出生日期可以算出年齡:
select datediff(year,scrq,'2013') as 年齡 from page_shsjgrgl
根據(jù)當(dāng)前年份自動(dòng)算出年齡
select datediff(year,csny,cast(YEAR(GETDATE()) as char))
select year(djsj) from page_shsjgrgl
select month(djsj) from page_shsjgrgl
select day(djsj) from page_shsjgrgl
在同一數(shù)據(jù)庫(kù)中復(fù)制表結(jié)構(gòu):
select * into a from b where 1<>1
當(dāng) IDENTITY_INSERT 設(shè)置為 OFF 時(shí),不能為表 'aa' 中的標(biāo)識(shí)列插入顯式值。
set identity_insert aa ON----設(shè)置打開(kāi),
批量插入:
insert into aa(Customer_ID, ID_Type, ID_Number) select Customer_ID, ID_Type, ID_Number from TCustomer;
set identity_insert aa OFF---關(guān)閉
不同數(shù)據(jù)庫(kù)之間的復(fù)制:
復(fù)制結(jié)構(gòu):
select * into test.dbo.b from GCRT.dbo.page_shsjgrgl where 1<>1
復(fù)制內(nèi)容:
insert into test.dbo.b(xm,ssdq) select xm,ssdq from GCRT.dbo.page_shsjgrgl
查看數(shù)據(jù)庫(kù)中所有的數(shù)據(jù)表表名:
select name from SysObjects where type='u'
查看數(shù)據(jù)庫(kù)中所有表含有同一字段的表:
select name from sysobjects where xtype = 'u' and id in(select id from syscolumns where name = '同一字段')
查看數(shù)據(jù)表中的所有字段:
select name from Syscolumns where id=object_id('表名')
查詢數(shù)據(jù)庫(kù)時(shí)隨機(jī)10條記錄:
select top 10 * from td_areacode order by newid()
修改字段類型:
ALTER TABLE 表名 ALTER COLUMN 字段名 varchar(30) NOT NULL
use ZHJIANGJGYL
declare @temp nvarchar(30)
set @temp = 'ZWI4'
select hllx from page_yljg_zyry where hllx not in(
select
case @temp when ''
then ''
else b1 end
from (
select * from TD_Code where page_en='page_yljg_zyry' and B2='ZWI'
) s where s.b1 !=
case @temp when '' then '' else @temp end
)
更改數(shù)據(jù)庫(kù)表字段類型:
alter table page_shsjgrgl alter column s1 int