🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.bbs.comp.database
Article #932

LearnNote: SQL Server - SQL Server��ݿ�������

#932
From: Galaxy��VOLUNTEERS
Date: Sun, 10 Aug 2008 12:42
114 lines
4207 bytes
SQL Server��ݿ������� 

1. ����
��ݿ����������ָ��ݵ���ȷ�Ժ������ԡ�
������Լ���������õĶ�������ǹ�ϵ��Ԫ�顢�����֡�
������Լ�������漰�������������״̬�����Ǿ�̬�ģ�Ҳ�����Ƕ�̬�ġ� 


a. �м�Լ��
���ͣ���ʽ��ȡֵ��Χ����ֵ��
b. Ԫ��Լ��
�ֶμ���ϵ��Լ��
c. ��ϵԼ��
������
ʵ��������Լ��̬����Ҫ��
����������Լ��̬����Ҫ��
��������Լ��̬��
ͳ��Լ��̬��
����һ���ԣ���̬��
ԭ���ԣ���̬�� 


2.SQL Server��������Լ��
SQL Server֧������������Լ���������֮ǰ���ܵIJ�̫һ��
1��ʵ��������
2�����������ԣ������������ԣ�
3���û��Զ��������� 


2.1 ʵ�������Ժ�����
ͨ��Primary Key��PK��ʵ��Ψһ�ҷǿ�
���������
create table testtable1
{
col1 char(10) primary key
};
��
create table testtable1
{
col1 char(10),
primary key(col1)
}; 


2.2 ���������Ժ����
�������ò����ڵ�ֵ
��ֹ���²�����
a. ����ûֵ��������ֵ
b. �������ֵ
c. ����ɾ��ֵ�����ֵ��ɾ��
��������
a. references
create table testtable2
{
col1 char(10) references testtable2(col1)
}
��
create table testtable2
{
col1 char(10),
foreign key(col1) references testtable2(col1)
} 


ʵ�ֲ��������Բ��ԣ�
a. restrict�����Ʋ��ԣ�
Υ�����ܾ�
b. cascade���������ԣ�
ͨ����REFERENCES�Ӿ�����ON DELETE �� ON UPDATE�Ӿ�ʵ�֣�
[ ON DELETE { CASCADE | NO ACTION } ]
[ ON UPDATE { CASCADE | NO ACTION } ] 
Ĭ��ΪNO ACTION�����ع�������
CASCADEΪ׷�ݵ�ִ���޸ĵı?����һ���ı�Ҳִ����ͬ�IJ����� 


2.3 �û��Զ���������Լ��
SQL Server�ṩ�ǿ�Լ������Ե�CHECKԼ���Ԫ���CHECKԼ����������ʵ��
�û���������Ҫ��
CHECK(����)
create table testtable3
{
col1 int,
col2 int,
check(col1+col2<10)
} 


����Լ��
alter table add constraint
eg:
create table testtable4
{
col1 char(10),
contraint PK_col1 primary key(col1)
} 


�ο����ϣ�
��ݿ�������
http://www.cs.xmu.edu.cn/education/fine_courses/database/%BE%AB%C6%B7%BF%CE%B
3%CC%BD%A8%C9%E8%C4%DA%C8%DD/3_%CD%F8%C2%E7%B0%E6cai/content/10/10.0.0.htm 


������Լ���SQL���� 
http://www.cnblogs.com/2007/archive/2007/08/24/868428.html 

--
I thought what I'd do was, I'd pretend I was one of those deaf-mutes
Or should I?

You know what I��d like to be? I mean if I had my goddam choice, I��d just be
the catcher in the rye and all.

�� ��Դ:����ˮ˼Դ bbs.sjtu.edu.cn��[FROM: 58.35.146.84]
Technical Details
Message-ID: <4bOdH2$b0e@bbs.sjtu.edu.cn>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!news.neu.edu.cn!news.shec6.edu.cn!SJTU

Show Full Headers
Complete Article Headers
X-Google-Thread: 101948,e7267318de76de69,start
X-Google-Attributes: gid101948,domainid0,public,usenet
X-Google-Language: Chinese,GB
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!news.neu.edu.cn!news.shec6.edu.cn!SJTU
From: Dem.bbs@bbs.sjtu.edu.cn (Galaxy��VOLUNTEERS)
Newsgroups: cn.bbs.comp.database
Subject: LearnNote: SQL Server - SQL Server��ݿ�������
Date: 10 Aug 2008 12:42:37 GMT
Organization: ��ˮ˼Դ
Message-ID: <4bOdH2$b0e@bbs.sjtu.edu.cn>
Mime-Version: 1.0
Content-Type: text/plain; charset="gb2312"
Content-Transfer-Encoding: 8bit
X-Filename: database/M.1218372146.A
Lines: 113