MySQL学习笔记4:完整性约束限制字段
完整性约束是对字段进行限制,从而符合该字段达到我们期望的效果比如字段含有默认值,不能是NULL等如果插入的数据不满足限制要求,数据库管理系统就拒绝执行操作
完整性约束是对字段进行限制,从而符合该字段达到我们期望的效果比如字段含有默认值,不能是NULL等直观点说:如果插入的数据不满足限制要求,数据库管理系统就拒绝执行操作
设置表的主键
主键能够标识表中每条信息的唯一性,如同身份证号码和人的关系
人可以同名,但是身份证号码却是唯一的,
创建主键的目的在于快速查找到表中的某一条信息
单字段主键
代码如下:
mysql> create table student(
-> id int primary key,
-> name varchar(20),
-> sex boolean
-> );
Query OK, 0 rows affected (0.09 sec)
创建了三个字段,其中id为主键
多字段主键
多字段主键由多个属性组合而成,在属性定义完之后统一设置主键
代码如下:
mysql> create table student2(
-> id int,
-> course_id int,
-> score float,
-> primary key(id,course_id)
-> );
Query OK, 0 rows affected (0.11 sec)
student2表有三个字段,其中id和course_id的组合可以确定唯一的一条记录
设置表的外键
表的外键与主键是相对应的,比如表A中的id是外键,表B中的id是主键
那么就可以称表B为父表,表A为子表
设置表外键的作用在于建立与父表的联系,比如表B中id为123的学生删除后,表A中id为123的记录也随着消失
这样做的目的在于保证表的完整性
代码如下:
mysql> create table student3(
-> id int primary key,
-> course_id int,
-> teacher varchar(20),
-> constraint fk foreign key(id,course_id)
-> references student2(id,course_id)
-> );
Query OK, 0 rows affected (0.12 sec)
这里创建student3表,constraint后面的fk是外键别名,foreign key也就是设置外键的字段
references后的内容表示父表,和父表中的主键
需要注意的是,父表中的主键不能为空,并且主键和外键的数据类型要一致
设置表的非空约束
非空性很好理解,就是设置表中字段的值不能为空(NULL)
如果在已经设置此约束性条件的字段中插入空值,数据库系统则会报错
代码如下:
mysql> create table student4(
-> id int not null,
-> name varchar(20),
-> sex boolean
-> );
Query OK, 0 rows affected (0.10 sec)
这里的not null就是约束条件
设置表的唯一性约束
唯一性是指表中该字段的值不能重复出现,设置表的唯一性约束
也就是给表中某个字段加上unique
代码如下:
mysql> create table student5(
-> id int unique,
-> name varchar(20)
-> );
Query OK, 0 rows affected (0.10 sec)
此处id字段便不可重复
设置表的属性值自动增加
auto_increment主要用于为表中插入的新记录自动生成唯一的ID
一个表只能有一个字段使用auto_increment约束
并且该字段必须为主键的一部分
代码如下:
mysql> create table student6(
-> id int primary key auto_increment,
-> name varchar(20)
-> );
Query OK, 0 rows affected (0.12 sec)
这里的id是主键,并且会自动增加id值,比如1,2,3,4……
需要注意的是,auto_increment约束的值必须是整数类型
设置表中属性的默认值
在表中插入一条新的记录时,如果没有为该字段赋值
那么数据库系统会自动为该字段赋上一条默认值
代码如下:
mysql> create table student7(
-> id int primary key,
-> score int default 0
-> );
Query OK, 0 rows affected (0.10 sec)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Title: Windows 11 Memory Integrity is turned off, how to fix it? With the launch of Windows 11, many users have upgraded to the new system. However, some users may encounter problems with Memory Integrity being turned off when using Windows 11, which affects the performance and stability of the computer. This article will discuss the problem of turning off memory integrity in Windows 11 and provide some solutions and suggestions. 1. Causes of Memory Integrity Shutdown Problem in Windows 11

Learn the database functions in the Go language and implement the addition, deletion, modification, and query operations of PostgreSQL data. In modern software development, the database is an indispensable part. As a powerful programming language, Go language provides a wealth of database operation functions and toolkits, which can easily implement addition, deletion, modification and query operations of the database. This article will introduce how to learn database functions in Go language and use PostgreSQL database for actual operations. Step 1: Install the database driver in Go language for each database

In the recently released Windows 11 operating system, Microsoft introduced a new feature, memory integrity. This feature is designed to improve system security and protect user privacy and data from malware attacks. However, sometimes users may need to temporarily turn off memory integrity in order to install some specific software or perform some special operations. So, after Windows 11 memory integrity is turned off, how to turn it on? First of all, turning off memory integrity in Windows 11 is not a simple operation,

How to achieve data consistency and integrity of PHP functions through microservices? Introduction: With the rapid development of the Internet and the continuous innovation of technology, microservice architecture has become one of the most popular architectures today. As a method of building small services that are deployed independently, microservices architecture provides many advantages such as flexibility, scalability, and independent deployment. However, when we use PHP as a development language to implement a microservice architecture, how to ensure data consistency and integrity becomes an important task. This article will describe how to use PHP

Title: Method to confirm whether Oracle installation is complete and effective, specific code examples are required. With the development of information technology, database management systems have become one of the key tools for enterprise data management and processing. As the world's leading relational database management system, Oracle is widely used in enterprises. However, a complete and effective Oracle installation is critical to an enterprise's data management and business operations. This article will introduce how to confirm whether the Oracle installation is complete and valid, and provide specific code examples to help management

With the widespread application of Internet technology, data security issues have received increasing attention. For sensitive information stored in databases, ensuring data confidentiality and integrity is critical. MySql database is an open source relational database management system that provides a variety of encryption technologies to ensure data security. 1. Transmission encryption Transmission encryption ensures data security by encrypting the network connection used during data transmission. For the MySql database, SSL and SSH represent the two main transmission encryption technologies. SSL (

How to use MySQL's foreign keys and constraints to improve data integrity and consistency? In the MySQL database, foreign keys and constraints are two important concepts that can help improve data integrity and consistency. In this article, we will discuss in detail how to use MySQL's foreign keys and constraints to achieve this goal, and provide some code examples. 1. The concept and function of foreign keys Foreign keys are a mechanism used to establish relationships between tables, which can ensure the consistency of data between related tables. Foreign keys usually consist of a table's primary key (or unique

As web applications continue to evolve, PHP has become one of the most widely used server-side scripting languages. In the PHP development process, in order to achieve efficient, maintainable and scalable code, code specifications are essential. This article will introduce how to implement code specification constraints in PHP to ensure that the code style is consistent and easy to understand, thereby improving the quality of internal team collaboration and external delivery. 1. PSR Standard PHP Programming Specifications (PHPStandardRecommendations) abbreviated as P
