SQL Server創(chuàng)建數(shù)據(jù)庫和數(shù)據(jù)表的相關(guān)約束實現(xiàn)方法
本文分析了SQL Server創(chuàng)建數(shù)據(jù)庫和數(shù)據(jù)表的相關(guān)約束實現(xiàn)方法。分享給大家供大家參考,具體如下:
創(chuàng)建約束語法如下:
CREATE DATABASE [test] ON (NAME=N'test',FILENAME=N'd:\SQL2kt_Data\test.mdf',SIZE=3mb,MAXSIZE=UNLIMITED,FILEGROWTH=1MB) LOG ON (NAME=N'test_log',FILENAME=N'd:\SQL2kt_Data\test_log.ldf',SIZE=1MB,MAXSIZE=2048MB,FILEGROWTH=10%) GO
名詞解釋(翻譯):
constraint
1. 約束;限制[C][(+on)]
legal constraints on the company's activities
對該公司活動法律的限制
2. 強迫;強制[U]
He acted under constraint.
他被迫采取行動。
3. 抑制;拘束;態(tài)度不自然[U]
She showed constraint in the presence of the strangers.
她在陌生人面前顯得很拘束。
4. 拘禁[U]
5. 拘束(或限制)的事物[C]
clustered
聚集成群的
--主外鍵:選中設(shè)置外鍵的列,右鍵--關(guān)系--表和列規(guī)范--點擊帶有“...”的按鈕
--創(chuàng)建帶有主鍵的表,其中,[tid]desc,看上去是倒敘添加數(shù)字,其實不是,添加數(shù)據(jù)是正常的,但是當(dāng)數(shù)據(jù)添加完成后,最后添加的數(shù)據(jù)將第一個被查詢出來。
create table dbo.test3( [tid] [int] identity(100,1) not null, [name] [varchar](100), constraint [pk_tid] primary key clustered( [tid] desc ) )on [primary]
--設(shè)置外鍵
alter table dbo.test4 add fkt foreign key (tid) references(來自) dbo.test3([tid]) ON UPDATE CASCADE ON DELETE CASCADE
--給沒有設(shè)置主鍵的表設(shè)置主鍵,主鍵字段必須為非空。
--刪除主鍵()
alter table test5 drop constraint(限制) pk_id(別名)
--刪除外鍵
alter table test4 drop constraint fkt(別名)
約束
--非空約束
alter table test5 alter column name int not null
--唯一約束
直接在表中建立唯一約束、
constraint 約束別名 unique 列表名
create table dbo.test6( id int not null, vname varchar(20) constraint test6_unique unique nonclustered( vname asc ) )
--check約束
建立check約束
constraint 約束別名 check 約束條件
(修改)
alter table test6 with nocheck add constraint test6_check check(vname != 'shit')
--卸載約束
alter table test6 drop constraint test6_check
--創(chuàng)建修改視圖
create view dbo.view2 as select * from dbo.test6 where dbo.test6.id <= 3;
--看結(jié)果select * from dbo.view2
--刪除試圖
drop view dbo.view2
--主外鍵:選中設(shè)置外鍵的列,右鍵--關(guān)系--表和列規(guī)范--點擊帶有“...”的按鈕
--創(chuàng)建帶有主鍵的表,其中,[tid]desc,看上去是倒敘添加數(shù)字,其實不是,添加數(shù)據(jù)是正常的,但是當(dāng)數(shù)據(jù)添加完成后,最后添加的數(shù)據(jù)將第一個被查詢出來。
create table dbo.test3( [tid] [int] identity(100,1) not null, [name] [varchar](100), constraint [pk_tid] primary key clustered( [tid] desc ) )on [primary]
--設(shè)置外鍵
alter table dbo.test4 add constraint fkt foreign key (tid) references dbo.test3([tid]) ON UPDATE CASCADE ON DELETE CASCADE
--給沒有設(shè)置主鍵的表設(shè)置主鍵,主鍵字段必須為非空。
--刪除主鍵
alter table test5 drop constraint pk_id
--刪除外鍵
alter table test4 drop constraint fkt
約束
//javascript :判空
//邏輯層驗證 :通過java或者c#進行驗證 :登錄名是否正確,唯一性通常在此作,盡可能降低數(shù)據(jù)庫服務(wù)器的負載
//數(shù)據(jù)庫驗證 :唯一約束,check約束
--非空約束
alter table test5 alter column name int not null
--唯一約束
create table dbo.test6( id int not null, vname varchar(20) constraint test6_unique unique nonclustered( vname asc ) )
--給已有的字段創(chuàng)建唯一約束
CREATE UNIQUE iNDEX 索引名 ON 表名稱(字段名)
注意:字段中已有值不能重復(fù)
--check約束
alter table test6 with nocheck add constraint test6_check check(vname != 'shit') alter table test3 with nocheck add constraint test3_check2 check(tname != 'shit' and tname != 'fuck' and tname != 'ohyeah')
--卸載約束
alter table test6 drop constraint test6_check
--默認約束
create table test4( tid int, pwd varchar(20) default '000000' not null )
--給已有的字段增加默認約束
--添加綁定值
--卸載綁定值
補充:數(shù)據(jù)庫中約束
約束的目的:確保表中數(shù)據(jù)的完整性
1. 常見的約束類型:
a) 主鍵約束(Primary Key Constraint):要求主鍵列數(shù)據(jù)唯一,并且不允許為空
b) 唯一約束(Unique Constraint):要求該列唯一,允許為空,但只能出現(xiàn)一個空值。
c) 檢查約束(Check Constraint):某列取值范圍限制、格式限制等,如有關(guān)年齡的約束
d) 默認約束(Default Constraint):某列的默認值,如果男生較多,性別默認為“男”
e) 外鍵約束(Foreign Key Constraint):用于兩表間建立關(guān)系,需要指定引用主表的哪列
2. 約束的格式:
alter table 表名
add constraint 約束名(取名規(guī)則:約束類型_約束字段) 約束類型 具體的約束說明
3. 例子:
alter table stu add constraint pk_stuno primary key(sno)--sno學(xué)號為主鍵 alter table stu add constraint uq_stuid unique(sid)--sid為身份證號,每個身份證號是唯一的 alter table stu add constraint df_sadess default('地址不詳') for saddress--saddress為地址,默認值為地址不詳 alter table stu add constraint ck_sage check(sage between 15 and 40)--sage學(xué)生年齡,要求其值在到之間 alter table scores add constraint fk_st foreign key(sno) references stu(sno) --外鍵約束,主表stu連接從表scores,關(guān)鍵字段sno
創(chuàng)建表間約束并不困難,但是專業(yè)的名詞需要記住
希望本文所述對大家SQL Server數(shù)據(jù)庫設(shè)計有所幫助。
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。