您现在的位置: 万盛学电脑网 >> 程序编程 >> 数据库 >> mssql数据库 >> 正文

SQLServer基础语法实例应用(一)

作者:佚名    责任编辑:admin    更新时间:2022-06-22

 

一、基础

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

2、说明:删除数据库

? 1 DROP  DATABASE database-name 

3、说明:备份数据库

? 1 2 3 4 5 USE master -- 创建 备份数据的 device EXEC sp_addumpdevice 'disk', 'cc_jz', 'd:cc_jz.dat' -- 开始 备份 BACKUP DATABASE cc_jz TO cc_jz

4、说明:创建新表

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)   --> 测试数据:[a] if object_id('[a]') is not null drop table [a] go create table [a]([ID] int,[品名] varchar(6),[入库数量] int,[入库时间] datetime) insert [a] select 1,'矿泉水',100,'2013-01-02' union all select 2,'方便面',60,'2013-01-03' union all select 3,'方便面',50,'2013-01-03' union all select 4,'矿泉水',80,'2013-01-04' union all select 5,'方便面',50,'2013-01-05'   select * from a /* ID          品名     入库数量        入库时间 ----------- ------ ----------- ----------------------- 1           矿泉水    100         2013-01-02 00:00:00.000 2           方便面    60          2013-01-03 00:00:00.000 3           方便面    50          2013-01-03 00:00:00.000 4           矿泉水    80          2013-01-04 00:00:00.000 5           方便面    50          2013-01-05 00:00:00.000   (5 行受影响)   */

5、说明:删除新表

? 1 drop table tabname

 6、说明:增加一个列

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Alter table tabname add column col type   Alter table a add  col int   select * from a /* ID          品名     入库数量        入库时间                    col ----------- ------ ----------- ----------------------- ----------- 1           矿泉水    100         2013-01-02 00:00:00.000 NULL 2           方便面    60          2013-01-03 00:00:00.000 NULL 3           方便面    50          2013-01-03 00:00:00.000 NULL 4           矿泉水    80          2013-01-04 00:00:00.000 NULL 5           方便面    50          2013-01-05 00:00:00.000 NULL   (5 行受影响)   */

7、说明:添加主键:

? 1 Alter table tabname add primary key(col)

说明:删除主键:

? 1 Alter table tabname drop primary key(col)

8、说明:创建索引:

? 1 create [unique] index idxname on tabname(col….)

删除索引:

? 1 drop index idxname

注:索引是不可更改的,想更改必须删除重新建。

9、说明:创建视图:

? 1 create view viewname as select statement

删除视图:

? 1 drop view viewname 10、说明:几个简单的基本的sql语句 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 --选择: select * from table1 --插入: insert into table1(field1,field2) values(value1,value2) --删除: delete from table1 --where 范围 --更新: update table1 set field1=value1 --where 范围 --查找: select * from table1 where field1 like '%value1%' --排序: select * from table1 order by field1,field2 [desc] --总数: select count as totalcount from table1 --求和: select sum(field1) as