SQL语句大全 下载本文

SQL语句大全

创建数据库

创建之前判断该数据库是否存在

if exists (select * from sysdatabases where name='databaseName') drop database databaseName go

Create DATABASE databasename

删除数据库

drop database databasename

备份sql server

--- 创建备份数据的 device USE master

EXEC sp_addumpdevice 'disk', 'testBack', 'c:\\mssql7backup\\MyNwind_1.dat' --- 开始备份

BACKUP DATABASE pubs TO testBack

创建新表

create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) 根据已有的表创建新表: A:go use 原数据库名 go

select * 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=\数据表 where 字段名=字段值 order by 字段名 [desc]\

sql=\* from 数据表 where 字段名 like '%字段值%' order by 字段名 [desc]\

sql=\数据表 where 字段名=字段值 order by 字段名 [desc]\sql=\数据表 order by 字段名 [desc]\sql=\数据表 where 字段名 in ('值1','值2','值3')\sql=\数据表 where 字段名 between 值1 and 值2\(2) 更新数据记录:

sql=\数据表 set 字段名=字段值 where 条件表达式\

sql=\数据表 set 字段1=值1,字段2=值2 …… 字段n=值n where 条件表达式\

(3) 删除数据记录:

sql=\数据表 where 条件表达式\ sql=\数据表\将数据表所有记录删除) (4) 添加数据记录:

sql=\数据表 (字段1,字段2,字段3 …) values (值1,值2,值3 …)\ sql=\into 目标数据表 select * from 源数据表\(把源数据表的记录添加到目标数据表)

(5) 数据记录统计函数:

AVG(字段名) 得出一个表格栏平均值

COUNT(*;字段名) 对数据行数的统计或对某一栏有值的数据行数统计 MAX(字段名) 取得一个表格栏最大的值 MIN(字段名) 取得一个表格栏最小的值 SUM(字段名) 把数据栏的值相加 引用以上函数的方法:

sql=\字段名) as 别名 from 数据表 where 条件表达式\set rs=conn.excute(sql)

用 rs(\别名\获取统计的值,其它函数运用同上。 查询去除重复值:select distinct * from table1 (5) 数据表的建立和删除:

CREATE TABLE 数据表名称(字段1 类型1(长度),字段2 类型2(长度) …… ) (6) 单列求和:

SELECT SUM(字段名) FROM 数据表

编辑本段最新sql语句

查询数据库中含有同一这字段的表:

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 test.dbo.b from GCRT.dbo.page_shsjgrgl where 1<>1 复制内容:

insert into test.dbo.b(xm,ssdq) select xm,ssdq from GCRT.dbo.page_shsjgrgl 查看数据库中所有的数据表表名: 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 s.b1 !=

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 a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c B:right outer join:

右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。