SQL 約束解說
2009-04-27 09:29
約束主要包含:
1、not null :用于控制字段的內(nèi)容一定不能為空(NULL)。 ???? 使用方法 :Create table MyTable ?????????????? ( ?????????????????? id varchar(32) not null, ?????????????????? name varchar (32) ?????????????? ) 2、Unique :控件字段內(nèi)容不能反復(fù),一個表同意有多個 Unique 約束。 ????? 在Sql Server、Orcale、MS Access 支持的 加入 Unique 語法 : ???????????? Create table MyTable ??????????????? ( ??????????????????? id varchar(32) not null unique , ??????????????????? name varchar (32) ??????????????? ) ????? 在Sql Server、 My Sql 支持的 加入 Unique 語法 : ???????????? Create table MyTable ??????????????? ( ??????????????????? id varchar(32) not null, ??????????????????? name varchar (32), ??????????????????? unique (id,.....) ???????????????? ) ????? 在Sql Server、Orcale、MS Access、My Sql 都支持的 加入 Unique 語法 : ????????????? Create table MyTable ????????????????? ( ????????????????????? id varchar(32) not null, ????????????????????? name varchar (32), ???????????????????? Constraint uniqueName unique ( UniqueColumn,..... ) ?????????????????? ) ????? 注 :第一種寫法(也就是在字段后面直接加入約束),保證每一個字段數(shù)據(jù)唯一性。 ????????? 另外一種寫法(也就是同一時候加入幾個字段為約束),這個是保證幾個字段數(shù)據(jù) 同一時候 是唯一的,比方 Unique(id,name) 兩個字段為約束,那么當 id 有反復(fù)值,而 name 沒有反復(fù)值的情況下是同意的,僅僅有當兩個字段數(shù)據(jù)都與原數(shù)據(jù)反復(fù)的情況下才是不同意的。 ??? ?? 在Sql Server、Oracle、MS Access 刪除 Unique 約束語法:drop constraint UniqueName; ?? 在My Sql 刪除 Unique 約束語法:drop index UniqueName ; 3、Primary Key :也是用于控件字段內(nèi)容不能反復(fù),但它在一個表僅僅同意出現(xiàn)一個。 ????? 在Sql Server、Orcale、MS Access 支持的 加入Primary Key 語法 : ????????????? Create table myTB1 ???????????? ( ???????????????? id nvarchar(32) not null primary key , ???????????????? name nvarchar(32) ????????????? ) ????? 在Sql Server、My Sql 支持的加入 Primary Key 語法 : ???????????? Create table myTB1 ???????????? ( ???????????????? id nvarchar(32) not null, ???????????????? name nvarchar(32), ???????????????? primary key (id) ???????????? ) ?????? 在Sql Server、Orcale、MS Access、My Sql 支持的 加入Primary Key 語法 : ???????????? Create table myTB1 ???????????? ( ????????????????? id nvarchar(32) not null, ???????????????? name nvarchar(32), ???????????????? constraint PrimaryName primary key (id) ????????????? ) 在Sql Server、Orcale、MS Access、My Sql 表已存在的情況下 ,加入表的Primary Key約束語法 : ????????????? Alter table myTB1 ????????????? ADD Primary Key (id,......) --這種寫法,系統(tǒng)會自己定義約束名稱 ????????????? Alter table myTB1 ????????????? Add Constaint PrimaryName primary key (id) --這種寫法,自己能夠自己定義約束名稱 在Sql Server、Orcale、MS Access 刪除表已存在的 Primary Key 約束的語法: ????????????? Alter table myTB1 ????????????? Drop Constraint PrimaryName 在My Sql 刪除表已存在的 Primary Key 約束的語法: ????????????? Alter table myTB1 ????????????? Drop Primary Key Unique 與 Primary 的同樣之處 :UNIQUE 和 PRIMARY KEY 約束均為列或列集合提供了唯一性的保證。 Unique 與 Primary 的不同之處 :每一個表能夠有多個 UNIQUE 約束,可是每一個表僅僅能有一個 PRIMARY KEY 約束,Unique同意有NULL值,而 Primary key 不同意有NULL值。 注 :在同一個數(shù)據(jù)庫中,就算在不同的表中,約束名稱是不同意同樣的。 4、Foreign Key :FOREIGN KEY 約束用于預(yù)防破壞表之間連接的動作,F(xiàn)OREIGN KEY 約束也能防止非法數(shù)據(jù)插入外鍵列,由于它必須是它指向的那個表中的值之中的一個。 ?? 在Sql Server、My Sql 支持的加入 Foreign Key語法 : ????????????? Create table myTB1 ????????????? ( ?????????????????? id nvarchar(32) not null primary key, ?????????????????? name nvarchar(32), ??????????????????? foreign key(id) references myTB(id) ????????????? ) ?? 在Sql Server、Orcale、MS Access 支持的加入 Foreign Key語法 : ????????????? Create table myTB1 ????????????? ( ?????????????????? id nvarchar(32) not null foreign key references myTB(id), ?????????????????? name nvarchar(32) ????????????? ) ?? 在Sql Server、Orcale、MS Access、My Sql 都支持的加入 Foreign Key語法 : ????????????? Create table myTB1 ????????????? ( ?????????????????? id nvarchar(32) not null primary key, ?????????????????? name nvarchar(32), ??????????????????? Constraint foreignName foreign key(id) references myTB(id) ????????????? ) 在Sql Server、Orcale、MS Access、My Sql 的表已存在情況下,向表加入外鍵約束的語法: ????????????? Alter table myTB1 ????????????? Add foreign key(id) references myTB(id) --這樣寫系統(tǒng)會自己定義約束名稱 ?????????????? Alter table myTB1 ????????????? Add Constraint foreignName foreign key(id) references myTB(id) --這樣寫自己能夠自己定義約束名稱 在Sql Server、Orcale、MS Access 中刪除外鍵約束的語法: ????????????? Alter table myTB1 ????????????? Drop Constraint foreignName; 在My Sql 中刪除外鍵約束的語法: ????????????? Alter table myTB1 ????????????? Drop foreign key foreignName; 5、Check :用于控制字段的值范圍。 ????? 在Sql Server、My Sql 支持的加入 check 語法 : ????????????? Create table myCheck ????????????? ( ?????????????????? id nvarchar(32) not null, ?????????????????? age int not null, ?????????????????? check (age>15 and age <30) ????????????? ) ?????? 在Sql Server、Orcale、MS Access 支持的加入 check 語法 : ????????????? Create table myCheck ????????????? ( ?????????????????? id nvarchar(32) not null, ?????????????????? age int not null check (age>15 and age<30) ????????????? ) ????? 在Sql Server、Orcale、MS Access、My Sql 都支持的加入 check 語法 : ?????????????? Create table myCheck ????????????? ( ?????????????????? id nvarchar(32) not null, ?????????????????? age int not null, ?????????????????? constraint checkName check (age<15 and age>30) ????????????? ) 在Sql Server、Orcale、MS Access、My Sql 的表已存在情況下,向表加入check約束的語法: ????????????? Alter table myCheck ????????????? add check (id='celly'); --這樣定義是系統(tǒng)自己定義 check約束名稱。 ????????????? Alter table myCheck ?????????????? add constraint checkName check(id='celly'); --這樣定義是自己自己定義 check約束名稱。 在 Sql Server、Orcale、MS Access 刪除表已存在的 check 約束的語法: ????????????? Alter table myCheck ????????????? drop constraint checkName 6、Default :用于設(shè)置新記錄的默認值。 ????? 在Sql Server、Orcale、MS Access、My Sql 加入default約束的語法: ????????????? Create table myDefault ????????????? ( ????????????????? id int, ????????????????? name nvarchar(32) default 'celly' ????????????? ) ????? 在My Sql 的已存在表中加入 字段默認值: ?????????????? Alter table myDefault ????????????? Alter [id] set default 0 ????? 在 Sql Server、Orcale、MS Access 的已存在表中加入 字段默認值: ????????????? Alter table myDefault ????????????? Alter column [id] set default 0 ????? 在 My Sql 中刪除字段默認值語法: ????????????? Alter table myDefault ????????????? Alter ColumnName drop default |
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
