欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mysql中的各種約束條件深入探討

 更新時(shí)間:2024年05月06日 15:02:10   作者:景天科技苑  
在mysql中對編輯的數(shù)據(jù)進(jìn)行類型的限制,不滿足約束條件的報(bào)錯(cuò),本文給大家分享mysql中的各種約束條件,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧

mysql約束

在mysql中對編輯的數(shù)據(jù)進(jìn)行類型的限制,不滿足約束條件的報(bào)錯(cuò)

unsigned   :    無符號
not null   :       不為空
default    :       默認(rèn)值
unique     :      唯一值,加入唯一索引
(索引相當(dāng)于字典目錄,索引的提出是為了加快速度,一味地亂加索引不會(huì)提高查詢效率)
primary key:    主鍵
auto_increment: 自增加一 ,必須設(shè)置了主鍵才能設(shè)置該參數(shù)
zerofill   :    零填充
foreign key:    外鍵

約束在寫sql時(shí),放在數(shù)據(jù)類型的后面,如下,放在int的后面
字段名 類型 約束

unsigned 無符號

create table t3(id int unsigned);
insert into t3 values(-1); error
insert into t3 values(4000000000); success

設(shè)置無符號位約束,插入負(fù)值就報(bào)錯(cuò)

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

設(shè)置不為空約束,插入空就報(bào)錯(cuò)

NULL值是處于0和1之間的某個(gè)值,他也表示一個(gè)值,只不過這個(gè)值是NULL值,而不是0。
在進(jìn)行計(jì)算的時(shí)候,1與NULL則結(jié)果為NULL。而0與NULL則結(jié)果為0。
1或NULL則結(jié)果為1,0或NULL則結(jié)果為NULL;可見NULL值是介于0和1之間的值。
另外非NULL既不是1也不是0,還是NULL

default : 默認(rèn)值

create table t5(id int not null  , name varchar(11) default "沈思雨" );
insert into t5 values(1,null);
insert into t5(id) values(2);

設(shè)置了默認(rèn)值后,插入時(shí)填入值,就是設(shè)置的值,非全列插入時(shí),不寫該字段的值,就用默認(rèn)值

create table t5_2(id int not null  default "1111" , name varchar(11) default "沈思雨" );
insert into t5_2 values(); # 在values里面不寫值,默認(rèn)使用默認(rèn)值;

unique: 唯一約束

加入唯一索引(索引的提出是為了加快速度,一味地亂加索引不會(huì)提高查詢效率,索引是有一個(gè)文件來存索引)
唯一 可為null 標(biāo)記成: 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變成了多個(gè)null

如果要?jiǎng)h除null的字段,可以用 where 字段 is null 來刪
唯一性約束,可以有多個(gè)null值,不違背唯一性約束

primary key: 主鍵

[ 唯一 + 不為null ] PRI 標(biāo)記數(shù)據(jù)的唯一特征
一個(gè)表中,只能設(shè)置一個(gè)字段為一個(gè)主鍵,unique唯一約束可以設(shè)置多個(gè)
創(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

設(shè)了主鍵,該字段不能重復(fù),不能為空

unique + not null => PRI

create table t8(id int unique not null ,  name varchar(10) default "趙沈陽" );

設(shè)置了唯一性約束,且不為null,功能就跟primary key一樣了

如果沒有設(shè)置primary key,設(shè)置了unique not null ,默認(rèn)把unique +not null 設(shè)置的字段設(shè)為主鍵

primary key / unique + not null => 優(yōu)先把primary key 作為主鍵;

create table t9(id1 int unique not null ,  id2 int primary key );

同時(shí)設(shè)置了unique +not null 和 primary key 。優(yōu)先把primary key 作為主鍵

一個(gè)表只能設(shè)置單個(gè)字段為一個(gè)主鍵;

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);
# 使用默認(rèn)值或者自增插入數(shù)據(jù)
insert into t11 values();

刪除數(shù)據(jù),這是刪除所有數(shù)據(jù)
delete from t11;
刪除數(shù)據(jù) + 重置id
truncate table t11;

主鍵自增,可以用0,null,default占位

刪除一條數(shù)據(jù)后,如果再添加不想主鍵從下一個(gè)開始,需要在添加之前,復(fù)位主鍵
刪除數(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=幾 下次插入時(shí)就從幾開始遞增

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ù)不足,前面補(bǔ)0

到此這篇關(guān)于mysql中的各種約束條件的文章就介紹到這了,更多相關(guān)mysql約束條件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論