建立資料庫
建立之前判斷該資料庫是否存在
if exists (select * from sysdatabases where name='databasename')
drop database databasename
gocreate database databasename
刪除資料庫
drop database databasename
備份sql server
--- 建立備份資料的 device
use master
exec sp_addumpdevice 'disk', 'testback', 'c:\mssql7backup\mynwind_'
--- 開始備份
backup database pubs to testback
建立新錶
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
根據已有的表建立新錶:
a:go
use 原資料庫名
goselect * into 目的資料庫名.dbo.目的表名 from 原表名(使用舊表建立新錶)
b:create table tab_new as select col1,col2… from tab_old definition only
建立序列
create sequence simon_sequence
minvalue 1 -- 最小值
maxvalue 999999999999999999999999999 -- 最大值
start with 1 -- 開始值
increment by 1 -- 每次加幾
cache 20;
刪除新錶
drop table tabname
增加乙個列
alter table tabname add colname coltype
刪除乙個列
alter table tabname drop column colname
新增主鍵
alter table tabname add primary key(col)
說明:刪除主鍵:alter table tabname drop primary key(col)
建立索引
create [unique] index idxname on tabname(col…。)
刪除索引:drop index idxname on tabname
注:索引是不可更改的,想更改必須刪除重新建。
建立檢視
create view viewname as select statement
刪除檢視:drop view viewname
基本的sql語句
(1) 資料記錄篩選:
sql="select * from 資料表 where欄位名=字段值 order by欄位名[desc]"
sql="select * from 資料表 where欄位名like '%字段值%' order by 欄位名 [desc]"
sql="select top 10 * from 資料表 where欄位名=字段值 order by 欄位名 [desc]"
sql="select top 10 * from 資料表 order by 欄位名 [desc]"
sql="select * from 資料表 where欄位名in ('值1','值2','值3')"
sql="select * from 資料表 where欄位名between 值1 and 值2"
(2) 更新資料記錄:
sql="update 資料表 set欄位名=字段值 where 條件表示式"
sql="update 資料表 set 欄位1=值1,欄位2=值2 …… 欄位n=值n where 條件表示式"
(3) 刪除資料記錄:
sql="delete from 資料表 where 條件表示式"
sql="delete from 資料表" (將資料表所有記錄刪除)
(4) 新增資料記錄:
sql="insert into 資料表 (欄位1,欄位2,欄位3 …) values (值1,值2,值3 …)"
sql="insert into 目標資料表 select * from 源資料表" (把源資料表的記錄新增到目標資料表)
(5) 資料記錄統計函式:
**g(欄位名) 得出乙個**欄平均值
count(*;欄位名) 對資料行數的統計或對某一欄有值的資料行數統計
max(欄位名) 取得乙個**欄最大的值
min(欄位名) 取得乙個**欄最小的值
sum(欄位名) 把資料欄的值相加
引用以上函式的方法:
sql="select sum(欄位名) as 別名 from 資料表 where 條件表示式"
set rs=
用 rs("別名") 獲取統計的值,其它函式運用同上。
查詢去除重複值:select distinct * from table1
(5) 資料表的建立和刪除:
create table 資料表名稱(欄位1 型別1(長度),欄位2 型別2(長度) …… )
(6) 單列求和:
select sum(欄位名) from 資料表
編輯本段最新語句
查詢資料庫中含有同一這字段的表:
select name from sysobjects where xtype = 'u' and id in(select id from syscolumns where name = 's3')
根據出生日期可以算出年齡:
select datediff(year,scrq,'2013') as 年齡 from page_shsjgrgl
根據當前年份自動算出年齡
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
在同一資料庫中複製表結構:
select * into a from b where 1<>1
當 identity_insert 設定為 off 時,不能為表 'aa' 中的標識列插入顯式值。
set identity_insert aa on----設定開啟,
批量插入:
insert into aa(customer_id, id_type, id_number) select customer_id, id_type, id_number from tcustomer;
set identity_insert aa off---關閉
不同資料庫之間的複製:
複製結構:
select * into from where 1<>1
複製內容:
insert into select xm,ssdq from
檢視資料庫中所有的資料表表名:
select name from sysobjects where type='u'
檢視資料庫中所有表含有同一欄位的表:
select name from sysobjects where xtype = 'u' and id in(select id from syscolumns where name = '同一字段')
檢視資料表中的所有字段:
select name from syscolumns where id=object_id('表名')
查詢資料庫時隨機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 !=
case @temp when '' then '' else @temp end
)更改資料庫表字段型別:
alter table page_shsjgrgl alter column s1 int
高階查詢
a:union運算子
union 運算子通過組合其他兩個結果表(例如table1 和table2)並消去表中任何重複行而派生出乙個結果表。當 all 隨union 一起使用時(即union all),不消除重複行。兩種情況下,派生表的每一行不是來自table1 就是來自table2。
b: except運算子
except 運算子通過包括所有在table1 中但不在table2 中的行並消除所有重複行而派生出乙個結果表。當all 隨except 一起使用時(except all),不消除重複行。
c:intersect運算子
intersect 運算子通過只包括table1 和table2 中都有的行並消除所有重複行而派生出乙個結果表。當all 隨intersect 一起使用時(intersect all),不消除重複行。
注:使用運算詞的幾個查詢結果行必須是一致的。
外連線a、left outer join:
左外連線(左連線):結果集既包括連線表的匹配行,也包括左連線表的所有行。
sql: select from a left out join b on =
SQL語句常用
1 新建資料庫aa createdatabaseaa 2 開啟資料庫aa opendatabaseaa 3 選擇aa為當前資料庫 setdatabasetoaa 4 修改資料庫aa modifydatabaseaa 5 刪除資料庫aa deletedatabaseaa 6 新建專案aa create...
SQL語句練習
資料庫的基本操作為增 刪 改 查,對應這四種操作有insert delete update select語句。這四種基本操作的語法要求掌握。針對這四種基本操作完成下面練習。1在員工表增加新的員工資訊 姓名 張三 員工號 99999 性別 男 出生日期 1980 2 15 部門 採購科 工作時間 19...
SQL語句面試題
2.請教乙個面試中遇到的sql語句的查詢問題 表中有a b c三列,用sql語句實現 當a列大於b列時選擇a列否則選擇b列,當b列大於c列時選擇b列否則選擇c列。create table tmp a int,b int,c int insert into tmp values 10 20 30 in...