MySQL---約束

約束:
1) not null (非空), 只能用於列級約束
2) unique(唯一)
3) primary key(主鍵--非空且唯一) 表中只允許一個主鍵
4) foreign key(外鍵--引用別的表的數據)
5) check, 只能用於列級約束
6) default 缺省值

 語法描述:
         對於列級約束,直接寫在列定義的後邊就行。
         列定義 not null (非空約束)
         表級約束,在列定義完後,使用關鍵字
         約束關鍵字(列名)
 
 當主鍵約束爲兩個列時只有兩個列連接值一樣的時候纔會違反約束。


小例子:
create table test(
	id int auto_increment,
	name varchar(50) not null,
	idcard varchar(20),
	address varchar(50),
	unique(idcard),
	primary key(id, name)
);

當違反約束的時候插入不成功,會報錯。

測試插入情況:
insert into test(
	id,
	name,
	idcard,
	address
) values (
	1,
	'aaa',
	'12213123',
	'beijing'
), (
	1,
	'bbbb',
	'232423',
	'shanghai'
);

下面的記錄無法插入,違反主鍵約束
insert into test(
	id,
	name,
	idcard,
	address
) values (
	1,
	'aaa',
	'12213123',
	'beijing'
);

違反的是非空約束
insert into test(
	id,
	name,
	idcard,
	address
) values (
	2,
	null,
	'12213123',
	'beijing'
);

違反唯一約束
insert into test(
	id,
	name,
	idcard,
	address
) values (
	2,
	'cccc',
	'12213123',
	'beijing'
);

在非定義的時候給某表添加,丟棄表級的唯一約束
語法描述:
       添加
       alter table 表名
       add unique(列名);
 
       丟棄
       alter table 表名
       drop index `列名`;
       或者
       drop key `列名`;
 
        使用modify不能丟棄表級約束

小例子:

	爲teachers表的name列添加表級的唯一約束
	alter table 
		teachers
	add unique(name);

	丟棄表級唯一約束
	alter table 
		teachers
	drop index `name`;
	和 
	alter table 
		teachers
	drop key `name`;

	以下語句無法丟棄表級約束
	alter table 
		teachers 
	modify 
		name varchar(25);

在非定義的時候給某列添加列級的非空約束
語法描述:
     添加
     alter table 表名
     change
           列定義 設置約束;(not null)
 
     丟棄
     alter table 表名
     modify
         列定義 設置約束;(null)

小例子:
	
	爲phone列添加列級的非空約束
	alter table 
		teachers
	change 
		phone phone varchar(15) not null;
	
	丟棄列級約束
	alter table 
		teachers 
	modify 
		phone varchar(15) null;

外鍵約束
     保證子表中的記錄中的某列要和母表中的某列一致。
 +----------+-----------------+------+-----+---------+----------------+
| Field    | Type            | Null | Key | Default | Extra          |
+----------+-----------------+------+-----+---------+----------------+
| id       | int(11)         | NO   | PRI | NULL    | auto_increment |
| name     | varchar(30)     | YES  |     | NULL    |                |
| gender   | enum('男','女') | YES  |     | 男      |                |
| age      | int(11)         | YES  |     | NULL    |                |
| phone    | varchar(15)     | YES  |     | NULL    |                |
| birthday | datetime        | YES  |     | NULL    |                |
| home     | varchar(200)    | YES  |     | NULL    |                |
+----------+-----------------+------+-----+---------+----------------+
 語法描述:
       [constraint 外鍵名] foreign key(子表的引用列) references 母表(母表的主鍵);

小例子:
	教師表,班主任要引用老師表的主鍵。
	create table classes (
		id int auto_increment,
		name varchar(20) unique,
		master int,
		classroom int not null,
		begindate datetime,
		primary key(id),
		foreign key(master) references teachers(id)
	);
	
	+----+--------+--------+------+--------------+---------------------+--------------+
| id | name   | gender | age  | phone        | birthday            | home         |
+----+--------+--------+------+--------------+---------------------+--------------+
|  1 | 張老師 | 男     |   40 | 135234234234 | 1976-05-02 10:01:01 | 北京通州32號 |
|  2 | 李老師 | 女     |   40 | 135234234234 | 1976-05-02 10:01:01 | 北京昌平32號 |
|  3 | 劉老師 | 男     |   30 | 135234234234 | 1986-05-02 10:01:01 | 北京朝陽32號 |
|  4 | 閆老師 | 女     |   25 | 124234234    | 1990-01-02 00:00:00 | 北京西城     |
+----+--------+--------+------+--------------+---------------------+--------------+
	插入4值時,需要先到母表中進行檢索,若可以找到數據,插入成功
	insert into classes 
	(
		name,
		master,
		classroom,
		begindate
	) values (
		'H50826',
		4,
		232,
		now()
	); 

	插入400值時, 需要先到母表中進行檢索, 如果沒有找到數據,則插入失敗
	insert into classes 
	(
		name,
		master,
		classroom,
		begindate
	) values (
		'JavaEE0826',
		400,
		333,
		now()
	); 
	插入成功
	insert into classes 
	(
		name,
		master,
		classroom,
		begindate
	) values (
		'Android0826',
		4,
		333,
		now()
	); 
	插入成功
	insert into classes 
	(
		name,
		master,
		classroom,
		begindate
	) values (
		'JavaEE1117',
		1,
		101,
		'2016-7-17'
	); 

在非定義的時候添加,丟棄外鍵約束語法
 alter table 表名
 add [constraint stu_fk] foreign key(classid) references class(id);
 
 alter table 表名
 drop foreign key 外鍵名;


測試外鍵約束
	當母表中的記錄被引用時, 不能刪除這條記錄
	先把子表中的相應記錄刪除,才能再刪除這條記錄

	insert into 
		teachers 
	(
		id,
		name,
		gender,
		age,
		phone,
		birthday,
		home 
	) values (
		4,
		'奈老師',
		'女',
		40,
		'135234234234',
		'1976-5-2 10:1:1',
		'北京京州32號'
	);
	
	把子表的外鍵約束丟棄
	alter table classes 
	drop foreign key classes_ibfk_1;

設置級聯刪除
 語法描述:
     設置外鍵,在刪除母表記錄時,子表記錄會被級聯,也被刪除
     alter table 表名
     add [constraint 鍵名] foreign key(子列引用的列) references 母表(母表主鍵) on delete cascade on update cascade;
  
     設置外鍵,在刪除母表記錄時,子表記錄中的引用列被置爲null
     alter table 表名
      add [constraint 鍵名] foreign key(子列引用的列) references 母表(母表主鍵) on delete set null on update set null;

小例子:
		級聯刪除
		alter table classes 
		add constraint my_fk foreign key(master) references teachers(id) on delete cascade on update cascade; 
		級聯置null
		alter table classes 
		add constraint my_fk foreign key(master) references teachers(id) on delete set null on update set null; 
	

limit關鍵字,分頁
     limit 數值1,數值2 數值1表示略過的記錄,數值2表示顯示的記錄數。
     分頁 : 略過當前頁以前的所有記錄, 顯示一頁該顯示的記錄數
     select * from 表名 where limit (pageNo - 1) * pageSize, pageSize;



  



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章