SQL語句(二)建立帶主鍵和約束的資料表

2021-09-19 15:48:00 字數 2318 閱讀 2272

建立帶主鍵和約束的表

建立帶組合主鍵和外來鍵的表

- 設為主鍵

cname varchar(40) not

null, --

- 非空

chours int

constraint conshour check(chours >=

0and chours <=

300),

credit

decimal(5,2) constraint conscredit check(credit >=

0and credit <

20),

intro

text

--- text欄位存放一段文字

)

insert

into

course

values('

enp09001

', '

理論力學

', 56, 3, '

研究物體機械運動的基本規律')

-- 設為組合主鍵

foreign

key (snumb) references student(snumb), --

-- (參照完整性)宣告是什麼表中的外來鍵(即:插入資料時,必須要在student和course存在才可以)

foreign

key (cnumb) references

course (cnumb)

)

insert

into

study

values('

10001

', '

enp09001

', 90) --

- snumb和cnumb必須要在student和course存在才可以

select

*from

student

select

*from

course

select

*from study

3.總結:

//新增內容

四類資料完整性: 

實體完整性,確保不出現完全相同的資料記錄

區域完整性, 通過限制資料型別,檢查約束,輸入格式,外來鍵約束,預設值,非空等體現

使用者自定義完整性,使用者自定義由使用者根據實際應用中的需要自行定義。

douzujun 閱讀(

...)

編輯收藏

oracle建立表和約束的SQL語句

1 建立模擬的資料表 1.1.建立學生表student create table student stuid number not null,學生id stuname varchar2 10 not null,名稱 gender varchar2 10 not null,性別 age number ...

SQL初級 建立表和約束

在自己建立的資料庫中建立表 use sb 使用某個資料庫,格式 ues 資料庫名 create table 123 格式 create table 自定義的表名 欄位名一般為有一定意義的英文 names char 15 格式 欄位名 型別 括號裡面的是允許輸入的長度 age int,int型的後面不...

sql語句建立主鍵

對於有資料的表,在企業管理器裡面建立索引時,容易超時,使用sql語句只要在,工具 選項設定超時值為0,就不會超時。sql語句建立有聯合主鍵的表 create table tabcheck check id int not null,check no int not null,startdate da...