怎麼為建立好的表設定主碼和外碼約束

2021-03-12 01:02:49 字數 4145 閱讀 4878

1樓:有點近有點遠

create table student ( sno char(5) not null unique ,

sname char(20) unique ,

s*** char(1) check (s*** in (『m』,』f』)), sage int, sdept char(15) ) ;³主碼的三種指定方式 1) 用not null unique 作為列級約

專束指定 2) 用primary key 作為列級約束指定 (以上兩種僅適用於單屬性主碼的指屬定) 3) 用primary key (《主碼》) 作為表級約束指定

foreign key (sno) references s(sno) , foreign key (**o) references c(**o) )

2樓:匿名使用者

用alter table

sql,如何用sql語句對乙個已經編輯好的表在設定主碼和外碼?

3樓:揮著翅膀de爺

--修改主鍵 alter table tablename --修改表add

constraint tb_primaryprimary key clustered (列名) /*將你要設定為主鍵約束的列*/

--修改約束alter table 銷售表 --修改表add constraint ck_sl --建立檢查約束check (數量 >= 1 and 數量 <= 10000) --新增約束條件

--修改外來鍵alter table 表名

add constraint 外來鍵名稱 foreign key (字段)

references 關係表名 (關係表內字段)

4樓:

sql中的主碼和外碼,屬於索引,sql索引有兩種,聚集索引和非聚集索引,索引主要目的是提高了sql server系統的效能,加快資料的查詢速度與減少系統的響應時間

建立索引的語法:

create [unique][clustered | nonclustered]  index  index_name

on  [with [index_property [,....n]]

說明:unique: 建立唯一索引。

clustered: 建立聚集索引。

nonclustered: 建立非聚集索引。

index_property: 索引屬性。

unique索引既可以採用聚集索引結構,也可以採用非聚集索引的結構,如果不指明採用的索引結構,則sql server系統預設為採用非聚集索引結構。

刪除索引語法:

drop index table_name.index_name[,table_name.index_name]

說明:table_name: 索引所在的表名稱。

index_name : 要刪除的索引名稱。

修改索引的語法:

alter index命令在其用來做什麼方面多少有些欺騙性。截止到現在,alter命令總是與修改物件的定義有關。例如alter表以新增或禁用約束和列。

alter index是不同的-該命令與維護有關,而與結構完全不相干。如果需修改索引的組成,那麼只能drop然後create索引,或者用drop_existing=on選項create並使用索引。

alter index on]

| [[,] fillfactor =

| [[,] sort_in_tempdb = ]

| [[,] ignore_dup_key = ]

| [[,] statistics_nore***pute = ]

| [[,] online = ]

| [[,] allow_row_locks = ]

| [[,] allow_page_locks = ]

| [[,] maxdop =

)]|[ partition =

[ with (< partition rebuild index option>

[,...n])]]]

| disable

| re***anize

[ partition = ]

[ with (lob_***paction = )]

| set ([ allow_row_locks = ]

| [[,] allow_page_locks = ]

| [[,] ignore_dup_key = ]

| [[,] statistics_nore***pute = ]

)}[;]

5樓:匿名使用者

alter table 表名

add constraint 主鍵名稱 primary key (字段)

goalter table 表名

add constraint 外來鍵名稱 foreign key (字段)

references 關係表名 (關係表內字段)go

6樓:匿名使用者

create table table_name

colum_name datatype

[constraint constraint_name] [not] null primary key |unique

7樓:匿名使用者

主鍵你可以在表設計視窗選中要設定主鍵的列選擇設定主鍵即可,外來鍵你可以右鍵,選擇關係,在裡面新增即可。至於設定取值範圍你可以設定乙個約束。

sql語言建立表時候怎麼定義主碼和外碼

8樓:仁昌居士

sql語言建立表時候用primary key(屬性名)定義主碼,用foreign key(屬性名)定義外碼。62616964757a686964616fe58685e5aeb931333433623064

主碼是一種唯一關鍵字,表定義的一部分。乙個表的主碼可以由多個關鍵字共同組成,並且主碼的列不能包含空值。主碼是可選的,並且可在 create table語句中用primary key(屬性名)定義。

將乙個表的值放入第二個表來表示關聯,所使用的值是第乙個表的主鍵值(在必要時可包括復合主鍵值)。此時,第二個表中儲存這些值的屬性稱為外來鍵,用foreign key(屬性名)定義。

如:create table customer

(sid integer,

cid integer,

last_name varchar(30),

first_name varchar(30),

primary key (sid),foreign key(cid 名));

9樓:匿名使用者

creat table student

(sno char(20) not null primary key,

sname......,

.....

)creat table c

(**o int not null primary key,.....

)creat table sc

(.......

constraint fk foreign key (sno,**o)

references student(sno)references c(**o))

關於sql製表當中如何把兩個一起作為主碼,另乙個表引用這個表的主碼當中的乙個作為外碼

10樓:匿名使用者

你現在是2nf,搞成3nf就行了。

方案一:

***pany表中,僅***pany_name作pk -- 很奇怪,為何你的pk要加上city?

方案二:專

***pany表中,增加id欄位屬;works表引用該id欄位

資料庫中怎麼設定"組合主碼外碼

11樓:匿名使用者

外碼主碼 我們在建立資料庫的時候,需要為每張表指定乙個主碼,主碼也叫主鍵。

所謂主碼就是在實體集中區分不同實體的候選碼。

乙個實體集中只能有乙個主碼,但可以有多個候選碼。

外碼,如果乙個關係中的乙個屬性是另外乙個關係中的主碼則這個屬性為外碼。

sql怎樣建立表許可權,怎麼設定SQL資料庫使用者許可權

建立兩個測試表 create table tb1 id int,value int insert tb1 values 1,10 create table tb2 id int,value int insert tb1 values 1,10 go 建立登入 create login liang w...

怎樣建立自己的email,該怎麼設定自己的Email

在 上申請免費的郵箱,不要太多哦 yahoo.163.都有免費的郵箱申請的,開啟網頁就可以知道如何申請了,很簡單的 該怎麼設定自己的e mail 30 設定e mail的方法 1 選擇郵箱 開啟瀏覽器,點選郵箱,選擇郵箱,如 163.2 編輯乙個郵箱名,輸入密碼,點選登入 這是不能登入郵箱的,主要是...

怎麼建立主分割槽,其他的都是邏輯分割槽,vista

常見問題 vista 系統通過diskpart直接建立擴充套件分割槽解決方案 怎麼讓邏輯分割槽變成主分割槽 用u盤啟動盤自帶的分割槽工具,對磁碟重新分割槽,重新劃分,在帶狀的磁碟區域,把你想變的邏輯分割槽設定為主分割槽就可以了 硬碟分割槽,全部建立主分割槽。還是乙個主分割槽,三個邏輯分割槽 mbr分...