国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > SQL Server > 正文

SQL Server創建數據庫和數據表的相關約束實現方法

2020-07-25 12:48:52
字體:
來源:轉載
供稿:網友

本文分析了SQL Server創建數據庫和數據表的相關約束實現方法。分享給大家供大家參考,具體如下:

創建約束語法如下:

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. 抑制;拘束;態度不自然[U]
She showed constraint in the presence of the strangers.
她在陌生人面前顯得很拘束。

4. 拘禁[U]

5. 拘束(或限制)的事物[C]

clustered

聚集成群的

--主外鍵:選中設置外鍵的列,右鍵--關系--表和列規范--點擊帶有“...”的按鈕

--創建帶有主鍵的表,其中,[tid]desc,看上去是倒敘添加數字,其實不是,添加數據是正常的,但是當數據添加完成后,最后添加的數據將第一個被查詢出來。

create table dbo.test3( [tid] [int] identity(100,1) not null, [name] [varchar](100),constraint [pk_tid] primary key clustered( [tid] desc))on [primary]

--設置外鍵

alter table dbo.test4 add fkt foreign key (tid) references(來自) dbo.test3([tid]) ON UPDATE CASCADE ON DELETE CASCADE

--給沒有設置主鍵的表設置主鍵,主鍵字段必須為非空。

復制代碼 代碼如下:
alter table dbo.test5 with check add constraint pk_id primary key (id)

--刪除主鍵()

alter table test5drop constraint(限制) pk_id(別名)

--刪除外鍵

alter table test4drop constraint fkt(別名)

約束

--非空約束

alter table test5alter 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 test6with nocheck add constraint test6_checkcheck(vname != 'shit')

--卸載約束

alter table test6drop constraint test6_check

--創建修改視圖

create view dbo.view2asselect * from dbo.test6 where dbo.test6.id <= 3;

--看結果select * from dbo.view2
--刪除試圖

drop view dbo.view2
 
--主外鍵:選中設置外鍵的列,右鍵--關系--表和列規范--點擊帶有“...”的按鈕

--創建帶有主鍵的表,其中,[tid]desc,看上去是倒敘添加數字,其實不是,添加數據是正常的,但是當數據添加完成后,最后添加的數據將第一個被查詢出來。

create table dbo.test3( [tid] [int] identity(100,1) not null, [name] [varchar](100),constraint [pk_tid] primary key clustered( [tid] desc))on [primary]

--設置外鍵

alter table dbo.test4 add constraint fkt foreign key (tid) references dbo.test3([tid]) ON UPDATE CASCADE ON DELETE CASCADE

--給沒有設置主鍵的表設置主鍵,主鍵字段必須為非空。

復制代碼 代碼如下:
alter table dbo.test5 with check add constraint pk_id primary key (id)

--刪除主鍵

alter table test5drop constraint pk_id

--刪除外鍵

alter table test4drop constraint fkt

約束

//javascript :判空
//邏輯層驗證 :通過java或者c#進行驗證 :登錄名是否正確,唯一性通常在此作,盡可能降低數據庫服務器的負載
//數據庫驗證 :唯一約束,check約束

--非空約束

alter table test5alter column name int not null

--唯一約束

create table dbo.test6( id int not null, vname varchar(20)constraint test6_unique unique nonclustered( vname asc ))

--給已有的字段創建唯一約束

CREATE UNIQUE iNDEX 索引名 ON 表名稱(字段名)

注意:字段中已有值不能重復

--check約束

alter table test6with nocheck add constraint test6_checkcheck(vname != 'shit')alter table test3with nocheck add constraint test3_check2check(tname != 'shit' and tname != 'fuck' and tname != 'ohyeah')

--卸載約束

alter table test6drop constraint test6_check

--默認約束

create table test4( tid int, pwd varchar(20) default '000000' not null)

--給已有的字段增加默認約束

復制代碼 代碼如下:
alter table test3 add default 0 for tname1

--添加綁定值
復制代碼 代碼如下:
exec sp_bindefault td, 'test2.vname'

--卸載綁定值
復制代碼 代碼如下:
exec sp_unbindefault 'test2.vname'

補充:數據庫中約束

約束的目的:確保表中數據的完整性

1. 常見的約束類型:

a) 主鍵約束(Primary Key Constraint):要求主鍵列數據唯一,并且不允許為空
b) 唯一約束(Unique Constraint):要求該列唯一,允許為空,但只能出現一個空值。
c) 檢查約束(Check Constraint):某列取值范圍限制、格式限制等,如有關年齡的約束
d) 默認約束(Default Constraint):某列的默認值,如果男生較多,性別默認為“男”
e) 外鍵約束(Foreign Key Constraint):用于兩表間建立關系,需要指定引用主表的哪列

2. 約束的格式:

alter table 表名

add constraint 約束名(取名規則:約束類型_約束字段)  約束類型  具體的約束說明
3. 例子:

alter table stu  add constraint pk_stuno primary key(sno)--sno學號為主鍵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學生年齡,要求其值在到之間alter table scores  add constraint fk_st foreign key(sno) references stu(sno)--外鍵約束,主表stu連接從表scores,關鍵字段sno

創建表間約束并不困難,但是專業的名詞需要記住

希望本文所述對大家SQL Server數據庫設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武陟县| 花垣县| 南郑县| 修文县| 白银市| 梅河口市| 涿州市| 泸西县| 黔东| 泽普县| 台中县| 水富县| 蒲城县| 荔波县| 巴东县| 绥宁县| 刚察县| 上蔡县| 绥中县| 阿克陶县| 体育| 满洲里市| 华蓥市| 松阳县| 横山县| 萍乡市| 莫力| 昭觉县| 郴州市| 常山县| 鄂尔多斯市| 永寿县| 正镶白旗| 十堰市| 涟水县| 荔波县| 富宁县| 黄龙县| 健康| 同德县| 电白县|