例: 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
(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 tele114_run02=100, tele114_idx02=100
2、表 x 建表 create table spec_code ( No tinyint not null, /*特编号(0--99)*/ Name varchar(50) null, /*名称*/ Addr varchar(50) null, /*地址*/ Tel varchar(8) null, /*电话号码*/ StaffNo varchar(4) not null /*录入员工号*/ ) on segrun /*将表放在segrun段上*/ x 删除表 Drop tabel tele114_1th x 修改表,在表中增加新的列(用这个命令增加的列必须允许null值) Alter table friends_etc add country varchar(20 ) null x 表级或列级约束 Create table my_publishers (pub_id char(4), Pub_name carchar(40). Constraint my_chk_constraint Check (pub_id in ('1389','0736','0877')) Or (pub_name not like 'bad news books')) x 指定默认值 Create table ny_titles (title_id char(6t), Title varchar(80), Price money default null, Total_sales int default (0)) x 指定Unique 和primary key 约束 Primary&nb