列屬性約束,保證表的結構和數據的正確性和穩定性。mysql
總的來講有五種:惟一性和主鍵約束、外鍵約束、檢查約束、空值約束、默認值約束。
五大關鍵詞:UNIQUE和Primary Key, Foreign Key, CHECK, NOT NULL, DEFAULT
sql
規定一個字段的值,是否能夠是null。 null(默認) 或 not null
Null表示沒有值。與任何數據不一樣。表示什麼都沒有。
若是一個列不容許爲空,可是在賦值時,沒有爲該字段賦值,則會出現問題。
Mysql的每條記錄,若是存在能夠爲null的字段,則須要使用一個字節保存哪些字段是空。服務器
create table one( a int not null, b int );
insert into one (a) values(10);
select * from one;
使用default value
來聲明
DEFAULT子句用於爲列指定一個默認,當該字段不存在值時,會被啓用。
默認值必須爲一個常數,不能爲一個函數或一個表達式。
能夠不存在default,可是mysql會判斷:
若是該列能夠爲空,則默認值爲null;若是不能夠,則不設置默認值。markdown
注意:
能夠在處理數據時,使用default,顯示地使用默認值。
有些列是不能有默認值的:Blob,text。
時間戳類型,能夠設置一個特殊的默認值 CURRENT_TIMESTAMP。在當列不存在或者傳遞的值爲null時,使用當前的時間戳。併發
drop table two;
create table two( a int not null default 10, b int not null default 20, c int default 30, d int );
insert into two(a) values(10);
insert into two(b) values(10);
select * from two;
默認值,在沒有爲該字段設置值是啓用。
並且默認值的設置須要使用固定值。ide
常見場景
一個字段不能爲空,並且存在默認值。函數
主鍵(PK,primary key)就是能夠惟一標識某條記錄的字段或者是字段的集合。
主關鍵字是表中的一個或多個字段,要求主鍵的值不能重複,只能有一個主鍵。
若是還存在不重複的記錄,能夠定義成惟一索引,提升檢索效率。
主鍵不能爲空,不設置或者null都會變成not null。性能
主鍵能夠是真實實體的屬性。
可是經常使用的好的解決方案是:
利用一個與實體信息不相關的屬性,做爲惟一標識。
主鍵與業務邏輯不發生關係。只用來標識記錄。(自身保證不衝突,主鍵不屬於當前實體的任何屬性。)編碼
設置主鍵的語法:經過primary key
完成
兩種方案:
一、字段上設置atom
drop table teacher;
create table teacher( t_id int primary key, t_name varchar(10), classname varchar(6), days tinyint unsigned )character set gbk;
insert into teacher values(1,'周杰倫','0324',32);
insert into teacher values(-1,'劉德華','0334',35);
show create table teacher\G
主鍵不能爲空,也不能重複。一個字段是主鍵的話,自動設置不爲空。
二、在定義完字段後,能夠定義
經過這種方式能夠定義多列主鍵。
create table teacher( t_id int, t_name varchar(10), classname varchar(6), days tinyint unsigned, primary key (t_id) );
組合主鍵
drop table teacher;
create table teacher( t_name varchar(10), classname varchar(6), days tinyint unsigned, primary key (t_name,classname) );
注意:
組合主鍵表示一個主鍵內包括多個字段,而不是多個字段都是主鍵;只須要一個惟一標識便可,mysql規定只能存在一個主鍵。
經過修改表來刪除主鍵 alter table tbl_name drop primary key;
經過修改表來增長主鍵Alter table tb add primary key(id);
主鍵字段原則上在插入後,應該不被修改,可是語法上能夠修改,可是修改的值不能與已有值衝突。
主鍵常見的設計:
每一個表都應該存在一個能夠惟一標識的主鍵字段,
最好與實體沒有聯繫,不是實體屬性字段。
爲每條記錄提供給一個惟一的標識。
每次插入記錄時,將每一個字段的值自動增長1。
使用auto_increment
標識。
須要整型,還須要有索引。
一般自動增加是從1開始遞增,可是能夠經過修改表屬性,更改初始值。
表屬性 auto_increment=x
;(若是比已存在的小,則會從已有的最大值新記錄)
drop table teacher;
create table teacher( t_id int primary key auto_increment, t_name varchar(10), classname varchar(6), days tinyint unsigned );
insert into teacher values(null,'周杰倫','0324',32);
insert into teacher values(null,'劉德華','0334',35);
insert into teacher (t_name,classname,days)values('周韋彤','0324',31);
select * from teacher;
自動增加的初始值:是能夠設置的,默認是1。
經過表的選項:auto_increment n
alter table teacher auto_increment 10;
select * from teacher;
insert into teacher values(null,'范冰冰','0334',35);
select * from teacher;
自動增加是否能夠手動插入該列的值?
仍然能夠手動插入。
若是是主鍵的話,該列不能重複,不然,能夠重複。
insert into teacher values(7,’范冰冰’,’0334’,35);
自動增加該列是否能夠更新?
能夠
update teacher set t_id=100 where t_name='范冰冰';
delete from teacher where t_name='范冰冰';
如何刪除自增加的主鍵id?
先刪除自增加,在刪除主鍵。
Alter table tb change id id int(10);//刪除自增加
Alter table tb drop primary key;//刪除主建
Alter table test change id id int;//刪除自增加
Alter table test drop primary key;//刪除主鍵
insert into test values(6,'mark',5);
1:1 一對一
兩個實體表內,存在相同的主鍵字段。
若是記錄的主機值等於另外一個關係表內的記錄的主機主鍵值,則兩條記錄一一對應。
1:n 一對多
一個實體,對應多個其餘實體
例如:一個班級對應多個學生
設計:
在多的那端,增長一個字段,
用於指向該實體所屬的另外的實體的惟一標識,即主鍵。
m:n 多對多
設計:
典型的是利用一箇中間表,表示實體之間的關係。
中間表的每一個記錄,表示一個關係。
一個M:N能夠經過1:M,1:N來實現。
若是一個實體(student)的某個字段(class_id),指向(引用)另外一個實體(class)的主鍵(class_id),就稱student實體的class_id是外鍵。
被指向的實體(class),稱之爲主實體,也叫(父實體)。
負責指向的實體,稱之爲從實體(從表),也叫子實體(子表)。
做用:
用於約束處於關係內的實體。
須要解決的問題?
增長子表記錄時,是否有與之對應的父表記錄。
在刪除或者更新主表記錄時,從表應該如何處理相關的記錄。
如何定義一個外鍵?
在從表上,增長一個字段,指向主表的主鍵。
使用關鍵字 foreign key
drop table if exists class;
create table class( class_id int primary key auto_increment, class_name varchar(20) not null default 'deeplearning' comment '班級名稱' )character set utf8;
set names gbk;
客戶端與服務器端通訊的編碼 character set utf8
這裏的utf8是數據存儲到服務器的編碼。
drop table if exists student;
create table student( stu_id int primary key auto_increment, stu_name varchar(10) not null default '', class_id int, foreign key (class_id) references class(class_id) )character set utf8;
必須主表裏面先有數據,才能在從表中插入數據。
insert into class values (null,'c program design');
insert into student values (null,'張三丰',1);
設置級聯操做
在主表數據發生改變時,與之關聯的從表數據應該如何處理。
主表更新,主表刪除時:使用關鍵字on update
,on delete
來標識。
容許的級聯動做:
cascade關聯操做,若是主表被更新或刪除,那麼從表也會執行相應的操做。
set null,設置null,表示從表不指向任何主表記錄。
restrict,拒絕主表的相關操做。
修改外鍵:
先刪除,再新建,經過修改表完成。 alter table table_name drop foreign key(class_id);
刪除外鍵須要用指定外鍵名稱達到目的,能夠經過在建立外鍵時指定名稱
或者使用mysql默認生成的名稱。 show create table student\G
alter table student drop foreign key student_ibfk_1;
增長外鍵,設置刪除時將從表的數據設置爲null alter table table_name add foreign key(外鍵);
alter table student add foreign key (class_id) references class(class_id) on delete set null;
show create table student\G select * from class;
select * from student;
delete from class where class_id=2;
select * from class;
select * from student;
on delete
增長外鍵,設置刪除時將從表的數據同時也刪除。
insert into class values (1,'c program design');
update student set class_id=1 where stu_id = 1;
alter table student drop foreign key student_ibfk_1;
alter table student add foreign key (class_id) references class(class_id) on delete cascade;
select * from class;
select * from student;
delete from class where class_id=1;
select * from class;
select * from student;
on update
指的是隻有主表的主鍵發生變化,纔會對從表產生影響。
insert into class values (1,'c program design');
insert into student values (1,'張三丰',1);
select * from class;
select * from student;
alter table student drop foreign key student_ibfk_1;
alter table student add foreign key (class_id) references class(class_id) on delete cascade on update restrict;
update class set class_id=2 where class_id=1;
不能操做成功
更新存在關聯的主表數據的主鍵字段
alter table student drop foreign key student_ibfk_1;
alter table student add foreign key (class_id) references class(class_id) on delete cascade on update cascade;
默認的服務器表類型,經過my.ini能夠配置。 default-storage-engine=INNODB
在建立表或編輯表時,能夠指定表的存儲引擎。
利用表屬性:engine 引擎類型
。
經過engine myisam
,engine innodb
在建立時指定。
alter table class engine myisam; 不能操做成功,有外鍵。
注意:外鍵只被innodb存儲引擎所支持。其餘引擎是不支持的。
create table cla***oom( room_id int primary key auto_increment, room_no char(3) )engine myisam character set utf8;
myisam與innodb區別
一、保存文件的方式不一樣
myisam:一個表三個文件。tablename.frm
結構,tablename.myd
數據,tablename.myi
索引
innodb:一個表一個文件。tablename.frm
結構
全部的innodb表,都使用相同的innodb存儲表空間在保存數據和索引。
二、Innodb不少時候是行級鎖,而myisam是表級鎖,innodb的併發高
三、MyIASM支持索引壓縮,而Innodb索引和數據是綁定保存不壓縮,體積大。
四、InnoDB 中不保存表的具體行數,也就是說,執行select count(*) from table
時,InnoDB要掃描一遍整個表來計算有多少行,
可是MyISAM只要簡單的讀出保存好的行數便可。
注意:當count(*)語句包含 where條件時,兩種表的操做是同樣的。
myisam適於插入,查找。
innodb適於更新,刪除。
選擇存儲引擎的依據: 一、性能 二、功能