当前位置:网站首页>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
边栏推荐
- 数学规划分类 Math Programming Classfication
- If jimureport building block report is integrated according to the framework
- Imitation Tiktok homepage interface
- Math programming classification
- Smart S7-200 PLC channel free mapping function block (do_map)
- Redis memory elimination mechanism?
- 启牛商学院和微淼商学院哪个靠谱?老师推荐的开户安全吗?
- 『Skywalking』.NET Core快速接入分布式链路追踪平台
- Why is the integer type 128 to byte -128
- IPv4 addresses have been completely exhausted, and the Internet can work normally. NAT is the greatest contributor!
猜你喜欢

完啦,上班三个月,变秃了

Whether the five distribution methods will produce internal fragments and external fragments

Usage of in in SQL DQL query

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

Ffmpeg plays audio and video, time_ Base solves the problem of audio synchronization and SDL renders the picture

xxl-job中 关于所有日志系统的源码的解读(一行一行源码解读)

ML-Numpy

Redis foundation 2 (notes)

Xiaobai programmer's fourth day

How to resolve a domain name to multiple IP addresses?
随机推荐
How to implement an app application to limit users' time use?
Usage of in in SQL DQL query
对需求的内容进行jieba分词并按词频排序输出excel文档
如何将一个域名解析到多个IP地址?
D3.js 学习
面向领域模型编程
数据平台下的数据治理
Xiaobai programmer's sixth day
On the difference between break and continue statements
Data quality: the core of data governance
ArcGIS10.2配置PostgreSQL9.2标准教程
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
Redis基础2(笔记)
MySQL --- 子查询 - 列子查询(多行子查询)
H5幸运刮刮乐抽奖 免公众号+直运营
SQL中in的用法 DQL 查询
Whether the five distribution methods will produce internal fragments and external fragments
Output Yang Hui triangle with two-dimensional array
Get together for ten years, tell your story, millions of gifts are waiting for you
SQL basic statement DQL select and extract DML insert delete