当前位置:网站首页>Five constraints and three paradigms
Five constraints and three paradigms
2022-07-25 22:24:00 【Who is Huang Huang】
One . Five constraints (z,w,f,m,w)
Memory method :
Chase me, Fermat WOW ,
Primary key 、 only 、 Non empty 、 Default 、 Foreign keys
Primary key 、unique、not null、default、 Foreign keys
1.1 Primary key constraint : Unique primary key
1. characteristic : only Non empty There is only one primary key in a table
1.2 Unique constraint unique
1. keyword unique
2. characteristic : only Can't repeat It can be for null
3. The difference between primary key constraint and unique constraint
A. Primary key constraint One There can only be One individual Unique constraint **** One There can be many individual
B. The primary key constraint cannot be empty only Constraint is You can set null
C. The unique constraint can be set as a joint unique constraint ( Multiple columns form a constraint )
4. There are two ways to create unique constraints
A. When creating a table, set
B. After the table is created, set
a: Set unique constraints when creating tables
CREATE TABLE p1(
pid INT(11) PRIMARY KEY AUTO_INCREMENT,
pname VARCHAR(20) UNIQUE
);
b: Set the unique constraint after creating the table
# Create table
CREATE TABLE p1(
pid INT(11) PRIMARY KEY AUTO_INCREMENT,
pname VARCHAR(20)
);
# Add primary key constraint
grammar :
alter table Table name add CONSTRAINT The name of the unique constraint unique( Name )
# Add unique constraint
ALTER TABLE p2 ADD CONSTRAINT u_name UNIQUE(pname)
# Delete unique constraint
ALTER TABLE p2 DROP INDEX u_name
c Union sole constraint
CREATE TABLE p3(
pid INT(11) PRIMARY KEY AUTO_INCREMENT,
pname VARCHAR(20),
pwd VARCHAR(20), UNIQUE(pname,pwd)
);
INSERT INTO p3(pname,pwd)VALUES(" Brother Mao ","123");
1.3 Non empty constraint not null
1. keyword : not null
2. effect : When inserting data, you must insert data Can't insert null
3. Set when creating a table
1.4 Default constraint default
1. keyword :default
2. effect : be used for Set the default value of the column
3. Set when creating a table
4. explain :
A. After setting the default constraint for the specified column No data inserted Show default
B. After setting the default constraint for the specified column Insert default It's also the default value
C. When inserting specific data Will replace the default value
1.5 Foreign key constraints
Foreign key constraints are used between two tables , Used to ensure the integrity of associated data .
Be careful :
The key association should be noted :
1. The foreign key must be the primary key of the main table ,
2. When deleting a table, delete the sub table first, and then delete the main table .
3. You can force the deletion of , Regardless of constraints :drop table orders cascade constraint;
4. You can use cascading updates and cascading deletes , In this way, update or delete operations are performed in the main table , The data in the sub table will also be updated or deleted synchronously
Example :
order surface
create table orders(
order_id number(10),
total_price number(10,2),
order_time date,
constraint orders_order_id_pk primary key (order_id)
);
order A detailed list
create table order_detail(
detail_id number(10),
order_id number(10),// stay order In the table is the primary key
item_name varchar2(10),
quantity number(10),
constraint order_detail_detail_id_pk primary key (detail_id),
constraint order_detail_order_id_fk foreign key (order_id)
referencs orders(order_id)|on delete cascade|on update cascade
);
Two . Three paradigms
2.1
The three paradigms are also used to constrain the database Ensure data integrity And correctness
2.2
A. The first paradigm ( Atomicity ): Each column in the table is the smallest atomic unit No further splitting
B. The second paradigm ( Dependence ): A table must have a primary key ; Non primary key classes must rely entirely on primary keys , Instead of relying on only part of the primary key
C. The third paradigm (): On the basis of following the first two paradigms No local dependencies ( Cannot pass dependencies ) You can only establish a dependency relationship with the primary key
边栏推荐
猜你喜欢

Ts:typera code fragment indentation display exception (resolved) -2022.7.24

Matrix of C language

Victoriametrics single node of kubernetes

Don't know mock test yet? An article to familiarize you with mock

How to resolve a domain name to multiple IP addresses?

xss-工具-Beef-Xss安装以及使用

Method of converting MAPGIS format to ArcGIS

分享两个音乐播放地址

Randomly generate 10 (range 1~100) integers, save them to the array, and print the array in reverse order. And find the average value, the maximum value and the subscript of the maximum value, and fin

PySpark数据分析基础:pyspark.sql.SparkSession类方法详解及操作+代码展示
随机推荐
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No suc
Redis foundation 2 (notes)
Leetcode 106. 从中序与后序遍历序列构造二叉树
Common source code for ArcGIS development
C language: random generated number + selective sorting
El expression improves JSP
【PMP学习笔记】第1章 PMP体系引论
点亮字符串中所有需要点亮的位置,至少需要点几盏灯
编译和反编译
4day
字符型常量和字符串常量的区别?
torchvision
还不懂mock测试?一篇文章带你熟悉mock
JSP nine built-in objects
What is partition and barrel division?
Jenkins+svn configuration
2day
Wkid in ArcGIS
Call of addition, subtraction, multiplication and division of integer type only
Matrix of C language