SQL+Server+数据库基本SQL语句汇总

SQL Server语句 序号 1 2 3 4 5 语句 if exists (select * from sysdatabases where name='databaseName') drop database databaseName 创建数据库(创建之前判断该数据库是否存在) go Create DATABASE databasename 删除数据库 drop database databasename 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],..) 1、use 原数据库名 go 根据已有表创建新表 select * into 目的数据库名.dbo.目的表名 from 原表名(使用旧表创建新表) 2、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 tablename add column_b int identity(1,1) Alter table tabname drop column colname ALTER TABLE 表名 ALTER COLUMN 字段名 varchar(30) NOT NULL DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 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="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" 注:like中"%"匹配0个或多个字符;like中"_"匹配一个字符 sql="update 数据表 set 字段名=字段值 where 条件表达式" sql="update 数据表 set 字段1=值1,字段2=值2 字段n=值n where 条件表达式" sql="delete from 数据表 where 条件表达式" sql="delete from 数据表" (将数据表所有记录删除) sql="insert into 数据表 (字段1,字段2,字段3 ) values (值1,值2,值3 )" sql="insert into 目标数据表 select * from 源数据表" (把源数据表的记录添加到目标数据表) AVG(字段名) 得出一个表格栏平均值 COUNT(*;字段名) 对数据行数的统计或对某一栏有值的数据行数统计 MAX(字段名) 取得一个表格栏最大的值 MIN(字段名) 取得一个表格栏最小的值 SUM(字段名) 把数据栏的值相加 引用以上函数的方法: sql="select sum(字段名) as 别名 from 数据表 where 条件表达式" set rs=conn.excute(sql) 用 rs("别名") 获取统计的值,其它函数运用同上。 select distinct * from table1 select name from sysobjects where xtype = 'u' and id in(sele

ct id from syscolumns where name = 's3') select * into a from b where 1<>1 select top 0 * into b from a 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 UNION 运算符通过组合其他两个结果表(例如TABLE1 和TABLE2)并消去表中任何重复行而派生 出一个结果表。当 ALL 随UNION 一起使用时(即UNION ALL),不消除重复行。两种情况下,派 生表的每一行不是来自TABLE1 就是来自TABLE2。 EXCEPT 运算符通过包括所有在TABLE1 中但不在TABLE2 中的行并消除所有重复行而派生出一个 结果表。当ALL 随EXCEPT 一起使用时(EXCEPT ALL),不消除重复行。 功能

6

创建序列

7 8 9 10 11 12 13 14 15 16

删除新表 增加一个列 删除一个列 修改一个列 添加主键 删除主键 创建索引 删除索引 创建视图 删除视图

17

选择数据记录

18 19 20

更新数据记录 删除数据记录 添加数据记录

21

数据记录统计函数

22 23 24

查询去除重复值 查询数据库中含有同一这字段的表 只复制表结构

25

复制内容

26 27

UNION 运算符(使用运算词的几个查询结果行必须是一致的)

EXCEPT 运算符

1/3

SQL+Server+数据库基本SQL语句汇总

SQL Server语句

SQL+Server+数据库基本SQL语句汇总相关文档

最新文档

返回顶部