mysql中的各種約束條件深入探討
mysql約束
在mysql中對編輯的數(shù)據(jù)進行類型的限制,不滿足約束條件的報錯
unsigned : 無符號 not null : 不為空 default : 默認值 unique : 唯一值,加入唯一索引 (索引相當于字典目錄,索引的提出是為了加快速度,一味地亂加索引不會提高查詢效率) primary key: 主鍵 auto_increment: 自增加一 ,必須設置了主鍵才能設置該參數(shù) zerofill : 零填充 foreign key: 外鍵
約束在寫sql時,放在數(shù)據(jù)類型的后面,如下,放在int的后面
字段名 類型 約束
unsigned 無符號
create table t3(id int unsigned); insert into t3 values(-1); error insert into t3 values(4000000000); success
設置無符號位約束,插入負值就報錯
not null : 不為空
create table t4(id int not null , name varchar(11)); insert into t4 values(1,"張宇"); insert into t4 values(null,"張宇"); error insert into t4(name) values("李四"); error
設置不為空約束,插入空就報錯
NULL值是處于0和1之間的某個值,他也表示一個值,只不過這個值是NULL值,而不是0。
在進行計算的時候,1與NULL則結(jié)果為NULL。而0與NULL則結(jié)果為0。
1或NULL則結(jié)果為1,0或NULL則結(jié)果為NULL;可見NULL值是介于0和1之間的值。
另外非NULL既不是1也不是0,還是NULL
default : 默認值
create table t5(id int not null , name varchar(11) default "沈思雨" ); insert into t5 values(1,null); insert into t5(id) values(2);
設置了默認值后,插入時填入值,就是設置的值,非全列插入時,不寫該字段的值,就用默認值
create table t5_2(id int not null default "1111" , name varchar(11) default "沈思雨" ); insert into t5_2 values(); # 在values里面不寫值,默認使用默認值;
unique: 唯一約束
加入唯一索引(索引的提出是為了加快速度,一味地亂加索引不會提高查詢效率,索引是有一個文件來存索引)
唯一 可為null 標記成: UNI
create table t6(id int unique , name char(10) default "趙萬里" ); insert into t6(id) values(1); insert into t6(id) values(1); error insert into t6(id) values(null); insert into t6(id) values(null); # id變成了多個null
如果要刪除null的字段,可以用 where 字段 is null 來刪
唯一性約束,可以有多個null值,不違背唯一性約束
primary key: 主鍵
[ 唯一 + 不為null ] PRI 標記數(shù)據(jù)的唯一特征
一個表中,只能設置一個字段為一個主鍵,unique唯一約束可以設置多個
創(chuàng)建主鍵
create table t7(id int primary key , name varchar(10) default "趙沈陽"); insert into t7(id) values(1); insert into t7(id) values(1); error insert into t7(id) values(null); error
設了主鍵,該字段不能重復,不能為空
unique + not null => PRI
create table t8(id int unique not null , name varchar(10) default "趙沈陽" );
設置了唯一性約束,且不為null,功能就跟primary key一樣了
如果沒有設置primary key,設置了unique not null ,默認把unique +not null 設置的字段設為主鍵
primary key / unique + not null => 優(yōu)先把primary key 作為主鍵;
create table t9(id1 int unique not null , id2 int primary key );
同時設置了unique +not null 和 primary key 。優(yōu)先把primary key 作為主鍵
一個表只能設置單個字段為一個主鍵;
create table t10(id1 int primary key , id2 int primary key ); error
auto_increment: 自增加一
一般配合 主鍵或者unique 使用
create table t11(id int primary key auto_increment , name varchar(255) default "敬文棟"); insert into t11 values(1,"張三"); insert into t11 values(null,"李四"); insert into t11(id) values(null); # 使用默認值或者自增插入數(shù)據(jù) insert into t11 values();
刪除數(shù)據(jù),這是刪除所有數(shù)據(jù)
delete from t11;
刪除數(shù)據(jù) + 重置id
truncate table t11;
主鍵自增,可以用0,null,default占位
刪除一條數(shù)據(jù)后,如果再添加不想主鍵從下一個開始,需要在添加之前,復位主鍵
刪除數(shù)據(jù)后,執(zhí)行下面的sql
如果是中途刪除,先查看一下目前的auto_increment
show create table student; | student | CREATE TABLE `student` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `age` int NOT NULL, `birthday` date DEFAULT NULL, `is_del` tinyint DEFAULT '0', `height` decimal(3,2) DEFAULT NULL, `cls_id` varchar(6) NOT NULL, PRIMARY KEY (`id`), KEY `fk_class` (`cls_id`), CONSTRAINT `fk_class` FOREIGN KEY (`cls_id`) REFERENCES `class` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8 |
AUTO_INCREMENT=幾 下次插入時就從幾開始遞增
ALTER TABLE (表名) AUTO_INCREMENT = 1;
zerofill : 零填充 (配合int使用,不夠5位拿0來填充)
create table t12(id int(5) zerofill); insert into t12 values(1234567);
位數(shù)超了之后,按寫入的數(shù)據(jù)直接插入
insert into t12 values(12);
位數(shù)不足,前面補0
到此這篇關(guān)于mysql中的各種約束條件的文章就介紹到這了,更多相關(guān)mysql約束條件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
當面試官問mysql中char與varchar的區(qū)別
這篇文章主要以聊天形式圖片的添加,將面試官面試真實場景體現(xiàn)出來,好奇的朋友不要錯過奧2021-08-08mysql 批量查詢?nèi)∶恳唤M最新一條數(shù)據(jù)
根據(jù)車牌號查詢最新的一條交車記錄的‘合同號’ ,這里只需要查詢‘合同號’這個字段,這篇文章主要介紹了mysql 批量查詢?nèi)∶恳唤M最新一條數(shù)據(jù),需要的朋友可以參考下2024-02-02SQL實現(xiàn)LeetCode(178.分數(shù)排行)
這篇文章主要介紹了SQL實現(xiàn)LeetCode(178.分數(shù)排行),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08在MySQL數(shù)據(jù)庫中復位根用戶的密碼的方法
2007-12-12MySQL中ONLY_FULL_GROUP_BY的使用小結(jié)
ONLY_FULL_GROUP_BY是MySQL中的一個重要SQL模式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-12-12