1. Create database:
if exists(select * from sysdatabases where name =‘databaseName‘)
drop database databaseName
go
Create DATABASE databaseName
on primary
2. Delete database
drop database databaseName
3. create table tableName(col1 type1 [not null][primary key] identity, col2 type2 [not null],...)--primary key
4. drop table tableName
5. delete from tableName
6. Alter table tableName add column columnName columnType
7. Alter table tableName drop column columnName
8. Alter table tableName add primary key
9. Alter table tableName drop primary key
10. create [unique] index indexName on tableName
11. drop index indexName on tableName
12.create view viewName as select statement
13. drop view viewName
14. select name from sysobjects where xtype = ‘u‘ and id in (select id from syssolumns where name =‘S3‘)
15. select top 10 * from tableName order by id
16.Alter table tableName alter column s int
17. select a.a, a.b, a.c, b.c, b.d from a left outer join b ON a.a = b.c 包含连接表的匹配项,也包括左连结表的所有行
18 复制表select * into b from a where
19.insert into b(x,y,z) select d,e,f from a where 。。。
20. select a,b,c from a where time between time1 and time2
21 Insert into table (col1, col2,col3) values(‘test‘,‘N‘,Null)
22. update table set col1=‘value’ where col2=‘N’
23. delete from table where col2 =‘N‘
24.
SELECT ProductID, AVG(OrderQty) AS AverageQuantity, SUM(LineTotal) AS Total
FROM Sales.SalesOrderDetail
GROUP BY ProductID
HAVING SUM(LineTotal) > $1000000.00
AND AVG(OrderQty) < 3 ;