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

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

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

mysql約束

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

unsigned   :    無符號(hào)
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 無符號(hào)

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

設(shè)置無符號(hào)位約束,插入負(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)文章

  • 當(dāng)面試官問mysql中char與varchar的區(qū)別

    當(dāng)面試官問mysql中char與varchar的區(qū)別

    這篇文章主要以聊天形式圖片的添加,將面試官面試真實(shí)場景體現(xiàn)出來,好奇的朋友不要錯(cuò)過奧
    2021-08-08
  • mysql 主從服務(wù)器的簡單配置

    mysql 主從服務(wù)器的簡單配置

    首先呢,需要有兩個(gè)mysql服務(wù)器。如果做測試的話可以在同一臺(tái)機(jī)器上裝兩個(gè)mysql服務(wù)程序,注意要兩個(gè)運(yùn)行程序的端口不能一樣。我用的是一個(gè)是默認(rèn)的3306,從服務(wù)器用的是3307端口。
    2009-05-05
  • mysql 批量查詢?nèi)∶恳唤M最新一條數(shù)據(jù)

    mysql 批量查詢?nèi)∶恳唤M最新一條數(shù)據(jù)

    根據(jù)車牌號(hào)查詢最新的一條交車記錄的‘合同號(hào)’ ,這里只需要查詢‘合同號(hào)’這個(gè)字段,這篇文章主要介紹了mysql 批量查詢?nèi)∶恳唤M最新一條數(shù)據(jù),需要的朋友可以參考下
    2024-02-02
  • MySql索引和索引創(chuàng)建策略

    MySql索引和索引創(chuàng)建策略

    這篇文章主要介紹了MySql索引和索引創(chuàng)建策略,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)又是幫助
    2022-08-08
  • MySQL慢查詢查找和調(diào)優(yōu)測試

    MySQL慢查詢查找和調(diào)優(yōu)測試

    MySQL慢查詢查找和調(diào)優(yōu)測試,接下來詳細(xì)介紹,需要了解的朋友可以參考下
    2013-01-01
  • SQL實(shí)現(xiàn)LeetCode(178.分?jǐn)?shù)排行)

    SQL實(shí)現(xiàn)LeetCode(178.分?jǐn)?shù)排行)

    這篇文章主要介紹了SQL實(shí)現(xiàn)LeetCode(178.分?jǐn)?shù)排行),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • 解讀mysql的for update用法

    解讀mysql的for update用法

    這篇文章主要介紹了解讀mysql的for update用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • MySQL中ONLY_FULL_GROUP_BY的使用小結(jié)

    MySQL中ONLY_FULL_GROUP_BY的使用小結(jié)

    ONLY_FULL_GROUP_BY是MySQL中的一個(gè)重要SQL模式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-12-12
  • MySQL 分表分庫怎么進(jìn)行數(shù)據(jù)切分

    MySQL 分表分庫怎么進(jìn)行數(shù)據(jù)切分

    這篇文章主要介紹了MySQL 分表分庫怎么進(jìn)行數(shù)據(jù)切分,幫助大家更好的理解和學(xué)習(xí)使用MySQL,感興趣的朋友可以了解下
    2021-03-03

最新評(píng)論