sql的外鍵約束和主鍵約束_SQL約束

sql的外鍵約束和主鍵約束

SQL | 約束條件 (SQL | Constraints)

Constraints are the guidelines implemented on the information sections of a table. These are utilized to restrict the kind of information that can go into a table. This guarantees the precision and unwavering quality of the information in the database.

約束是在表的信息部分上實施的準則。 這些用于限制可以進入表的信息的種類。 這保證了數據庫中信息的準確性和堅定不移的質量。

Constraints could be either on a segment level or a table level. The segment level constraints are applied uniquely to one segment, though the table level Constraints are applied to the entire table.

約束可以在段級別或表級別。 段級別約束唯一地應用于一個段,盡管表級別約束應用于整個表。

The following are probably the most usually utilized constraints accessible in SQL.

以下可能是在SQL中最常使用的約束。

  1. NOT NULL - Guarantees that a section can't have NULL worth.

    NOT NULL-保證節中的值不能為NULL。

  2. DEFAULT - Gives a default incentive to segment when none is determined.

    默認 -默認情況下,如果未確定細分原因,則會進行細分。

  3. PRIMARY KEY - Interestingly distinguishes each column/record in a database table.

    主鍵 -有趣地區分數據庫表中的每個列/記錄。

  4. UNIQUE - Guarantees that all qualities in a section are unique.

    唯一 -保證部分中的所有品質都是唯一的。

  5. FOREIGN KEY - Extraordinarily distinguishes a column/record in any of the given database tables.

    FOREIGN KEY-特別區分任何給定數據庫表中的列/記錄。

  6. CHECK - The CHECK requirement guarantees that all the qualities in a section fulfill certain conditions.

    檢查 -檢查要求可確保部分中的所有質量均滿足某些條件。

  7. INDEX - Used to make and recover information from the database rapidly.

    索引 -用于快速制作和從數據庫中恢復信息。

Constraints can be only be used either while creating a table (CREATE TABLE) or while altering any table (ALTER TABLE).

約束只能在創建表( CREATE TABLE )或更改任何表( ALTER TABLE )時使用。

Syntax:

句法:

    CREATE TABLE table_name 
(
column1 datatype Constraint,
column2 datatype Constraint,
column3 datatype Constraint,
....
);

放下約束 (Dropping Constraints)

Any constraint defined by the programmer can be dropped using the DROP CONSTRAINT option using the ALTER TABLE command.

可以使用ALTER TABLE命令使用DROP CONSTRAINT選項刪除程序員定義的任何約束。

Syntax:

句法:

    ALTER TABLE <table_name> DROP CONSTRAINT <column_name>

誠信約束 (Integrity Constraints)

Integrity constraints are utilized to guarantee the precision and consistency of the information in a social database. Information integrity is taken care of in a social database through the idea of referential integrity.

完整性約束用于保證社交數據庫中信息的準確性和一致性。 信息完整性是通過參照完整性的概念在社交數據庫中處理的。

There are numerous sorts of integrity constraints that assume a job in Referential Integrity (RI). These constraints incorporate Primary Key, Foreign Key, Unique Constraints and different constraints which are referenced previously.

有許多類型的完整性約束假定參照完整性(RI)中的工作 。 這些約束包括主鍵,外鍵,唯一約束和先前引用的不同約束。

Now let us see all constraints in detail with examples.

現在,讓我們通過示例詳細查看所有約束。

1)非空 (1) NOT NULL)

In the event that we determine a field in a table to be NOT NULL. At that point the field will never acknowledge invalid worth. That is, you will be not permitted to embed another column in the table without indicating any an incentive to this field.

如果我們確定表中的字段為NOT NULL 。 屆時,該領域將永遠不會承認無效價值。 就是說,您必須在未對該字段指示任何動機的情況下不允許在表格中嵌入另一列。

Example:

例:

CREATE TABLE Emp
(
id int NOT NULL,
name varchar(10) NOT NULL,
address varchar(20)
);

A table with named "Emp" will be created,

將創建一個名為“ Emp”的表,

IDNAMEADDRESS
ID 名稱 地址

2)獨特 (2) UNIQUE)

This constraint serves to exceptionally recognize each line in the table. For example for a specific segment, all the lines ought to have extraordinary qualities. We can have more than one UNIQUE section in a table.

此約束用于異常識別表中的每一行。 例如,對于特定的細分市場,所有生產線都應具有非凡的品質。 一個表中可以有多個UNIQUE節。

Example:

例:

CREATE TABLE emp
(
ID int NOT NULL UNIQUE,
NAME varchar(10),
ADDRESS varchar(20)
);
INSERT INTO emp(ID,NAME,ADDRESS)
VALUES(1123,'Hari','S colony');
INSERT INTO emp(ID,NAME,ADDRESS)
VALUES(1123,'Sunny','S colony');

This command will give an error as the ID constrains needs to be unique instead, we have kept it same as the other.

該命令將產生錯誤,因為ID約束需要唯一,我們將其保持不變。

3)主鍵 (3) PRIMARY KEY)

Primary Key is a field that remarkably recognizes each line in the table. On the off chance that a field in a table as Primary key, at that point the field won't have the option to contain NULL qualities just as all the lines ought to have one of a kind qualities for this field. In this way, as such, we can say this is a blend of NOT NULL and UNIQUE limitations.

主鍵是一個可以明顯識別表中每一行的字段。 如果將表中的某個字段用作主鍵的可能性很小,那么此時該字段將不能選擇包含NULL品質,就像該行的所有行都應具有一種品質一樣。 這樣,可以說這是NOT NULL和UNIQUE限制的混合。

Example:

例:

CREATE TABLE emp
(
ID int NOT NULL UNIQUE,
NAME varchar(10),
ADDRESS varchar(20),
PRIMARY KEY(ID)
);

"emp" table will be created. We have taken some of the records in it to use the table in further constraints.

將創建“ emp”表。 我們已將其中的一些記錄用于進一步限制的表中。

Table: emp

表:emp

IDNAMEADDRESS
112HariS colony
3211SunnyJ vihar
443MonaT Apartments
3212KaminiF plot
ID 名稱 地址
112 哈里 殖民地
3211 陽光明媚 維哈爾
443 莫娜 T公寓
3212 卡米尼 F圖

Here we can clearly see that each ID is NOT NULL value and is unique. Also, id is a primary key.

在這里我們可以清楚地看到每個ID都不是NULL值,并且是唯一的。 另外, id是主鍵。

4)外鍵 (4) FOREIGN KEY)

Foreign Key is a field in a table which exceptionally distinguishes each column of another table. That is, this field focuses to the primary key of another table. This normally makes a sort of connection between the tables.

外鍵是表中的一個字段,用于區別其他表的每一列。 即,該字段集中于另一個表的主鍵。 這通常在表之間建立某種連接。

Table: emp1

表:emp1

IDNAMEAGE
112Hari45
3211Sunny26
443Mona33
3212Kamini45
ID 名稱 年齡
112 哈里 45
3211 陽光明媚 26
443 莫娜 33
3212 卡米尼 45

Table: emp2

表:emp2

AGEDESIGNATIONSALARY
45Clerk35000
26Manager45000
33Accountant35000
45Receptionist25000
年齡 指定 薪水
45 文員 35000
26 經理 45000
33 會計 35000
45 接待員 25000

Query:

查詢:

CREATE TABLE emp1
(
ID int NOT NULL,
NAME int NOT NULL,
AGE int,
PRIMARY KEY (ID),
FOREIGN KEY (AGE) REFERENCES emp2(AGE)
);

5)檢查 (5) CHECK)

Utilizing the CHECK requirement we can indicate a condition for a field, which ought to be fulfilled at the hour of entering esteems for this field.

利用CHECK要求,我們可以指示一個字段的條件,應該在輸入該字段的評價時滿足該條件。

For instance, the underneath inquiry makes a table emp1 and determines the condition for the field AGE as (AGE >= 18) That is, the client won't be permitted to enter any record in the table with AGE < 18.

例如,下面的查詢創建一個表emp1并將字段AGE的條件確定為(AGE> = 18)。也就是說,不允許客戶在AGE <18的表中輸入任何記錄。

Table: emp1

表:emp1

IDNAMEAGE
112Hari45
3211Sunny26
443Mona33
3212Kamini45
ID 名稱 年齡
112 哈里 45
3211 陽光明媚 26
443 莫娜 33
3212 卡米尼 45

Query:

查詢:

CREATE TABLE emp1
(
id int NOT NULL,
name varchar(10) NOT NULL,
age int NOT NULL CHECK (AGE >= 18)
);

6)默認 (6) DEFAULT)

This requirement is utilized to give a default an incentive to the fields. That is, if at the hour of entering new records in the table in the event that the client doesn't indicate any an incentive for these fields, at that point the default worth will be doled out to them.

該要求用于默認激勵字段。 也就是說,如果在客戶未指示對這些字段進行任何激勵的情況下在表中輸入新記錄的時間,則屆時將向其分配默認值。

For instance, the underneath question will make a table named emp1 and determine the default an incentive for the field AGE as 18.

例如,下面的問題將創建一個名為emp1的表,并確定字段AGE的默認激勵為18。

CREATE TABLE emp1
(
ID int NOT NULL,
NAME varchar(10) NOT NULL,
AGE int DEFAULT 18
);

翻譯自: https://www.includehelp.com/sql/constraints.aspx

sql的外鍵約束和主鍵約束

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377019.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377019.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377019.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

nios pio interrupt 的使能

關于nios 中的中斷&#xff0c;因為要16c550中需要nios的中斷環境去測試&#xff0c;所以就用到了中斷。 硬件&#xff1a;在nios中添加硬件PIO,但是要使能中斷功能。如下圖所示&#xff1a; 系統列化&#xff0c;PIO的連接就不說了。但是要注意兩地方&#xff1a;edge type&am…

《單線程的build hash table、write rows to chunks、hash join的步驟以及流程圖》

Build Hash Table流程 1、初始化row buffer2、從build input table中讀一行3、若讀完build input table所有row&#xff0c;返回狀態READING_ROW_FROM_PROBE_item4、否則&#xff0c;向hash map中寫入一條row5、如果hash map 寫入成功&#xff0c;返回2&#xff0c;繼續執行6、…

在Scala的溪流

Scala | 流 (Scala | Streams) Stream in Scala is a type of lazy val. It is a lazy val whose elements are evaluated only when they are used in the program. Lazy initialization is a feature of Scala that increases the performance of the program. Scala中的Stre…

適合高速驅動電路的推挽電路

http://www.dzsc.com/data/html/2008-9-10/69023.html 圖1是使用NPN/PNP型晶體管的互補推挽電路&#xff0c;適于驅動功率MOSFET的門極。此電路雖然具有門極電流的驅動能力&#xff0c;但射極輸出波形不能比輸人信號快。 圖2是此電路的開關波形。它表示出tf、tr都快&#xff0c…

cholesky分解

接著LU分解繼續往下&#xff0c;就會發展出很多相關但是并不完全一樣的矩陣分解&#xff0c;最后對于對稱正定矩陣&#xff0c;我們則可以給出非常有用的cholesky分解。這些分解的來源就在于矩陣本身存在的特殊的 結構。對于矩陣A&#xff0c;如果沒有任何的特殊結構&#xff0…

socket編程常見函數使用方法

socket知識 有了IP地址&#xff0c;socket可知道是與哪一臺主機的哪一個進程通信 有了端口號&#xff0c;就知道是這個進程的哪一個套接字進行傳輸 應用進程使用描述符與它的套接字進行通信&#xff0c;也就是說一個進程創建一個套接字時就會返回一個套接字描述符 socket的…

需求變更流程不規范,項目早晚得完蛋

很多人&#xff0c;做的項目不少&#xff0c;但成功的不多。這是一個值得深思的問題。 項目為什么這么難做&#xff1f;需求蔓延&#xff0c;客戶難搞是基本原因。 如何解決上述問題&#xff1a; 1&#xff09;強化需求調研和項目設計在整個項目中的重要性 一般地&#xff0c;需…

html 表格套表格_HTML表格

html 表格套表格A table is a set of rows and columns, which could be created on a webpage in HTML, by <table> tag. The tabular representation of complex data makes it readable. 表格是一組行和列&#xff0c;可以通過<table>標簽在HTML網頁上創建。 復…

Android判斷界面

仿造微信&#xff0c;第一次進入去引導界面&#xff0c;否則進啟動界面。 package edu.hpu.init;import edu.hpu.logic.R;import android.app.Activity;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.os.H…

HDU計算機網絡系統2021復習提綱

目錄計算機網絡系統的主要功能TCP/IP模型與OSI模型的層次結構及各層功能。&#xff08;掌握&#xff09;TCP/IP參考模型各層次所對應的主要設備局域網的體系結構與IEEE.802標準數據鏈路層的編址方式和主要設備原理數據鏈路層CSMA/CD的技術原理交換機VLAN原理與劃分方法數據鏈路…

ruby 線程id_Ruby中的線程

ruby 線程idRuby線程 (Ruby Threads) In Ruby, with the help of threads, you can implement more than one process at the same time or it can be said that Thread supports concurrent programming model. Apart from the main thread, you can create your thread with …

Dynamic web project --- AspectJ Project

本來想今天晚上 直接轉到 以前的web項目 做測試。。。可惜在eclipse 添加 aspectj的時候 提示我不是 aspectj項目。。于是我就百度了好久&#xff0c;發現好多人都和我一樣 &#xff0c; 不過我也發現了一些可以的 比如右鍵 AJDTtools --> convert to Aspectj Project ,可惜…

2013 南京邀請賽 A play the dice 求概率

1 /**2 大意&#xff1a;給定一個色子&#xff0c;有n個面&#xff0c;每一個面上有一個數字&#xff0c;在其中的m個面上有特殊的顏色&#xff0c;當擲出的色子出現這m個顏色之一時&#xff0c;可以再擲一次。。求其最后的期望3 思路&#xff1a;假設 期望為ans4 ans 1/…

擲骰子

Description: 描述&#xff1a; In this article, we are going to see a dynamic programing problem which can be featured in any interview rounds. 在本文中&#xff0c;我們將看到一個動態的編程問題&#xff0c;該問題可以在任何采訪回合中體現。 Problem statement:…

《YOLO算法筆記》(草稿)

檢測算法回顧 5、6年前的檢測算法大體如下&#xff1a; 手動涉及特征時應該考慮的因素&#xff1a; 1、尺度不變性 2、光照不變性 3、旋轉不變性 這一步驟稱為特征工程&#xff0c;最重要的一個算法稱為sift&#xff0c;(回顧SIFT講解)體現了上述所有的觀點。 在分類的過程中…

U盤安裝Centos6.3

一 首先下載Centos6.3的光盤鏡像文件&#xff0c;網上到鏡像實在是太多了。 CentOS-6.3-i386-bin-DVD1.iso CentOS-6.3-i386-bin-DVD2.iso 二 下載個新版本的UltraISO, 在其菜單“啟動”下有“寫入硬盤鏡像“功能到&#xff0c;原來用到綠色版本是8.6.2.2011不支持&#xff0c;…

[轉]粵語固有辭彙與漢語北方話辭彙對照

本文轉自&#xff1a;http://beta.wikiversity.org/wiki/%E7%B2%B5%E8%AA%9E%E5%9B%BA%E6%9C%89%E8%BE%AD%E5%BD%99%E8%88%87%E6%BC%A2%E8%AA%9E%E5%8C%97%E6%96%B9%E8%A9%B1%E8%BE%AD%E5%BD%99%E5%B0%8D%E7%85%A7 粵語固有辭彙與漢語北方話辭彙對照 「粵語」&#xff08;或稱「…

openlayer調用geoserver發布的地圖實現地圖的基本功能

轉自&#xff1a;http://starting.iteye.com/blog/1039809 主要實現的功能有放大&#xff0c;縮小&#xff0c;獲取地圖大小&#xff0c;平移&#xff0c;線路測量&#xff0c;面積測量&#xff0c;拉寬功能&#xff0c;顯示標注&#xff0c;移除標注&#xff0c;畫多邊形獲取經…

LLVM與Codegen技術

LLVM 百度百科 LLVM是構架編譯器(compiler)的框架系統&#xff0c;以C編寫而成&#xff0c;用于優化以任意程序語言編寫的程序的編譯時間(compile-time)、鏈接時間(link-time)、運行時間(run-time)以及空閑時間(idle-time)&#xff0c;對開發者保持開放&#xff0c;并兼容已有…

跟烏克蘭人學編程1

今天要Disable一個菜單&#xff0c;工程項目多&#xff0c;不容易找。 烏克蘭人建議我用Spy&#xff0c;將靶拖到目標窗體上就可以看到類名。轉載于:https://www.cnblogs.com/SunWentao/archive/2012/12/19/2825220.html