常用sql语句

来源:互联网 发布:iphone7电池检测软件 编辑:程序博客网 时间:2024/06/05 15:38

创建数据库

CREATE DATABASE database-name

删除数据库

drop database dbname

创建新表

create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)

增加一个列

Alter table tabname add column col type

添加主键

Alter table tabname add primary key(col)

插入

insert into table(field1,field2) values(value1,value2)

删除

delete form table where ···

更新

update table set field1 = value where ···

查找

select * from table where field1 like '%value1%'

排序

select * from table order by field1,field2 desc

总数

select count as totalcount from table1

求和

select sum(field1) as sumvalue from table1

平均

select avg(field1) as svgvalue from table1

最大

select max(field1) as maxvalue from table1

最小

select min(field1) as minvalue from table1

UNION 运算符

UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。

EXCEPT 运算符

 EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。

INTERSECT 运算符

INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。

sql重置数据库自增长字段

方法1:清空已有数据,并且将自增字段恢复从1开始计数truncate table 表明
方法2:不清空已有数据,但将自增字段恢复从1开始计数dbcc checkident(表名,RESEED,0)关于方法2: dbcc   checkident(表名,RESEED,0)   参数3是数字几,就表示从数字几开始计数select * from dbo.Dim_Currencydelete   from dbo.Dim_Currencydbcc checkident(Dim_Currency,reseed,0)   --重新置位identity属性的字段,让其下个值从1开始
0 0