sql server2008(3)

来源:互联网 发布:linux lnmp安装不成功 编辑:程序博客网 时间:2024/06/05 20:17

从另一个角度对主键进行分类:

1、    自然主键,该列与其他列没有依赖关系;

2、    业务主键,该列数据与其他列数据之间有着依赖的关系;

 

第三种约束:默认约束 defaut

此种约束也是用来限制列数据内容的;

作用是:当向表中添加记录时,如果该列中设置了默认值,没有显式提供插入数据,则以默认值自动填充;如果显式提供了数据,则以提供的数据填充

好处在于省略了相同而重复的内容值。

作一个建筑工人表,包括自然主键,姓名,性别,年龄等信息。

use mydb

go

--建筑工人表worker

if exists(select name from sysobjects  where name='worker'

   and  xtype='u')

drop table worker

go

create table worker

(

   wid  int identity primary key,

   wname  nvarchar(6) not null,

   wsex  nchar(1) not null,

   --..

)

go

向表中添加记录:

insert into worker(wname,wsex) values('张三','男')

insert into worker(wname,wsex) values('李四','男')

--...

insert into worker(wname,wsex) values('张桂兰','女')

go

给性别列wsex添加默认约束,默认值为’男’,以省略显式插入。

重新设计表:

create table worker

(

   wid  int identity primary  key, --主键约束

   wname  nvarchar(6) not null,

   wsex  nchar(1) not nullconstraint df_wsex default '',--默认约束

   --..

)

go

 

重新向表中添加记录:

insert into worker(wname) values('张三')

insert into worker(wname) values('李四')

--...

insert into worker(wname,wsex) values('翠花','')

go

在以上添加的三行记录中,上面两行省略了性别的输入,表示让服务器在插入数据时使用默认值;而最后一行的性别不是默认值,则以显式提供的性别数据为主插入到表中。

在向表中的列设置默认约束时,默认值的设置分两种情况:

1、    静态默认值,如’男’,’女’,18等这些字面常量值;

2、    动态默认值,如getdate()函数

作一个电脑销售表,来测试动态默认值。

if exists(select name from sysobjects  where name='sales' and xtype='u')

drop table sales

go

create table sales

(

   sale_id  int identity primary key,

   ctype  nvarchar(20) not null,

   quantity  int not null,

   sale_time  datetime constraint df_sale_timedefault getdate(),

)

go

约束也是一类数据库对象,它的信息也会保存到系统对象表sysobjects,可以通过查询看到。

select name from sysobjects where xtype='pk' or xtype='d'

go

默认约束是所有约束中,功能最差的一个,因为只有在没有显式提供数据时,这个约束也有作用,一旦显式提供了列值,则此约束没有任何作用。

在特定情况下,如果和其他约束联合起来,会起到一个特别的作用。

和唯一性标识类型作主键,和默认约束结合

重新设计users表:

if exists(select name from sysobjects  where name='users' and xtype='u')

drop table users

go

create table users

(

   userid  uniqueidentifier primary keydefault newid(),

   --将主键约束和默认约束结合起来使用

   username  nvarchar(20) not null,

   password  varchar(20) not null,

)

go

向表中添加记录:

insert into users(username,password)  values('admin','admin')

insert into users(username,password)  values('member','member')

go

删除表worker中性别列wsex上的默认约束:

alter table worker dropconstraint df_wsex

go

 

再向表中添加记录:

insert into worker(wname) values('王五')

go

向表中的wsex列追加默认约束,默认值为’男’:

alter table worker

add

--constraint df_worker_wsex

default ''

for wsex

go

 

第四类约束:唯一性约束,unique

此类约束也是用来表中的某列,它和默认约束一样,都可以修饰多列,即默认约束、唯一性在一个表中可以有多个,而主键约束只有一个;

与主键的区别在于它支持null值,根据唯一性的特点,只能有一个

此类是单表约束中,操作最简单的一个。

使用场合:如果表中的非主键列中,需要限制列数据为不能重复,则将唯一性约束设置到该列上。如用户表中的用户名这一列。

重新设计用户表users:

if exists(select name from sysobjects  where name='users' and xtype='u')

drop table users

go

create table users

(

   userid  uniqueidentifier primary key default newid(),

   --将主键约束和默认约束结合起来使用

   username  nvarchar(20) not null

   /*constraint  unique_useranme*/ unique,

   password  varchar(20) not null,

)

go

向表中添加记录:

insert into users(username,password)  values('admin','admin')

insert into users(username,password)  values('member','member')

go

如果继续添加记录:

insert into users(username,password)  values('member','member')

go

唯一性约束的删除

alter table usersdrop constraint UQ__users__1FCDBCEB

go

向表中追加唯一性约束

alter table users

add

unique(username)

go

 

表的拷贝:

数据库内部拷贝

跨数据库拷贝

数据库中表的拷贝使用into子句,表拷贝语法:

select *

into student_copy

from student

go

表拷贝的优点在于不需要创建目录表的结构(部分)就将源表的结构及行记录信息复制过来。

表拷贝的缺点在于除了自增属性以外,其他所有的约束都会丢失

如果要将新表成为一张安全的表格,则需要在新表中追加所需要的约束。

简单的查询:

查询表的前面n条记录

select top 4 * from sysobjects

go

查询表4行记录中的部分列

select top 4  name,id,xtype,uid,info from sysobjects

go

查询表中从前面数n%的行记录

select top 4.5 percent   name,id,xtype,uid,info from sysobjects

go

 

附加数据库和分离数据库功能。

以上两个功能都是通过系统存储过程来完成的。

从外部介质上拷贝来的数据库文件,是不能直接在当前服务器使用的,即不是可用数据库,最多只能说是“备用”。

--附加pubs数据库

use master

go

execute sp_attach_db @dbname='pubs',

@filename1='C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\northwind.mdf',

@filename2='C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\northwind_log.ldf'

go

 

pubs示例数据库中的用户表

use pubs

go

select name from sysobjects where  xtype='u'

go

northind数据库中的用户表:

use northwind

go

select name from sysobjects where xtype='u'

go

在第三张表中,表名由两个单词组成,用空格间隔。访问时,需要用[ ]将名称括起来,作为一部分,才能访问。

select * from [order details]

go

分离数据库,此功能与删除数据库功能不同。当数据库被分离之后,它不会被删除,只是不能被当前服务器调用了;一旦需要,附加即可

使用master数据库中的系统存储过程sp_detach_db

use master

go

--northwind数据库从当前服务器中分离,由可用变为不可用

execute sp_detach_db  'northwind','true'

go

将示例数据库pubs中的表jobs和authors拷贝到mydb中。

use mydb

go

select * into jobs frompubs.dbo.jobs

go

mydb.dbo.jobs:

分别比较两个表pubs.dbo.jobs和mydb.dbo.jobs结构的区别:

拷贝来的表中,只有自增属性保留下来,所有约束都丢失了

源表jobs中,不但有自增属性,而且有综合的约束存在。

select * into authors frompubs..jobs

go

查找authors表中,属于KS州的作者信息:

select * from authors

where

state='ks'

go

作者中属于mi,in或tn州的作者信息:

select * from authors

where

state='mi'

or

state='in'

or

state='tn'

go

查找Authors表中将姓和名合并起来及其他部分列,取前5行。

select top 5

au_id,au_fname+' '+au_lname as  'au_name',phone,address

from authors

go

查找作者表中姓的首字母为s的作者:

select * from authors

where

--left(au_lname,1)='s'

substring(au_lname,1,1)='s'

go

查找作者的名字超过7个字符的作者:

select * from authors

where

len(au_fname)>7

go

查找作者表中姓的第三个字符为e,并且名的长度大于10的作者:

select * from authors

where

substring(au_lname,3,1)='e'

and

len(au_fname)>10

go

 

0 0