一、基础操作:

1、创建数据库:CREATE DATABASE database-name

2、删除数据库:DROP DATABASE dbname

3、备份数据库:

- 创建备份数据的device:USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:mssql7backupMyNwind_1.dat'

- 开始备份:BACKUP DATABASE pubs TO testBack

4、创建新表:

- 基于已有表创建新表:

A:CREATE TABLE tab_new LIKE tab_old

B:CREATE TABLE tab_new AS SELECT col1, col2… FROM tab_old

5、删除表:DROP TABLE tabname

6、增加列:ALTER TABLE tabname ADD COLUMN col type(列增加后将不能删除,DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度)

7、添加主键:ALTER TABLE tabname ADD PRIMARY KEY (col)

- 删除主键:ALTER TABLE tabname DROP PRIMARY KEY (col)

8、创建索引:

- 创建索引:CREATE [UNIQUE] INDEX idxname ON tabname (col…)

- 删除索引:DROP INDEX idxname(索引是不可更改的,想更改必须删除重新建)

9、创建视图:

- 创建视图:CREATE VIEW viewname AS SELECT statement

- 删除视图:DROP VIEW viewname

10、基本查询:

- 简单选择:SELECT * FROM table1 WHERE condition

- 插入数据:INSERT INTO table1 (field1, field2) VALUES (value1, value2)

- 删除数据:DELETE FROM table1 WHERE condition