MySQL資料庫筆記四:MySQL的約束

来源:https://www.cnblogs.com/sl0309/archive/2019/07/09/11160872.html
-Advertisement-
Play Games

<1>概念 <2>使用場景 <3>分類 1.default 2.not null 3.unique 4.主鍵約束 5.auto_increment:自增長約束 6.unsigned:無符號約束 7.zerofill:零填充約束 8.外鍵約束 總結:資料庫的約束 ...


<1>概念

是一種限制,它是對錶的行和列的數據做出約束,確保表中的數據的完整性和唯一性。

<2>使用場景

創建表的時候,添加約束

<3>分類

1. default:預設約束,域完整性
2. not null:非空約束,域完整性
3. unique:唯一約束,實體完整性
4. primary key:主鍵約束,實體完整性
5. foreign key:外鍵約束,參照完整性
6. check:檢查約束(MySQL不支持),域完整性
7. atuo_increment:自增長約束
8. unsigned:無符號約束
9. zerofill:零填充約束
資料庫中有三個完整性:域、實體、參照完整性

域(列)完整性:
    域完整性是對數據表中欄位屬性的約束。

實體完整性在MySQL中實現:
    通過主鍵約束和候選鍵約束實現的

參照完整性   
    也就是說是MySQL的外鍵

1.default

<1>概念
        指定某列的預設值,插入數據的時候,此列沒有值,則用default指的的值來填充。

<2>添加
        在創建表的時候添加   : create .....default
        create table t1(
            id int default 1,
            name varchar(20) default '老王'
        );

    通過alter語句添加:alter....modify/change.........
        alter table t1 modify id int default 2;
        alter table t1 change name name varchar(32) default '大拿';   

<3>刪除: alter.....modify/change
        alter table t1 modify id int;
        alter table t1 change name name varchar(32);

2.not null

<1>概念
    指定某列的值不為空,在插入數據的時候必須非空 "" 不等於 null ,0不等於 null

<2>添加
    在創建表的時候添加   : create .....not null
        create table t1(
            id int not null,
            name varchar(20) not null
        );

    通過alter語句添加:alter....modify/change.........
        alter table t2 modify id int;
        alter table t2 change name name varchar(32) null;   

<3>刪除: alter.....modify/change

3.unique

<1>概念
    指定列或者列組合不能重覆,保證數據的唯一性
    不能不限重覆的值,但是可以有多個null
    同一張表可以有多個唯一的約束

<2>添加唯一約束
    在創建表的時候添加   : create .....unique
        create table t3(
            id int unique,
            name varchar(32) not null
        );

        insert t3 value (1,'老王');
        insert t3 value (1,'老李'); -- id唯一約束,添加異常

        create table t3(
            id int,
            name varchar(32) not null,
            constraint id_unique unique(id,name)   --  添加複合約束
        );

    通過alter語句添加:alter....modify/change.........         /  alter .... add unique.....
        alter table t3 modify id int unique;
        alter table t3 add unique(name);
        alter table t3 add constraint un_id unique(id);

<3>刪除唯一約束:alter .... drop...index 名稱            / drop index  名稱 on 表名 
        alter table t3 drop index id_unique 

<4>註意:如果刪除的唯一約束列具有自增長約束,則必須要先刪除自增長約束,再去刪除唯一約束。

4.主鍵約束

<1>概念
    當前行的數據不為空並且不能重覆
    相當於:唯一約束+非空約束

<2>添加主鍵約束
    在創建表的時候添加   : create .....primary key

        create table t4(
            id int primary key,
            name varchar(20)
        );

    create table t4(
        id int,
        name varchar(32),
        [constraint id_unique] primary key (id,name)   --  聯合約束
    );

    通過alter語句添加:alter....modify/change.........         /  alter .... add  primary key.....
        alter table t4 modify id int primary key;
        alter table t4 add constraint pk_id_name  primary key(id,name);

<3>刪除主鍵:alter ... drop primary key
        alter table t4 drop primary key

<4>註意:如果刪除的主鍵列具有自增長約束,則必須要先刪除自增長約束,再去刪除主鍵約束。

5.auto_increment:自增長約束

<1>概述
        列的數值自動增長,列的類型只能是整數類型
        通常給主鍵添加自增長約束。

<2>添加
    在創建表的時候添加   : create .....auto_increment
    create table t5(
        id int auto_increment,
        name varchar(20)
    );

        通過alter語句添加:alter....modify/change.........     auto_increment
        alter table t5 change  id id int auto_increment;

<3>刪除:alter ... modify/change....
        alter table t5 modify  id int;

<4>註意:
    一張表只能有一個自增長列,並且該列需要定義約束。

6.unsigned:無符號約束

<1>概念
    指定當列的數值未非負數

     age  tinyint 1   -128~127  UNSIGNED 0~255

<2>添加
    在創建表的時候添加   : create .....UNSIGNED
    create table t5(
        id int,
        age tinyint UNSIGNED
    );

        通過alter語句添加:alter....modify/change.........     
        alter table t5 change  age age int tinyint UNSIGNED;
        alter table t5 modify age tinyint UNSIGNED;

<3>刪除:alter ... modify/change....
        alter table t5 modify age tinyint;
        alter table t5 change  age age int;

7.zerofill:零填充約束

<1>概述:
    指定當前列的數值的顯示格式,不影響當前列顯示範圍

<2>
    在創建表的時候添加   : create .....zerofill

    create table t6(
        id int,
        age tinyint zerofill
    );

<3>刪除:alter ... modify/change....
        alter table t5 modify age tinyint;
        alter table t5 change  age age int;

8.外鍵約束

通過建立外鍵,設置表於表之間的約束性,限制數據的錄入

員工表(從表)                         部門表(主表)
員工號 員工姓名    部門名稱                部門號    部門名稱
1       張三      1                   1           人力
2       李四      2                   2           銷售
3       王五      3

<1>概述
    建立表和表之間的關係,建立參照完整性。一個表可以有多個外鍵,每個外鍵必須參照另一個主鍵。
    被外鍵約束的列,取值必須參照其主表列中的值。

    註意:通常先創建主表,再創建從表

    create table dept(
        deptno int primary key auto_increment,
        dname varchar(32),
        loc varchar(32)
    );

    create table emp(
        empno int primary key auto_increment,
        ename varchar(32) not null,
        deptno int
    );


<2>添加外鍵約束
    create table emp(
        empno int primary key auto_increment,
        ename varchar(32) not null,
        deptno int
        [constraint fk_name] foreign key (deptno) references dept(deptno);  -- 添加外鍵約束
    );

    使用alter add constraint....
        alter table emp add  constraint  fk_name foreign key (deptno)  references dept(deptno);

<3>刪除外鍵約束
        alter drop foreign key fk_name

        在創建表時,不去明確的指定外鍵約束的名稱,系統輝自動的隨機生成一個外鍵的名稱,
        使用 show create table 表名。查看具體的外鍵名稱。

    alter table emp drop foreign key fk_name;

<4>設置外鍵中的級聯關係
    1.on delete cascade:刪除主表中的數據時,從表中的數據隨之刪除。
    2.on update cascade:更新主表中的數據時,從表中的數據隨之刪除。
    3.on delete set null:刪除主表中的數據時,從表中的數據置空

級聯刪除
    create table emp(
        empno int primary key auto_increment,
        ename varchar(32) not null,
        deptno int
        [constraint fk_name] foreign key (deptno) references dept(deptno) on delete cascade;    -- 添加外鍵約束   
    );

註意:
    插入數據時,先插入主表的數據,在插入從表的數據。
    刪除數據時候,先刪除從表的數據,在刪除主表中的數據。

總結:資料庫的約束

主鍵約束、自增長約束、外鍵約束、唯一約束、非空約束、預設約束

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1、臨時IP配置 # ifconfig eth0 192.168.110.118 netmask 255.255.255.0 gateway 192.168.110.2 up # ifconfig eth0 192.168.110.118/24 up # ifconfig eth0:1 192.16 ...
  • 1. Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host' ...
  • 《SQL Server溫故系列》之分組查詢 GROUP BY。GROUP BY 是一種能將查詢結果劃分為多個行組的查詢語句的子句,其目的通常是為了在每個組上執行一個或多個聚合運算,所以 GROUP BY 通常會與聚合函數一塊兒出現在查詢語句中。本文主要講述了 SQL Server 中 GROUP B... ...
  • 一.認識資料庫 1.什麼是資料庫? 資料庫就是存儲數據的倉庫 存儲數據的方式1 存儲數據的方式2 電腦的性能進行擴展 訪問不同電腦上的文件數據 2.常見資料庫 關係型資料庫 資料庫可以為數據與數據之間建立關聯關係,人是一條數據,他可能關聯著一個工作崗位數據。雙方可以通過自身找到對方。 非關係型 ...
  • 連接資料庫等基礎操作請自行解決哈,本篇是重點記錄如何改密碼。 一、查詢用戶密碼: 查詢用戶密碼命令: host:允許用戶登錄的ip‘位置'%表示可以遠程; user:當前資料庫的用戶名; authentication_string:用戶密碼(後面有提到此欄位); 二、 設置(或修改)用戶密碼: 預設 ...
  • 本帖參考網站<https://blog.csdn.net/lx318/article/details/82686925>的安裝步驟,並對8.0.16版本的部分安裝問題進行修正 在MySQL 8.0.16版本中安裝可能會出現部分錯誤提示已經不使用“UTF8B3”而是使用了“UTF8B4” #///// ...
  • 二、資料庫設計 資料庫設計是將數據對象轉換為數據表等資料庫對象的過程,是資料庫應用系統開發過程中首要的和基本的內容。 按照規範的設計方法,考慮資料庫及其應用系統開發全過程,將關係資料庫的設計分為六個階段:需求分析、概念結構設計、邏輯結構設計、物理結構設計、資料庫實施和數控運行和維護。其中需求分析的任 ...
  • 之所以會寫下這篇日誌,是因為安裝的過程有點虐心。目前這篇文章是針對windows操作系統上的mysqldb的安裝。安裝python的mysqldb模塊,首先當然是找一些官方的網站去下載:https://pypi.python.org/pypi/MySQL-python。下載後,cmd進入MySQL- ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...