sp_changegroup 改变数据库用户组
Sp_dboption 查询或改变数据库系统设置
Sp_dropdevice 删除设备
Sp_dropgroup 删除组
Sp_droplogin 删除帐号
Sp_help 查询数据库对象及所有数据库信息
Sp_helpdb 查询数据库信息
Sp_helpdevice 查询设备信息
Sp_helpgroup 查询组信息
Sp_helpindex 查询给定表信息
Sp_helpuser 查询用户信息
Sp_lock 查询当前加锁信息
Sp_monitor 查询SQL服务器统计信息
Sp_password 改变登录帐号口令
Sp_spaceused 查询表中的行数、数据页数及空间大小
Sp_who 查询当前用户及过程信息
Sp_syntax 查询操作语法
Sp_configure 配置系统参数
例:
1> sp_helpdb
2> go
name db_size owner dbid created status
--------------------------------------------------------------------------
master 3.0 MB sa 1 Jan 01, 1900 no options set
model 2.0 MB sa 3 Jan 01, 1900 no options set
sybsystemprocs 10.0 MB sa 4 Oct 24, 1997 trunc log on chkpt
tele114 370.0 MB sa 5 Oct 24, 1997 select into/bulkcopy, trunc log on chkpt
tempdb 22.0 MB sa 2 May 05, 1998 select into/bulkcopy
(0 rows affected, return status = 0)
例:
1> sp_monitor
2> go
last_run current_run seconds
-------------------------- -------------------------- -----------
May 5 1998 4:09PM May 5 1998 4:13PM 224
(0 rows affected)
cpu_busy io_busy idle
------------------------- ------------------------- -------------------------
17(1)-0% 5(0)-0% 923(223)-99%
(0 rows affected)
packets_received packets_sent packet_errors
------------------------- ------------------------- -------------------------
21(9) 51(23) 0(0)
(0 rows affected)
total_read total_write total_errors connections
------------------- ------------------- ------------------- ------------------
947(1) 595(113) 0(0) 3(1)
(0 rows affected, return status = 0)
(5)触发器(Triggers)
触发器是一种特殊的存储过程,用来维护不同表中的相关数据的一致性。当在一张表中插入、删除和修改数据时,触发器就会触发另一个存储过程,从而保持数据的一致性。
(6)缺省与规则(Defaults and rules)
缺省是在数据录入时,若用户没有输入数据,SQL Server自动输入的值。
规则是可以理解为对数据库、某一列、某用户数据类型的限制。
三、SQL 查询语言
SQL不仅包括查询数据的有关命令,还包括创建数据库及其对象,增、删、改数据等功能。分别定义为数据查询语言,数据定义语言及数据操作语言。这里先介绍数据查询语言。其基本句法为:
Select select_list from table_list where search_conditions
1、简单查询
A、选择若干列
Select expression [,expression]... From table_list
B、选择若干行
查出电话号码以415到头的记录
Select * from code_1th where tel like '415%'
查询中消除值重复的行
Select distinct tel from code_1th
对Text 和 char 可用 like ,其中可用通配符‘%’及‘-’,分别代表多个字符和单个字符。
其他常用查询条件有:(出text类型除外)
大小比较 =、>、<、>=、<=、!=、!>、!<
范围确定 between exp1 and exp2 /net between exp1 and exp2
列表或集合 in (exp1[,exp2,[...]]) not in (...)
谓词 like
多重条件 and or not
2、连接查询
A、等值连接和不等值连接:通过‘=’来比较两个表之间的数据时,称为等值连接;而通过其他比较符时,称为不等值连接
等值连接:
Select * from publishers, authors where publishers.city=authors.city
不等值连接:
B、自然连接: 在连接的目标列中相同名的列只保留一个
Select publishers.pub_id publishers.pub_name, publishers.state, authors.*
From publishers, authors where publishers.city=authors.city
3、子查询
A、表达式子查询
Select au_lname, au_fname from authors where city=
(select city from publishers where pub_name="abcde")
可以使用一切大小比较操作符;在操作符和子查询之间可以使用All 或any。
B、限定谓词子查询
Select pub_name from publishers from publishers
Where pub_id in
(select pub_id from titles where type='abcde')
C、相关查询
相关查询即嵌套查询依赖于外部父查询的值,嵌套查询要重复执行若干次。
Select distinct t1.type from titles t1
Where t1.type in ( select t2.type from titles t2 where t1.pub_id!=t2.pub_id)
4、集函数、分组与排序
A、对查询结果进行聚集处理
聚集函数: Sum([all|distinct] expression),avg([all|distinct] exoression) ,
Count([all|distinct]expression), count(*), max(expression), min(expression)
Select count(*) from titles
B、用Group by 和 having 子句对查询结果分组
Select type ,avg(advance), sum(total_sales) from titles group by type
Select type from titles group by type having count(*) >1
Having 类似于where , 但where 不能用聚集函数。
C、用Order by 对查询结果进行排序
Select type ,avg(price) from titles group by type order by avg(price)
D、Compute 子句
完成基于每一组中的值的聚集运算,聚集值作为一个新行出现在查询结果中。
Select type ,price advance from titles order by type compute sum(price), sum(advance) by type
四、数据库、数据库对象的增、删、改
1、数据库
x 打开数据库
Use tele114
x 创建数据库。拥有创建数据库权利的用户可以创建自己的数据库。
CREATE DATABASE tele114
ON tele114_def01=10,tele114_run01=200,tele114_idx01=200
LOG ON tele114_log01=80
x 删除数据库
Drop database tele114
x 修改数据库
Alter database tele114 on tele11






