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.mzph.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