mysql 資料完整性

2021-08-20 06:08:22 字數 4218 閱讀 3063

** 資料完整性。(資料完整性指的是儲存在資料庫中的資料的一致性和準確性)

資料完整性分類:

1、實體完整性

primary key約束

unique約束    (唯一值)

自增特性 auto_increment

2、域完整性

資料型別

非空約束  not null

check約束 mysql不支援

default約束

3、參照完整性

foreign key約束

4、使用者自定義完整性

儲存過程和觸發器

** 約束。

1、not null約束

功能:限制使用者向表中插入資料時,某列必須插入值,不允許為空

新增約束的方法:在列的定義後面直接新增關鍵字not null。(不寫預設為null)

示例:create table student

(stuid char(5) not null,

stuname varchar(10) not null,

stu*** enum('男', '女') #此列允許不插入值

)驗證not null約束的功能

另一種新增非空約束的方法:、

create table t1(tid number);

alter table t11 modify tid char(5) not null; 

(可以既改變型別,又新增約束)

2、default約束

當insert語句沒有指定值時,default約束會在列中輸入乙個預設值。

定義default約束的語法 :

default constraint_expression

mysql中的預設值約束有乙個限制,即:預設值只能是乙個常量,不能象oracle那樣允許函式。

只有乙個例外:current_timestamp

下面這些語句是等效的: 

create table t (ts timestamp);

create table t (ts timestamp default current_timestamp 

on update current_timestamp);

3、primary key約束

primary key約束用來實現實體完整性

確保特定的列中不允許重複,且不許為null

primary key約束定義了表的主鍵,它可以唯一地標識一行。

每個表只能定義乙個primary key約束

可以定義在一列或多列上

定義primary key約束的語法 :

[constraint constraint_name]

primary key  

示例:drop table t_doctor;

create table t_doctor

(doc_id char(5) primary key,

doc_name char(8) not null,

*** char(2),

phone varchar(20)

);* 病人表,注意:不支援注釋符--

create table t_patient

(pat_id char(5),

pat_name char(8) not null,

*** char(2),

pid char(18), 

phone varchar(20),

constraint pk_patient_id primary key(pat_id)

constraint pk_patient_id 也可以省略

4、unique約束

unique約束也用來實現實體實整性

unique約束表明同一列的任意兩行都不能具有相同值。

可以插入多個null(與其他的dbms不一樣)

若表中已有一主鍵,但又想保證其他的列也是唯一的,可以定義unique約束。

乙個表中可以有多個unique約束

定義unique約束的語法 :

[constraint constraint_name]

unique  

在建立表時新增unique約束 

drop table t_patient;

create table t_patient

(pat_id char(5) primary key,

pat_name char(8) not null,

pid char(18) unique,

/* constraint un_pid unique(pid) */

);5、check約束

check約束通過對輸入到特定列的值設定檢查條件,將輸入資料限制為指定的值

可以在單列或多列上建立check約束 

定義check約束的語法 :

在建立表時,也可以為表新增check約束:

create table t_patient

(pat_id char(5) primary key,

pat_name char(8) not null,

*** char(2) check(***='男' or ***='女'),

pid char(18),

phone varchar(20),

constraint ck_patient_pid check(length(pid)=18 or length(pid)=15)

); 6、foreign key約束

foreign key約束定義了表之間的一致性關係,用於強制參照完整性。

foreign key約束定義了對同乙個表或其他表的列的引用,這些列具有primary key或者unique約束。 

定義foreign key約束的語法 :

[constraint constraint_name]

[foreign key(column_name [,…])]

references ref_table[(ref_column_name [,…])]

[on delete ]

[on update ]

drop table t_record; 

create table t_record

(pat_id char(5) references t_patient(pat_id),  #mysql中這樣寫建立不上約束

admission_date date,

doc_id char(5),

diagnosis nvarchar(500),

foreign key(pat_id) references t_patient(pat_id),

foreign key(doc_id) references t_doctor(doc_id)

)7、級聯引用完整性

[on delete ]

[on update ]

cascade使用級聯,刪除或更新主表資料時,同時刪除從表中的資料或將從表中的資料自動更新。

set null選項則在刪除或更改主表資料時,自動將從表中相應資料設定為空。

no action不使用級聯 

alter table t_record

add constraint fk_record_docid 

foreign key(doc_id) references t_doctor(doc_id)

on delete cascade;

alter table t_record

add constraint fk_record_patid 

foreign key(pat_id) references t_patient(pat_id)

on delete set null;

8、刪除約束

alter table table_name 

drop primary key #刪除主鍵約束

| drop index index_name #可用此句刪除唯一約束

| drop foreign key fk_symbol #刪除外來鍵約束

not null與default約束以修改列的方式實現

check約束根本就沒有儲存

alter table t_record drop foreign key fk_record_docid;

alter table t_record drop foreign key fk_record_patid;

9、檢視表中的約束

show create table t_record;

MySQL資料完整性(實體完整性 域完整性)

資料完整性 為保證插入到資料庫中的資料是正確的,防止使用者輸入錯誤的資料 分為實體完整性 域完整性 參照完整性 下節再說 1 實體完整性 實體指的是表中的一行,一行記錄對應乙個實體 通過主鍵實現 主鍵 關鍵字 primary key 特點 不能為null,並且唯一。邏輯主鍵 推薦 例如id,不代表實...

mysql 完整性 詳解MySQL 資料完整性

資料完整性分為 實體完整性,域完整性,參考完整性。參照完整性指的就是多表之間的設計,主要使用外來鍵約束。多表設計 一對多 多對多 一對一設計 一 實體 行 完整性 實體完整性通過表的主鍵來實現。使用主鍵來表示一條記錄的唯一,且不為空 語法 primary key 主鍵分類 邏輯主鍵 例如id,不代表...

mysql 域完整性 Mysql之資料完整性約束

mysql之ddl操作 四 資料完整性約束 實體完整性 域完整性 引用完整性 自定義完整性 1 實體完整性 主鍵約束 唯一約束 主鍵自增 1 主鍵約束 特點 唯一的,不能為空 關鍵字 primary key 新增約束語法 alter table 表名 add constraint 約束名 prima...